A problem with function arguments
Feb 18, 2014 at 1:48am UTC
Hello, I have been doing some exercises in my book, and have made the following code, with one compiler error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#include <iostream>
using namespace std;
struct student
{
char fullname[30];
char hobby[30];
int ooplevel;
};
int getinfo(student*, int );
void display1(student);
void display2(student);
void display3(const student*, int );
int main()
{
cout << "Enter class size \n" ;
int class_size;
cin >> class_size;
while (cin.get() != '\n' );
student *pt_stu = new student[class_size];
int entered = getinfo(pt_stu, class_size);
for (int i = 0; i < entered ; i++)
{
display1(pt_stu[i]);
display2(pt_stu+i); //Here
}
display3(pt_stu, entered);
delete [] pt_stu;
cout << "Done\n" ;
return 0;
}
int getinfo(student *ar_student, int size)
{
int i = 0;
cout << "Enter info for each student:\n" ;
for (i = 0; i < size; i++)
{
cout << "Student " << i+1 << " name: " ;
if (!(cin.getline(ar_student[i].fullname, 30)))
{
cout << "Input terminated\n" ;
cin.clear();
while (cin.get() != '\n' );
break ;
}
cout << "Student " << i+1 << " hobby (NONE for no hobby): " ;
while (!(cin.getline(ar_student[i].hobby, 30)))
{
cout << "Error, try again:\n" ;
cin.clear();
while (cin.get() != '\n' );
}
cout << "Enter op level(-1 for undefined): " ;
while (!(cin >> ar_student[i].ooplevel))
{
cin.clear();
while (cin.get() != '\n' );
}
}
return i;
}
void display1(student sumstudent)
{
cout << "(By Value)\n" ;
cout << "Name: " << sumstudent.fullname << endl;
cout << "Hobby: " << sumstudent.hobby << endl;
if (sumstudent.ooplevel == -1)
cout << "ooplevel: Undefined\n" ;
else
cout << "ooplevel: " << sumstudent.ooplevel << endl;
}
void display2(student *sumstudent) //and here
{
cout << "(By address)\n" ;
cout << "Name: " << sumstudent->fullname << endl;
cout << "Hobby: " << sumstudent->hobby << endl;
if (sumstudent->ooplevel == -1)
cout << "ooplevel: Undefined\n" ;
else
cout << "ooplevel: " << sumstudent->ooplevel << endl;
}
void display3(const student *ar_stu, int size)
{
cout << "List all:\n" ;
for (int i = 0; i < size; i++)
{
cout << "Name: " << ar_stu[i].fullname << endl;
cout << "Hobby: " << ar_stu[i].hobby << endl;
if (ar_stu[i].ooplevel == -1)
cout << "ooplevel: Undefined\n" ;
else
cout << "ooplevel: " << ar_stu[i].ooplevel << endl;
}
}
The compiler says that:
error: could not convert ‘(pt_stu + ((sizetype)(((long unsigned int )i) * 64ul)))’ from ‘student*’ to ‘student’
I've tried experimenting with other functions and purposely giving them wrong arguments, but the compiler errors with these experiments are different.
I also tried using
display2(&pt_stu[i]);
, but that didn't do much?
Can someone lend a hand?
(by the way, does the stack overflow community accept beginner questions?)
Feb 18, 2014 at 2:13am UTC
The error message gives you a pretty huge clue. You've declared display2() to take an argument of type student (on line 11), but you're trying to pass in a pointer to student instead.
Feb 18, 2014 at 6:49pm UTC
Didn't even consider looking at the prototype, now i feel really stupid..
thanks
Feb 18, 2014 at 7:09pm UTC
No problem - it's easily done! You're welcome :)
Topic archived. No new replies allowed.