Structures Help

This is a discussion on Structures Help within the C++ Programming forums, part of the General Programming Boards category; i have a program at the moment that is asking the user to input thier name, age and course. the ...

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Structures Help

    i have a program at the moment that is asking the user to input thier name, age and course. the details are stored in a stucture and is the passed to the function by the caller. the function collects the data and then on the return to the caller the stucture contains the useres details. then main() should output the contents of the structure.

    at the moment i have this but for some reason when you put your name in it skips Age, and Course. also i am unsure if it is doing what i have explained corectly. this is what i have so far

    Code:
    #include <iostream>
    using namespace std;
    struct myStruct
    {
       char Name;
       int age;
       char course;
    };
    
    void myFunc1(myStruct m)    // by value
    {
        cout<<"Enter you name";
        cin >>m.Name;
    }
    
    void myFunc2(myStruct &m)  // by reference
    {
        cout<<"Age: ";
        cin>>m.age;
    }
    
    void myFunc3(myStruct &m)
    {
        cout<<"Course: ";
        cin >>m.course;
    }
    
    int main()
    {
        myStruct m;
        m.Name;
        m.age;
        m.course;
        myFunc1(m);
        myFunc2(m);
        myFunc3(m);
        system("pause");
    }
    any help will be much appreciated

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i havent looked at the rest of the code but right off the bat i see this:
    Code:
    struct myStruct
    {
       char Name;
       int age;
       char course;
    };
    what datatype is Name? what values is it cabable of holding? what value are you intending to save in it? here lies your problem

    edit: after looking at the rest of the code...
    Code:
    m.Name;
    m.age;
    m.course;
    these lines dont do anythign so get rid of them. also, be consistent with your naming, make 'Name' lowercase 'name'.
    Last edited by nadroj; 10-30-2006 at 11:47 AM.

  3. #3
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    how do you add datatypes to the name and the values of what it can hold?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    there are 2 important points in this, scan through to find the relevant points or read it all, it wouldnt hurt.http://www.cplusplus.com/doc/tutorial/variables.html

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    A char holds a single character. You want a string to hold a string of characters. Use the C++ string class in <string> as you have in earlier programs.

  6. #6
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    hey managed to figure that bit out well i think i have, this is what i have got.

    Code:
    const int NAME_LEN = 10;
    const int C_LEN = 20;
    const int AGE = 10;
    
    struct myStruct
    {
       char name[NAME_LEN];
       int age[AGE];
       char course[C_LEN];
    };
    just now need a bit of help to output it all in the way it was explained in the first post.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    as daved said, you want a 'string'. use my link, and maybe your browsers search function and the word 'string' to figure it out, its on there!

    the way you have it will work but is very susceptible to errors. your 'age' was fine how you initially had it, just name and course can be better.
    Last edited by nadroj; 10-30-2006 at 12:10 PM.

  8. #8
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    do i actually need the amount of functions i have or would it be better using less?

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if you are just learning structures then dont worry to much about efficiency like that.

    did you solve the string problem? as said previously, youll need to #include <string> and change the 2 char variables to 'string's..

  10. #10
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    i have included the <string> but trying to get the string bit working, but not happening at the moment

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    post your updated code and let us know 'what isnt working' and we'll help

  12. #12
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    this is the example of the type of string i am trying to use

    Code:
    char stringname[10];
    	cout << "First Name: ";
        cin.getline(stringname, 10);
        
    	cout << stringname  << endl;
    is this correct?

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if you included string then declare the variables as strings instead of char[]. then just use: cin >> myString; to read input. if you need to save spaces i think you have to use: getline(cin,myString);

    edit: yes that does look correct. but it would be easier to use 'string' variables because they are easier. no need to worry about out of bounds or indexes, etc, also the string class has many useful functions.
    Last edited by nadroj; 10-30-2006 at 12:58 PM.

  14. #14
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    this is my new string code is it the same or better?

    Code:
    string mystring;
    	cout << "First Name: ";
        cin>>mystring;
        
    	cout << mystring  << endl;
    if it is what is next to do to get it all working correctly?

  15. #15
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    this is what i have done to the first bit of the program using a sting, can you let me know if it is right or not, if not, what i can do to correct it?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    const int NAME_LEN = 10;
    const int C_LEN = 20;
    
    struct myStruct
    {
       string name[NAME_LEN];
       int age;
       char course[C_LEN];
    };
    
    void myFunc1(myStruct m)    // by value
    {
        	cout << "First Name: ";
            cin >> m.name;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21