Apr 20, 2013 at 1:49pm UTC
What is wrong with this code?
Compiling error:
`fullname' has not been declared
1 2 3 4 5 6 7 8 9 10 11 12
struct student{
char fullname[30];
char hobby[30];
int ooplevel;
};
int getinfo(student* students[], int size){
for (int i=0; i<size; i++){
cout << "Student (" << (i+1) << ")\n" ;
cout << "Enter name: " ;
cin.get((students+i)->fullname,30); <-- [COMPILE ERROR OCCURENCE]
}
}
Last edited on Apr 20, 2013 at 1:52pm UTC
Apr 20, 2013 at 1:55pm UTC
Do you know what student* students[]
means? Array of student pointers. Is this what you want?
Apr 20, 2013 at 2:11pm UTC
Yeah, I want an array of student strucs than can be accesed through the -> (assignment operator) AKA pointers.
The problem for me right now is, that I have problems with accessing the char variables for some reason?!
Apr 20, 2013 at 2:14pm UTC
Oh wait, seems I fixed it with:
cin.get((*students+i)->fullname,30);
Have no idea why this works tho.
Apr 20, 2013 at 2:16pm UTC
Just do this: int getinfo(student* students, int size){
.
I made a mistake maybe... I need to see more of the code to tell for sure.
Last edited on Apr 20, 2013 at 2:19pm UTC
Apr 20, 2013 at 2:36pm UTC
Mehh. My program became I giant clusterfuck.
Tho I found the real reason for the problem.
It's not:
int getinfo(student* students[], int size)
But instead:
int getinfo(student students[], int size)
or:
int getinfo(student* students,int size)
They're both the same cause an array is the same as a pointer and the other way around.
Anyways, atleast I learned something today ;)
Last edited on Apr 20, 2013 at 3:01pm UTC
Apr 20, 2013 at 3:13pm UTC
Minimacfox wrote:They're both the same cause an array is the same as a pointer and the other way around.
An array is not the same as a pointer. It's just that both syntaxes, when used in the function parameter list, declares a pointer.
Last edited on Apr 20, 2013 at 3:13pm UTC
Apr 20, 2013 at 3:40pm UTC
1 2 3 4 5 6 7 8
int getinfo(student students[], int size){ // "students" parameter changed
for (int i=0; i<size; i++){
cout << "Student (" << (i+1) << ")\n" ;
cout << "Enter name: " ;
cin.get((students+i)->fullname,29); // Reduced 2nd parameter to 29
// to save space for a null-character
}
}
Minimacfox wrote:"They're both the same cause an array is the same as a pointer and the other way around. "
1 2 3
void Function int Array[] ); // IS: int Array*
void Function int *Array[] ); // IS: int Array**
void Function int Array[][] ); // IS: int Array**
Wazzak
Last edited on Apr 20, 2013 at 3:41pm UTC
Apr 20, 2013 at 3:44pm UTC
Framework wrote:1 2
cin.get((students+i)->fullname,29); // Reduced 2nd parameter to 29
// to save space for a null-character
This is unecessary because get already takes the null-character in to account.
Framework wrote:void Function int Array[][] ); // IS: int Array**
Even if you add the missing parenthesis this is not valid.
Last edited on Apr 20, 2013 at 3:52pm UTC