Hello I am writing a program that allows the user to input multiple sets of numbers and it shows the count, total, and average of the numbers then displays them. However I am trying to put a if statement in it so that if the user's total = more than the max value it returns a invalid comment and goes back to main to restart the loop. here is what I have.
#include <iostream>
using namespace std;
int main()
{
int startingValue = 0;
const int sentinelValue = -1;
int maxNumber = 2147483647;
cout << " This program displays the count, total, and average of the numbers you have\n entered between 0 and 2,147,483,647\n\n";
cout << " To signify the end of your number input, enter a -1.\n\n";
cout << " Enter your starting value: ";
cin >> startingValue;
cout << endl << endl << endl;
int counter = 0;
int accumulated = 0;
int userInput = startingValue;
cout << " You have entered: " << userInput << endl;
if (accumulated > maxNumber)
{
break;
cout << " Your total is invalid, please input new numbers.";
main();
}
else
cout << "\n Enter another number: ";
cin >> userInput;
}
cout << "\n You have chosen "
<< counter << " numbers ";
cout << "with a total of " << accumulated << endl;
cout << " Your average is " << accumulated / counter << endl << endl;
cout << "\n End of Program\n\n";
Please put that in code tags.
First of all, you can streamline counter = counter + 1 to counter++. You can also streamline the immediately following line to accumulated += userInput.
You should avoid calling main like that. It is a bad idea. If you want to go around your program again put a loop around it. Don't call main. The only thing that's really supposed to call main is the OS, when you open your prog. Nothing else. (If you are bent on that workaround I believe there is an article on main calling itself, but it is purely an INTELLECTUAL exercise, not intended to actually be used, because you shouldn't be calling main anyways.)
The reason your if isn't working, I think, is because the break statement exits the loop immediately, jumping to the lines immediately following loop scope, and thereby skipping the rest of the code in your if.
#include <iostream>
usingnamespace std;
int main()
{
int startingValue = 0;
constint sentinelValue = -1;
int maxNumber = 2147483647;
cout << " This program displays the count, total, and average of the numbers you have\n entered between 0 and 2,147,483,647\n\n";
cout << " To signify the end of your number input, enter a -1.\n\n";
cout << " Enter your starting value: ";
cin >> startingValue;
cout << endl << endl << endl;
int counter = 0;
int accumulated = 0;
int userInput = startingValue;
cout << " You have entered: " << userInput << endl;
while (userInput > sentinelValue)
{
counter = counter + 1;
accumulated = accumulated + userInput;
if (accumulated > maxNumber)
{
break;
cout << " Your total is invalid, please input new numbers.";
main();
}
else
cout << "\n Enter another number: ";
cin >> userInput;
}
cout << "\n You have chosen "
<< counter << " numbers ";
cout << "with a total of " << accumulated << endl;
cout << " Your average is " << accumulated / counter << endl << endl;
cout << "\n End of Program\n\n";
return 0;
}