Loop problem(noob)

I don't exactly know what I'm doing wrong here. Could someone take a look at this function I have written and explain why when my program arrives at the cout portion where it displays the scores does it continue to run through the entire array. I thought I had limited the number of indexes to 'q' but apparently I have not done that as the program reads out 10 total scores regardless of whether or not I have assigned them information. This leads to bizarre things being output to the console.

Thank you for any help you can offer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int scores()
{
    cout << "Please enter up to 10 scores(non-numeric input to quit)\n";
    int golfscores[10];
    int total = 0;
    int q = 0;
        do
            {
                (cin >> golfscores[q]).get();
                ++q;
            } while (q < 10 ); // I am aware this doens't check for non-numeric input validly but I am so 
//darn tired of this exercise I have re-written this code countless numbers of times and can't be 
//arsed to fix this check right now.

    for (int i = 0; i < q; i++)
        {
            cout << golfscores[i] << " ";
            total += golfscores[i];
        }

    return total;
}
Last edited on
If you enter a string where an integer was needed, cin.fail() will return true. Note that you'll have to do cin.clear() and possibly cin.ignore(big_number, '\n') before any other input.
What is happening is that your (cin >> golfscores[ q ] ).get(); is blowing off because when you type in non-numeric data, cin gets hosed. It doesn't ever break out of your while loop so it loops until q == 10!

Put an output statement and display the value of "q" in your loop.

You should test cin (if( cin.good() for example).
closed account (D80DSL3A)
That do loop won't end until q = 10. If you are entering 6 values (for example) do you just hit "enter" 4 extra times to get to the program output?
Oh, I see...
non-numeric input to quit
. This would cause cin to fail. The remaining values of golfscores would not be assigned, but q would still get incremented to 10. Not a good method for terminating input!

You could make the input stop by entering some "sentinal value" to break from the loop.
For example, if golf scores should not be negative then set up the loop to break if a negative value is entered.
1
2
3
4
5
6
do
{
      (cin >> golfscores[q]).get();
      if( golfscores[q] < 0 )break;// break BEFORE incrementing q
       ++q;
} while (q < 10 );


Another idea is to have the user give how many scores will be entered then loop to that value instead of to 10.
1
2
3
4
5
6
7
8
int numScores;
cout << "How many scores will you be entering (max=10)?"; cin >> numScores;
if( numScores >10 )numScores = 10;// enforce the maximum value
do
{
       (cin >> golfscores[q]).get();
       ++q;
} while (q < numScores );

Other methods are possible as well. Do these ideas help?
To all 3 of you thank you for the replies, I am going to try the "break" method you suggest fun2code & also the cin.clear() deal

I will report backw ith my results.

The reason I am not asking for user input of how many numbers to keep in the array is because the exercise states not to do so.

My revised code is now functioning. Here is the snippet that I changed for anyone curious eyes. Topic is now being marked as resolved.

1
2
3
4
5
6
7
8
9
10
   do
            {
                (cin >> golfscores[q]).get();
                if(cin.fail())
                    {
                        break;
                    }
                else
                ++q;
            } while (q < 10 );
Last edited on
Way to go! Way to stay with it! You will do fine!
Topic archived. No new replies allowed.