Like Tree1Likes
  • 1 Post By dennis.cpp

C++ Novice needs help

This is a discussion on C++ Novice needs help within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I've run into a small problem on this program I was creating. It's supposed to be a simple ...

  1. #1
    Registered User Black_Epiphany's Avatar
    Join Date
    Sep 2011
    Posts
    15

    C++ Novice needs help

    Hey guys, I've run into a small problem on this program I was creating. It's supposed to be a simple database using arrays to store names in strings and ages in a separate array. Unfortunately, when I try to have the program echo back all the inputted data, it fails and just inputs the first name and age entered. Take a look:

    Code:
    //This is a program that allows you to create a simple database and output it all to a text file
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    
    using namespace std;
    
    
    
    
    int main() {
        
        string name[10];
        int age[10];
        int input;
        int i; //counting
        int a;//counting
        a = 0;
        i = 0;
    
    
        mainmenu:
    
    
        cout << "Database Manager" << endl;
        cout << "1: New Database" << endl;
        cout << "2: Load Database" << endl;
        cout << "3: View Database" << endl;
        cout << "4: Edit Database" << endl;
        cin >> input;
    
    
        switch (input) {
        case 1:
            while (i < 10) {
            cout << endl;
            cout << "Enter the info for each person. Limit of 10 people."<< endl << "Type 'back' to return to the main menu"<< endl; //back function not implemented yet
            cout << "Input Name: " << endl;
                cin >> name[i];
            cout << "Input Age: "<< endl;
                cin >> age[i];
                i++;
            }
            break;
        
        case 3:
            
            for (a=0; a<10; a++) {//tried different kinds of loops, can't get it to print the arrays correctly
            cout << name[a] << endl << age[a] << endl << endl; //should print out every name and age
            }
            break;
        }
    
    
        goto mainmenu;
    }
    Thanks in advance for any help

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    3,268
    You should stop using the goto statement and use a while loop. A goto should be reserved for special occasions. A simple while loop would be a good replacement in this program.

    Now as to your actual problem, the iostream extraction operator>> will stop processing when it encounters a white space character. So if you want your names to have spaces you should use getline() instead of the extraction operator.

    Jim

  3. #3
    Registered User Black_Epiphany's Avatar
    Join Date
    Sep 2011
    Posts
    15
    Ok I changed it so that it uses getline() for the strings (and switched goto to a while loop using a bool variable) , but it's the output that doesn't seem to be working. It just outputs the first name and age.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Black_Epiphany's Avatar
    Join Date
    Sep 2011
    Posts
    15
    Code:
    //This is a program that allows you to create a simple database and output it all to a text file
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    
    using namespace std;
    
    
    
    
    int main() {
        
        string name[10];
        int age[10];
        int input;
        int i; //counting
        int a;//counting
        a = 0;
        i = 0;
        bool done;
        done = false;
    
    
    while(!done) {
    
    
        cout << "Database Manager" << endl;
        cout << "1: New Database" << endl;
        cout << "2: Load Database" << endl;
        cout << "3: View Database" << endl;
        cout << "4: Edit Database" << endl;
        cin >> input;
    
    
        switch (input) {
        case 1:
            while (i < 10) {
            cout << endl;
            cout << "Enter the info for each person. Limit of 10 people."<< endl << "Type 'back' to return to the main menu"<< endl; //back function not implemented yet
            cout << "Input Name: " << endl;
                getline(cin,name[i]);
            cout << "Input Age: "<< endl;
                cin >> age[i];
                i++;
            }
            break;
        
        case 3:
            
        while (a<10){
            cout << name[a] << endl << age[a] << endl << endl; //should print out every name and age
            }
            break;
        }
    }
    }

  6. #6
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    199
    You don't change a within your loop.

    Btw, it's good practice to define your variables in the place you actually use them rather than at the beginning of main().
    iMalc likes this.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: openSUSE 13.2 / KDE SC 4.14.6
    Compiler: gcc 4.8-7.1.2 with KDevelop 4.7.1-4.1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Novice help
    By genesis531501 in forum C++ Programming
    Replies: 3
    Last Post: 08-21-2011, 01:55 PM
  2. novice help
    By vernontan in forum C Programming
    Replies: 2
    Last Post: 01-19-2010, 09:22 AM
  3. C++ Novice
    By AaronHall in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2008, 05:48 PM
  4. Replies: 10
    Last Post: 06-17-2005, 10:00 PM
  5. Novice needs help
    By donniebb in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2003, 04:50 PM

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