0

I have a structure sportist

struct sportist{           
    string name;
    string surname;
    int goals;
    string tim;

}

Here is the function that should read the values.

 void read(sportist x[],int n)     
{
    int i;
    for(i=0;i<n;i++)
    {

        cout<<"************************************************"<<endl;
        cout<<"Name:";
        cin>>x[i].name;
        cout<<endl<<"Surname:"; 
        cin>>x[i].surname;
        cout<<endl<<"Goals :";
        cin>>x[i].goals;
        cout<<endl<<"Name of the team:";
        cin>>x[i].tim;

    }

My question is how can I use pointers, because I need to? My attempt:

 void read(sportist* x,int n)
{
    int i;
    for(i=0;i<n;i++)
    {

        cout<<"************************************************"<<endl;
        cout<<"Name:";
        cin>>x->name;
        cout<<endl<<"Surname:"; 
        cin>>x->surname;
        cout<<endl<<"Goals :";
        cin>>x->goals;
        cout<<endl<<"Name of the team:";
        cin>>x->tim;

    }
} 

What I want is to sort the sequence of athletes and teams by the number of goals and print them on the screen to sort them in a popup order. But it shows me errors when I debug.

  • C++ does provide lists, vectors and smart pointers (and so on). If you don't have to use c-style arrays and raw pointers, don't do it – RoQuOTriX Mar 31 '20 at 14:37
  • My assignment is to use pointers too. That's why @RoQuOTriX –  Mar 31 '20 at 14:38
  • 1
    You're already using pointers. In a parameter type, `sportist x[]` is equivalent to `sportist* x`. `sportist* x[]` is the same as `sportist** x`. – molbdnilo Mar 31 '20 at 14:44
  • Maybe you should add this to the question, that you NEED to use pointers ;) – RoQuOTriX Mar 31 '20 at 14:44
  • Define "use pointers." Do you need to pass a pointer, or an array of pointers? –  Mar 31 '20 at 14:46
  • An array of pointers @Chipster –  Mar 31 '20 at 14:49
  • Hmm, [looks like you got it right](https://stackoverflow.com/a/1719073/10957435). Are you getting an error? If so, can you add it to your question? –  Mar 31 '20 at 14:54
  • Nipicking but it's not possible to pass an array to a function in C++. Everything is done with pointers not arrays. – john Mar 31 '20 at 14:54
  • I edited my question(my attempt). I assume I got it right? –  Mar 31 '20 at 14:58

1 Answers1

0

you should pay attention to one point when you are using array x[i] with i increasing ,you are traversing the array ,but with pointer you should move pointer so it would point to next elements of array.You should use x++;.

look:

void read(sportist* x, int n)
{
    int i;
    for (i = 0; i < n; i++)
    {

        cout << "************************************************" << endl;
        cout << "Name:";
        cin >> x->name;
        cout << endl << "Surname:";
        cin >> x->surname;
        cout << endl << "Goals :";
        cin >> x->goals;
        cout << endl << "Name of the team:";
        cin >> x->tim;
        x++;
    }
}

if you miss x++; each time you will write entered data in first element of array.

Also note in function you are declaring this array of sportist , if you declare sportist* x instead of sportist x[num] ,you have to allocate memory for it too.

hanie
  • 1,863
  • 3
  • 9
  • 19
  • How can I allocate memory? –  Mar 31 '20 at 16:19
  • @Klea you should use `malloc` like this `sportist * x=(sportist *)malloc(sizeof(sportist));` – hanie Mar 31 '20 at 16:21
  • Where should I ? @hanie –  Mar 31 '20 at 16:34
  • because when you define an array required memory will be reserved ,but with pointers it's not like that.Check this (https://stackoverflow.com/questions/37549594/crash-or-segmentation-fault-when-data-is-copied-scanned-read-to-an-uninitializ) – hanie Mar 31 '20 at 16:38