Q:

Nested loops in C++ and user input

Pretty new here to programming, and I have an assignment where I need to achieve the following: 

ask for total amount of people
get each of their names
allow user to enter up to 5 scores for each person
if there are less than 5 scores for a given person, inputting -100 will stop it

So far I have written this:
#include <iostream>

using namespace std;

int main() {

    string personName;
    int totalPerson, personScoreCounter;
    double personGrade, personGradeTotal;

    cout << "Input total amount of people: ";
    cin >> totalPerson;

   for (int person = 1; person <= totalPerson; person++)
   {
       cout << "Input name for person " << person << ": ";
       getline(cin, personName);
       cin.ignore();

       while ( (personGrade != -100) && (personScoreCounter <= 5) )
       {
           cout << "Input up to 5 scores for " << personName << " (-100 to end): ";
           cin >> personGrade;

           if (personGrade >= 0 && personGrade <= 100) // valid range of scores
           {
               personGradeTotal += personGrade;
               personScoreCounter++;
           }
           else
           {
               cout << "Input only scores from 0-100" << endl;
           }

           cout << "Input up to 5 scores for " << personName << " (-100 to end): ";
           cin >> personGrade;
       }
}

    // calculate averages and other stuff in here.

    return 0;
}

After getting their name, only the last cout inside the while loop seems to execute first, then it starts from the top and so on until the for loop hits the end depending on totalPerson. I know I'm missing a few things in here, probably in the order of operations and also the way I am executing my loops, but I just can't see it. Could any of you guys with experience in the language please give me any pointers as to what's happening here and how I can fix it? Thank you.

A:

Inside your while group, you only want to use your cout line once (at the beginning looks good).
Your first check should be for ==-100 or similar, since as it is now, you'll get a "Input only scores from 0 to 100" message if you enter -100.
You should keep a cin.ignore(); call after each use of cin >> VARIABLE, since then you will drop the EoL character.
Example code:
#include <iostream>

using namespace std;

int main() {
    int totalPerson;

    cout << "Input total number of people: ";
    cin >> totalPerson;
    cin.ignore();

   for (int person = 1; person <= totalPerson; person++)
   {
       int personScoreCounter=0;
       double personGrade = -1, personGradeTotal=0;
       string personName;

       cout << "Input name for person " << person << ": ";
       std::getline(cin, personName);

       while ( (personGrade != -100) && (personScoreCounter < 5) )
       {
           cout << "Input up to 5 scores for " << personName << " (-100 to end): ";
           cin >> personGrade;
           cin.ignore();

           if (personGrade == -100) {
               break;
           } else if (personGrade >= 0 && personGrade <= 100) {
               personGradeTotal += personGrade;
               personScoreCounter++;
           } else {
               cout << "Input only scores from 0-100" << endl;
           }
       }

    // calculate averages and other stuff in here.
    double avg = personGradeTotal / personScoreCounter;
    cout << "Avg = " << avg << endl;
   }
    return 0;
}

Some of your variables also needed to move inside the for loop.
Additionally I changed the limits on the personScoreCounter to [0:4] rather than [1:5] - this way you can use it for averaging more easily.

