Thread: C++ Array Help

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    9

    C++ Array Help

    I am writing a program that prompts the user for the name and age of a group of people. The group's size is unknown initially, so the program must keep asking the user if they want to enter more data. It has been a while since I have worked with C++ so I am trying to figure out how to take the user inputs of name and ages into an array until the user wants to stop imputing information.

    I know this is incorrect but maybe you guys could help steer me in the right direction.

    Code:
    char name[15];
    int age[3];
    char answer;
    int i;
    
    while(answer=='y'||'Y')
    {
    cout << "Enter name: ";
    cin >> name[i];
    cout << "Age: ";
    cin >> age[i];
             
    cout << "Would you like to add another person? (Y/N)";
    cin >> answer;
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Data Structures
    vector - C++ Reference
    Combine these two.

    You have to initialize "answer" to 'y' or 'Y' before entering the loop.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    Every time I try to run this it asks me for the name and once I hit enter It keeps running. It does not let me enter a age or ask if I want to run this program again.


    Code:
    int main()
    {
    
    
    char name[15];
    int age[3];
    char answer = 'y'||'Y';
    int i = 0;
    
    while(answer=='y'||'Y')
    {
    
    cout << "Enter name: ";
    cin >> name[i];
    cout <<endl;
    
    cout << "Enter Age: ";
    cin >> age[i];
    cout <<endl;
    
    cout << "Would you like to add another person? (Y/N)";
    cin >> answer;
    cout <<endl;
    
    }



    I found a piece of code using vectors that works but I do not know how to implement it so It will prompt a user to enter a name and age until the user is done entering data.

    Code:
    const int NUM_PERSONS = 5;
    
        vector<string> names(NUM_PERSONS);
        vector<int> ages(NUM_PERSONS);
    
        for (int i = 0; i < names.size(); i++)
        {
            cout << "Enter name: ";
            getline (cin, names[i]);
    
            cout << "Age: ";
            cin >> ages[i];
            cin.get();  
    	}

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    char answer = 'y'||'Y';
    HUH??? While this compiles, it's really the same as saying
    Code:
    char answer = 'y';
    So change that.

    Code:
    while(answer=='y'||'Y')
    You can't do that. Each comparison must be separate.
    Code:
    while(answer == 'y'|| answer == 'Y')
    Shouldn't you be making sure you don't receive more entries that you have space for?

    You also are leaving a newline in the input buffer. Add a
    Code:
    cin.ignore()
    after you "add another" question gets answered.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You do this:
    Code:
    std::wstring Name;
    std::vector<std::wstring> Names;
    std::getline(std::wcin, Name);
    Names.push_back(Name);
    // Repeat lines 3 to 4
    Last edited by Elysia; 02-27-2011 at 04:30 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by rags_to_riches View Post
    Code:
    char answer = 'y'||'Y';
    HUH??? While this compiles, it's really the same as saying
    Code:
    char answer = 'y';
    Actually it is the same as:
    Code:
    char answer = true;

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    Quote Originally Posted by Elysia View Post
    You do this:
    Code:
    std::wstring Name;
    std::vector<std::wstring> Names;
    std::getline(std::wcin, Name);
    Names.push_back(Name);
    // Repeat lines 1 to 2
    Could you explain to me what is happening here? I am confused

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    What are you confused about? Rather than forcing us to write complete tutorials that you probably won't read but once, will you explain what you do understand? It's nice to know if you are even trying.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by kmdv View Post
    Actually it is the same as:
    Code:
    char answer = true;
    Oooo...good point!

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    OK here is what I have so far and as of right now it works. Except I have one question.
    Code:
    string names[15];
    int ages[3];
    char answer = 'y';
    
    
    while(answer == 'y'|| answer == 'Y')
    {
    	 for (int i = 0; i < 3; i++)
        {
    	cout << "Enter name: ";
    	cin >> names[i];
        cout <<endl;
    
    
    	cout << "Enter Age: ";
        cin >> ages[i];
        cout <<endl;
    
    
    	cout << "Would you like to add another person? (Y/N)";
    	cin >> answer;
    	cout <<endl;
    
    	
    	 }
    }
    	 for(int x = 0; x < 3; x++)
      {
        cout << "Name: " << names[x] << "   Age: ";
        cout << ages[x] << endl;
      }
    Code:
     for (int i = 0; i < 3; i++)
    I just set this to 3 so I could run the program but I want to know how I would do this if I didn't have a set number of Names and Ages? So when I run this program 1 time and input 10 names and ages, and the next time i only want to input 3 or 4? I tried names.size but it did not work.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, so first I showed you an example of how to do it properly. If you don't understand it, ask a specific question about what you don't understand.
    As for the second question, ask yourself a logical question. If you were to take a list of names, how would you know when to stop? How would a computer know when to stop?
    Formulate the logic, make a flowchart, translate to code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    Quote Originally Posted by Elysia View Post
    You do this:
    Code:
    std::wstring Name;
    std::vector<std::wstring> Names;
    std::getline(std::wcin, Name);
    Names.push_back(Name);
    // Repeat lines 3 to 4
    @Whiteflags - This is what I was confused about. I have never worked with anything like this before so I do not know what it means. I am not trying to have you do this for me. I am simply asking for help to guide me in the right direction which everyone has been doing so far.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The only reason you would need a counting loop like
    Code:
    for (int i = 0; i < 3; i++)
    is because you are using arrays which have a fixed size.

    You later posted vector code, which is good because it gets more memory on its own if it needs it. But, incorrectly in this case, you used it so that you accessed elements like this:
    Code:
            cout << "Enter name: ";
            getline (cin, names[i]);
    
            cout << "Age: ";
            cin >> ages[i];
            cin.get();
    Not much better than arrays. Once you go over the NUM_PERSONS amount, you'll have the same errors arrays would get you.

    The vector class has a push_back( ) method. Read the actual input into appropriate variables first, then
    Code:
    names.push_back( some_name );
    ages.push_back( some_age );
    That's all you have to do to get the array to extend itself. Now you just keep looping until the user says no, and you have a couple vectors filled with everything the end user typed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM