-
struct and string
I was tryin to compile this and i brought up several errors...
If you cannot tell me the problem without the errors let me know.
Code:
#include <iostream.h>
#include <string.h>
int main()
{
struct person {
string first_name;
string second_name;
int age;
};
person num1;
num1.first_name = "John";
num1.second_name = "Doe";
num1.age = "16";
cout << "Person 1: "<< num1.first_name<<" "<<num1.second_name<<endl;
cout << "Age: "<<num1.age;
cin.get();
return 0;
}
-
Code:
#include <iostream>
#include <string>
using namespace std;
struct person {
string first_name;
string second_name;
int age;
} num1;
int main()
{
num1.second_name = "Doe";
num1.age = 16;
cout << "Person 1: "<< num1.first_name<<" "<<num1.second_name<<endl;
cout << "Age: "<<num1.age;
cin.get();
return 0;
}
That should work. Compiled without warning with gcc and -Wall (all warnings) flag.
-
General remark:
>If you cannot tell me the problem without the errors let me know.
Most of us can, but knowing the error messages saves us all the work the compiler already did for you, so we don't have to do it again. So please always post them.
-
The only thing I see wrong with it is:
It should be:
-
Code:
#include <iostream.h>
#include <string.h>
struct person {
string first_name;
string second_name;
int age;
} num1;
int main()
{
num1.second_name = "Doe";
num1.age = 16;
cout << "Person 1: "<< num1.first_name<<" "<<num1.second_name<<endl;
cout << "Age: "<<num1.age;
cin.get();
return 0;
}
errors:
Line 5,19 Type name expected
Line 5,19 Declaration Missing ;
Line 6,20 Type name expected
Line 6,20 Multiple declaration for person::string
Line 5,20 Earlier Declaraton of person::string
Line 6,20 Declaration missing ;
Line 13,18 second_name is not a member of person
Line 15,40 first_name is not a member of person
Line 15,63 second_name is not a member of person
-
Heh, I think I know what it is, #include <string>
Is that what the problem is?
-
Here you go Vicious, no underscore:
Code:
#include <iostream>
#include <string>
using namespace std;
struct person {
string firstname;
string secondname;
int age;
} num1;
int main()
{
num1.firstname = "John";
num1.secondname = "Doe";
num1.age = 16;
cout << "Person 1: "<< num1.firstname<<" "<<num1.secondname<<endl;
cout << "Age: "<<num1.age;
cin.get();
return 0;
}
Have fun!
-
Damn, I swear when I read your code earlier I did not see #include <string.h>, did you add that or did I just overlook it?
[edit] I mean the post vicious made right before my previous post.[/edit]
-
nope exact same errors..
And sorry josh.. that was there the whole time...
itll be okay..
[edit]
Hey I live in alabama too!
[/edit]
-
Cool, I have lived here in Mobile my whole life? What part of Alabama are you from?
-
The code I posted compiled and ran fine.
-
Montgomery...
And it wont compile for me :(
-
What compiler do you have?
-
-