Code for an averaging program

So I am trying to write code for a program that is supposed to average up a data set that is given. The user input should be the size of the data set then the actual data set itself. However, they can only choose between 1 - 10. If it is 0, the program should quit, and if it is below 0 or above 10, the program will just repeat back to the beginning. So what I need to know is if I put a while loop that says if it = 0 to quit and put the for loop with the calculations inside that. Then do a do/while loop for it saying if it is between 1 and 10 that it can do the calculations and then any other number will repeat the question. I am confused as to how to set it up.

Last edited on
Is the user only allowed to enter data sets with 1-10 values? If that is so, I'd consider doing something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int dataPoints;
bool validSet = false;

while( validSet == false )
   {
      cout << "Enter the number of data points in the data set (must be between 1 and 10): ";
      cin >> dataPoints;
      if( !(dataPoints <= 0 || dataPoints > 10) )
         {
            validSet = true; //This exits the loop
         }
   }

//Code to take in all the data points, could be a for loop that loops dataPoints number of times

//Code to average the data points, and then cout the result


I think this satisfies the criteria you mentioned: the loop will continuously ask for the number of dataPoints until it falls into the valid range. You could (and maybe also should) add a way for the user to exit the program inside that loop. This is because unless the user enters a number 1-10, the program will absolutely never end.
How about this ?
1
2
3
4
5
6
7
8
9
int size;
bool quit=false;
while(!quit){
  cout << "Enter how many number in the data set (must be between 1 and 10): ";
  cin>>size;
  if(!dataPoints)quit=true;
  if(dataPoints<0|| dataPoints>10)continue;
  // your program
}
Topic archived. No new replies allowed.