I'm back again for some more homework help.

I just want to start off by saying thanks for assisting me in my last assignment. This one, I have to allow it to Enter a name, and enter grades for that name, and then it will display the average for that name. It should then ask for another name, until the user enters end for the name. Sorry for the horrible formatting, I'm working on getting better.

#include <iostream>
#include <string>
using namespace std;

int main () {

int sum = 0;
int smallest = 100;
int ct = 0;
string name;
int x;
int bavg;
int avg;

     cout << "Enter a name: " << endl;
     cin >> name;
     while (name != "end" && name != "END"){ 
           //checks for the end
           cout << "Enter a test score (to stop press -999)" << endl;
           cin >> x;
           if ( x == -999){
              goto name; //first time using this, not sure if this will work
              }                //i think this should go to the string name??
              if (x < 0 || x > 100){ //check
                 cout << "You must enter a new value from 0 to 100: ";
                 cin >> x;
                 } //end of if check
           ct++;      //+1 to # of items entered
           sum += x; //adds the grade entered to the previous amount of sum
               if (x < smallest) { //this checks for the smallest number entered
                  smallest = x;
                  } //end of if smallest
        cout << "Enter another test score: " << endl;
        cin >> x;
        if ( x == -999){
              goto name;
        cout << "The sum of the dataset is: " << sum << endl;
        cout << "The smallest number is: " << smallest << endl;
        cout << "The number of items in the dataset is: " << ct << endl;
             bavg = sum - smallest; //takes the sum and subtracts the smallest
             avg = bavg / ct; //takes the average and divides by the # of items
             //Displays the name and the average
             cout << "The student, " << name << ", had the following average with the"
             << "lowest score being dropped: " << avg << endl;
     //Get's a new name 
     cout << "Enter a name: " << endl;
     cin >> name;  
     return 0;

I think one of my problems is that the numbers stay in memory for those integers. So everytime I enter a new person, it still has the previous persons grades. I think I can fix this by setting the values back to their original value after it ask for a new name at the bottom of the while loop.

The goto statement I put in there, I have yet to test. I was just suggested that I should use this. Could someone explain to me how the goto works?

Other problems I'm having, are, it only allows me to enter one value for each name. What do you people suggest I do to get it to input more values until the user decides that is enough, and then it outputs the average and sum and such, then ask for a new name and repeats the process?

Thanks for any help

>>The goto statement I put in there, I have yet to test. I
Don't bother to test it. Just delete it. I know some teachers that will give students an F for turning in a program that contains one or more goto statements. The goto statement is almost never needed.

What you need are a couple loops, something like this pseudo code.

do while name is not "done"
     prompt for name
     get user input
     // now enter all the grades
     do while grade is greater than or equal to 0
          prmpt for grade
          get user input
          calculate average grade
     end while statement
end while statement
This article has been dead for over six months. Start a new discussion instead.