for loop not letting me input variable data

This is a discussion on for loop not letting me input variable data within the C++ Programming forums, part of the General Programming Boards category; I have a for loop set up to name the elements of an array. I think I am using it ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    for loop not letting me input variable data

    I have a for loop set up to name the elements of an array. I think I am using it wrong. Here is my code.
    Code:
    /* Declared variables outside of for loop */
    string Heroes[9000];
    int h_cost[9000];
    string h_name;
    int h_min;
    int num_heroes;
    
    
    /*For loop to set up names and cost of heroes*/
    for (int h = 1; h <= num_heroes; ++h){
                              cout <<"What unit for Hero" <<h <<"?" <<endl;
                              string h_name = Heroes[h];
                              cin>> h_name;
                              cout <<"How many civs to buy this hero?" <<endl;
                              cin>> h_cost[h];
                              if (Heroes[h] == "Minerals"){
                                            cout <<"How many minerals awarded?" <<endl;
                                            cin>> h_min;
                                            }
                              }
    In essence I want the program to loop for as many heroes I have. The ouput should be similar to this....
    Code:
    What unit for Hero1?
    Heroes name // <---value input by user
    How many civs to buy this hero?
    Hero cost    // <---value input by user
    What unit for Hero2?
    Heroes name // <---value input by user
    How many civs to buy this hero?
    Hero cost // <---value input by user
    Instead I get this....

    Code:
    What unit for Hero1?
    Heroes name // <---value input by user
    What unit for Hero2?
    How many civs to buy this hero?
    What unit for Hero3?
    How many civs to buy this hero?
    What unit for Hero3?
    How many civs to buy this hero?
    What unit for Hero4?
    The problem is that its only letting me input the name for hero1 and just displays the rest of the questions without letting me input what I want them to be. Why isn't it letting me input values for my other heroes and costs?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Do not know what to tell you, I compiled your code and it let me input the values for every "Hero".

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Does the name of the first hero have a space in it? When using operator>> you can only read in a single word.

  4. #4
    1479
    Join Date
    Aug 2003
    Posts
    253
    Oh, ok thats my problem. How would I allow input to be able to use spaces without messing up the program?
    Knowledge is power and I want it all

    -0RealityFusion0-

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    You would have to use getline(cin, h_name); to get the name.

    Using getline leads to another issue since you will want to use operator>> to read in the h_cost int value. The operator>> leaves a newline in the stream from when the user hits enter after typing the answer. The getline function reads that newline and stops and so the second time through your loop the name will show up empty.

    A simple solution is to call cin.ignore() after every time you call cin >> to read in something. This will ignore the newline. Do not call ignore() after calls to getline, since getline() discards the newline on its own.

  7. #7
    1479
    Join Date
    Aug 2003
    Posts
    253
    Quote Originally Posted by Daved
    You would have to use getline(cin, h_name); to get the name.

    Using getline leads to another issue since you will want to use operator>> to read in the h_cost int value. The operator>> leaves a newline in the stream from when the user hits enter after typing the answer. The getline function reads that newline and stops and so the second time through your loop the name will show up empty.

    A simple solution is to call cin.ignore() after every time you call cin >> to read in something. This will ignore the newline. Do not call ignore() after calls to getline, since getline() discards the newline on its own.
    Thanks, but doing it that way created another problem. Using this code:
    Code:
    for (int h = 1; h <= num_heroes; ++h){
                              cout <<"What unit for Hero" <<h <<"?" <<endl;
                              string h_name = Heroes[h];
                              getline(cin, h_name);
                              
                              cout <<"How many civs to buy this hero?" <<endl;
                              //int num_civs = h_cost[h];
                              cin >> h_cost[h];
                              cin.ignore();
                              if (Heroes[h] == "Minerals"){
                                            cout <<"How many minerals awarded?" <<endl;
                                            cin>> h_min;
                                            }
                              }
    This does not allow me to input value for hero1.

    EDIT: Nevermind, I got it. I had to enter in another cin.ignore(); after the >>operator I used before I initialised the loop. Thanks Daved!!
    Last edited by RealityFusion; 09-21-2005 at 04:33 PM.
    Knowledge is power and I want it all

    -0RealityFusion0-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array substitution in composite data variable
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 08-12-2008, 04:29 AM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 12:50 AM
  4. Data missing in variable
    By nizbit in forum C Programming
    Replies: 8
    Last Post: 03-07-2005, 07:22 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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