about struct

Jun 25, 2012 at 7:00pm
hi everyone could you plase say what is wrong with this code i just get the first person's information in the array


#include <iostream>
#include<conio.h>
using namespace std;



struct human{
string name,surname;
int age;

};

void read_(human *h){

cout<<h->name<<" "<<h->surname<<" "<<h->age<<endl;
}
int main(){
human i[3];
for(int k=0;k<3;k++){
cout<<"name "<<endl;
cin>>i[k].name;
cout<<"surname"<<endl;
cin>>i[k].surname;
cout<<"age "<<endl;
cin>>i[k].age;



}
for(int z=0;z<3;z++){
read_(z[&i]);


}
getch();

}
Jun 25, 2012 at 7:11pm
It is the compiler that reports errors. So next time do not bother the forum and read compilation errors!
I can point out an invalid construction

read_(z[&i]);

Also interesting what idiot did teach you to use such names as read_ with trailing underscore?!
Last edited on Jun 25, 2012 at 7:13pm
Jun 25, 2012 at 7:18pm
@vlad - That is an error but it's not a compiler error. A good compiler should warn about it though.
Jun 25, 2012 at 7:20pm
firstly i dont bother anyone secondly you dont have to answer my question thirdly nobady teach me anything i am trying to learn c++ with resources i found on the net . and last thing please dont help me next time
Jun 25, 2012 at 7:21pm
@Peter87
Why do you have decided that it is not a compilation error? In fact a pointer is converted to integral type. And, secondly. z is not an array! How this code can be compiled?!
Last edited on Jun 25, 2012 at 7:28pm
Jun 25, 2012 at 7:25pm
@digrev

Your question is
could you plase say what is wrong with this code


and I pointed out that it shall not be compiled. So you should show at least the code that is compiled before ask why something is not read.
Last edited on Jun 25, 2012 at 7:26pm
Jun 25, 2012 at 7:30pm
1. You should not use the '_' character in the beginning and end of your function names, they are ONLY used for C++'s Standard Libraries's Internal Utilities, and using it may give you errors. So just remove it with some other name. You can anyways use the '_' character, if the first letter after this symbol is a Uppercase letter, like: _READ instead of _read. Or maybe it was lowercase, i don't remember. But for security reasons don't use it, eventually use read_f. There should be no problem using a '_' INSIDE a function name.

2. Into the statement read_(z[&i]); You missed it. It should be: read_(&z[i]);.

3. There is no third point. That should do the "job".
Last edited on Jun 25, 2012 at 7:31pm
Jun 25, 2012 at 7:34pm
@vlad
&i gives you a pointer to the array (human(*)[3]).
z[&i] is the same as (&i)[z]. Using operator[] on the pointer we get an array (human[3]). An array decays to a pointer (human*) which is exactly what the function expects as argument.
Jun 25, 2012 at 7:36pm
EssGeEich thakn you somuch friend it works now.. and thanks peter
Last edited on Jun 25, 2012 at 7:39pm
Jun 25, 2012 at 7:40pm
@Peter87
Thanks, I have also seen it but at the first glance it seems as an error. Of course a[0] and 0[a] are equivalent. But in any case such constructions should be avoided because they make the code difficult to read.:)

Last edited on Jun 25, 2012 at 7:46pm
Topic archived. No new replies allowed.