Thread: Storing Information then Displaying

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

    Storing Information then Displaying

    i have a simple program that asks the user for thier name, last name, and age then asks them if they want to either save the file or add new information (loops back to the begining and asks the same things).

    what i cant do is store the information of each time they want to add more information and display it all in the end and then be abel to save it all at the end and re-open it (i have the file saving and opening code complete so not a problem with that).

    for example
    Code:
    enter name: bob
    last name: thornton
    age:21
    
    would you like to add more (y/n) y
    
    enter name: tim
    last name : peckitt
    age: 21
    
    would you like to add more (y/n) y
    
    enter name: sue 
    last name:  brigde
    age: 33
    
    would you like to add more (y/n) n
    (part i cant do) display all:

    Code:
    bob
    thronton
    21
    
    tim
    peckitt
    21
    
    sue
    bridge
    33

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    use a vector<> and push_back() what they input.

    Alternatively, you could ask what the max number of users would be, and use new to allocate memory for that.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You'd need an array or some sort of way of holding more than one set of variables. A vector comes to mind.
    Code:
    struct data_t {
        int age;
        string name;
    };
    
    //...
    
    vector<data_t> info;
    
    while(whatever) {
        data_t data;
        // read stuff from user into data...
    
        info.push_back(data);
    }
    
    for(int x = 0; x < info.size(); x ++) {
        // print info[x]
        cout << "Age: " << info[x].age << endl;
        //...
    }
    [edit] Typing long examples means twomers beats me to it . . . [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    struct data_t {
        int age;
        string Firstname;
        string Lastname;
    };
    would work better.

  5. #5
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    if it helps this is the code i am using

    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    void saveinformation(char order);
    
    void other()
    {
        char FirstName[30], LastName[30];
        int Age;
        char FileName[20];
        cout << "Enter the name of the file you want to open: ";
        cin >> FileName;
        ifstream Students(FileName);
        Students >> FirstName >> LastName >> Age;
    
    	cout << "\nFirst Name: " << FirstName;
        cout << "\nLast Name:  " << LastName;
        cout << "\nEnter Age:  " << Age;
    
        cout << "\n\n";
        system("pause");
    }
    
    int main()
    {
    
        char FirstName[30], LastName[30];
        int Age;
        char FileName[20];
        
        cout << "Enter First Name: ";
        cin >> FirstName;
        cout << "Enter Last Name:  ";
        cin >> LastName;
        cout << "Enter Age:        ";
        cin >> Age;
        
    bool saveinformation(char order);
    {   
    char order;
    char moreinfo; 
    
        cout << "Would you like to save this information";
        cin >> order;
        
            if (order == 'n' || order == 'N')
                    {
                       cout << "would you like to add more information (Y/N)";
                       cin >> moreinfo;
                                          
                                          if (moreinfo == 'n' || moreinfo == 'N')
                                             {
                                                return false;
                                             }   
                                          
                                          if (moreinfo == 'y' || moreinfo =='Y')
                                             {
                                               main();
                                              }                              
                    }
                    
                       if (order == 'y' ||order == 'Y')   
                           {
                           
                           cout << "\nEnter the name of the file you want to create: ";
                               cin >> FileName;
                                   ofstream Students(FileName, ios::out);
                                       Students << FirstName << "\n" << LastName << "\n" << Age;
                                           system ("pause");
                                               other();
                                               
                           }
    }
    }

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    use std::string's instead of char whatever[]'s! They're much more robust, functional and ... erm good.

  7. #7
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    how do you use them and implement them in?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    #include <string> firstly. You make an instance by doing

    Code:
    std::string MyString;
    You can initalise on creation:

    Code:
    std::string myOtherString = "Strings are gtreat";
    instead of playing with strcat(), strlen() strcpy() etc, std::strings play in 5:4

    Code:
    cout<< myOtherString.size()
    outputs the size of the string.

    The + and = operators are overloaded for strings, so you can do the following -
    Code:
    std::string one = "one two three ";
    std::string two = "four take five";
    std::string three = one + two;
    instead of the afforementioned char array functions. That's the baics. I recently mentioned this to someone else, I'll find the link and edit this to incorporate it later.

    EDIT: found it - http://cboard.cprogramming.com/showt...td%3A%3Astring they mention here some places where you should use char arrays, or rather, where you shouldn't use std::string

    note, if you throw in a - using namespace std; you don't need the std::
    Last edited by twomers; 09-04-2006 at 02:23 PM.

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

    Vectors

    im using some vectors but i cant seem to get it to output the name.

    any ideas?

    Code:
     
      int main()
      {
          char name;
                cout <<("enter first name");
                          cin >> name;
    
         std::vector< char > v1(name); // copy of name
    
         std::ostream_iterator< char > output( cout, " " );
     
         cout << "Vector v1 contains: ";
         std::copy( v1.begin(), v1.end(), output );
         
         cout <<endl <<endl;
    system("pause");
    }

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    name is a single character in that program. You want a string of characters. You should use string instead of vector. If you are just trying to learn vectors, and are trying to simulate an array of characters, then you'd want to start with the array of characters, or use push_back to add one character at a time to the vector. Learning with vector<int> might be easier, though.

  12. #12
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    std::string Name;
    std::getline( cin, Name ); // Is Getline std::? I think it is, but I can't remember
    std::cout<< '\n' << Name;
    to manipulate this into a vector -

    Code:
    std::vector<char> vecName;
    for ( int i=0; i<Name.size(); i++ )
    {
      vecName.push_back( Name[i] );
    }
    // Then shuffle through the vecName to display. 
    EDIT - for that for loop, you may want to change the parameters to -

    Code:
    for( std::string::size_type i; i<Name.size(); i++ )
    Last edited by twomers; 09-05-2006 at 11:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  2. Displaying Process Information From task_struct?
    By smoothdogg00 in forum C Programming
    Replies: 2
    Last Post: 12-19-2006, 08:05 PM
  3. Problems Displaying the Information
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 10-14-2005, 12:50 AM
  4. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  5. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM