Thread: struct and string

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    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;
    }
    What is C++?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    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.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    The only thing I see wrong with it is:

    Code:
    num1.age = "16";
    It should be:

    Code:
    nume1.age = 16;

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    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
    What is C++?

  6. #6
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326

    Exclamation

    Heh, I think I know what it is, #include <string>

    Is that what the problem is?

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    317
    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!

  8. #8
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    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]

  9. #9
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    nope exact same errors..

    And sorry josh.. that was there the whole time...

    itll be okay..

    [edit]
    Hey I live in alabama too!
    [/edit]
    What is C++?

  10. #10
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    Cool, I have lived here in Mobile my whole life? What part of Alabama are you from?

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    317
    The code I posted compiled and ran fine.

  12. #12
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Montgomery...

    And it wont compile for me
    What is C++?

  13. #13
    Registered User
    Join Date
    May 2002
    Posts
    317
    What compiler do you have?

  14. #14
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Borland C++ 5.02
    What is C++?

  15. #15
    Registered User
    Join Date
    May 2002
    Posts
    317
    Have you tried :
    Code:
    #include <cstring>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM