Looping Practice

I have a program the I have compiled in Dev C++ and VC++ sucessfully. I want to add some more parameters to it. I'm a beginner with C++. What I need to do is cause the current working program I got to keep going until the user decides to quit. In other words, after the average is obtained, the user can then enter the grades for another student. Can anyone give me some pointers? Any help would be greatly appreciated. My code is below:




#include <iostream>
using namespace std;

int main()
{
cout << "Input the number of student grades: ";
int grades;
cin >> grades;
double num;
double sum;
sum = 0.0;
for (int x = 1; x <= grades; x++)
{
cout << "#" << x << " = ";
cin >> num;
sum += num;
}
cout << "Grades = " << grades << "\nSum = " << sum << "\nAverage = " << sum / grades << "\n";


system("PAUSE");
return 0;
try to use the #format when uploading code

The easiest way is this:

1
2
3
4
5
6
7
8
int goOn=1;
while (!goOn==0)
{
...                    //do whatever you want to repeat

cout<<"Go on (0 to quit, 1 to go on)?";
cin>>goOn;
} 


you could use a bool-variable to, wich is maybe better, but it makes the code longer and more complicated
Last edited on
@Scipio: Inputing a number without any validating is asking for trouble. If the user accidentally enters a letter your code goes into an infinite loop.

A better approach is to use a string:
1
2
3
4
5
6
7
8
9
10
11
12
int main() {
  string sCont = "y";
  while ( (sCont == "y") || (sCont == "Y") )
  {

  cout<<"Continue? (Y/N)";
  cin>>sCont;
  }
  cout << "Finished" << endl;

  return 0;
}
I know. I normally use this function to get an integer:

1
2
3
4
5
6
7
8
9
10
11
12
int getint(int max)
{
  int input;
  while (input<1||input>max)
  {
  char temp[100];
  std::cin.getline(temp, 100);
  input=strtol(temp,0,10);
  if (input<1||input>max)
  std::cout<<"Not an option!\n";
  }
}


But i didnt want to confuse benjacl with that stuff :)

@benjacl
Zaita's code is a good and simple solution. Use that one
Last edited on
What if 0 is a valid input? Or a negative number? :P
I write another piece of code :)

This code comes from a game in wich i use a menu to let the user choose what he wants to do, eg:

[1] start new game
[2] load game
[3] end

After this output i call the function with getint(3). 0 as input would give mistakes while running the program
Last edited on
@Zaita

You where right :)
I needed the function where 0 was a valid input. I changed the function into this:

1
2
3
4
5
6
7
8
9
10
11
12
int getint(int min, int max)
{
  int input;
  while (input<min||input>max)
  {
  char temp[100];
  std::cin.getline(temp, 100);
  input=strtol(temp,0,10);
  if (input<min||input>max)
  std::cout<<"Not an option!\n";
  }
}


And simply call it using two parameters
Problem with that is if the input is not a valid number. It'll still return 0, which would be interpreted as valid.
:)

You're a great help, you just avoided a lot of bugs in my code

I think i found the final answer :D. Would this work correctly?

1
2
3
4
5
6
7
8
9
10
11
12
int getint(int min,int max)
{
  int input;
  char temp[100];
  while (input<min||input>max||(input==0 && temp[0]!='0'))
  {
  std::cin.getline(temp, 100);
  input=strtol(temp,0,10);
  if (input<min||input>max||(input==0 && temp[0]!='0'))
  std::cout<<"Not an option!\n";
  }
}
That example may work with when using a "while" loop. But for this code I am trying to find a solution using the "for" loop. Thanks for the help so far.
1
2
3
4
5
cout << "how many grades do you wanna enter?";
string sGrades = "";
cin >> sGrades;
int numberofGrades = atoi(sGrades.c_str());
// etc 
Topic archived. No new replies allowed.