Structures Help

This is a discussion on Structures Help within the C++ Programming forums, part of the General Programming Boards category; sorry im not recieving emails when people are responding to my subscribed threads right now, dont know why. string name[NAME_LEN]; ...

  1. #16
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    sorry im not recieving emails when people are responding to my subscribed threads right now, dont know why.

    string name[NAME_LEN]; creates an array of 10 strings. you dont want this i dont think. you just want ONE string for ONE name, correct? if so just do 'string name;'. for 'char course[C_LEN];' you can change that to just 'string course;', its the same deal. you want a string not an array of characters. strings are much easier to work with, thats why they were created.

    once youve fixed that, if you still have errors or questions, post ALL of the updated code please! good luck

  2. #17
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    changed all that you have said to a string. everything loads and i can put information in and i believe that all the functions are working correctly. the only problem now is that i dont know if it is getting stucture and is then passing it to the function by the caller. then 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.

    this is the new code with the strings added

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    struct myStruct
    {
       string name;
       int age;
       string course;
    };
    
    void myFunc1(myStruct m)    // by value
    {
        	cout << "First Name: ";
            cin >> m.name;    <----------------------------is this needed still?
    }
    
    void myFunc2(myStruct &m)  // by reference
    {
        cout<<"Age: ";
        cin>>m.age;
    }
    
    void myFunc3(myStruct &m)
    {
        cout<<"Course: ";
        cin >>m.course;
    }
    
    int main()
    {
        myStruct m;
        myFunc1(m);
        myFunc2(m);
        myFunc3(m);
        system("pause");
    }
    Last edited by peckitt99; 10-30-2006 at 02:26 PM.

  3. #18
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    void myFunc1(myStruct m)    // by value
    {
        	cout << "First Name: ";
            cin >> m.name;    <----------------------------is this needed still?
    }
    yes it is needed. it says 'read input from standard input and save to the variable m.name.

    then main() should output the contents of the structure.
    well in order for it to print it, you have to tell it to!, heres an example: cout << m.name;

  4. #19
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    ah right i thought that was what this code was for.

    Code:
    int main()
    {
        myStruct m;
        myFunc1(m);
        myFunc2(m);
        myFunc3(m);
        system("pause");
    }

  5. #20
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    is there away to get to output all the data outside of the functions rather than just getting it to output within the function?

  6. #21
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    If you access the struct by its pointer or reference
    then the first function can fill it
    The second - process the data
    And the 3rd - output results

    Code:
        myStruct m;
        myFunc1(&m);
    to call...
    Code:
    void myFunc1(myStruct& m)    // by reference
    {
        cout << "First Name: ";
        cin >> m.name;    
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 2 of 2 FirstFirst 12
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