Apologies, we did get a bit away from the original question.
I'm listing the original program here, I merely adjusted the layout (indentation), I think it makes the structure clearer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
using namespace std;
int main ()
{
int number;
int large;
int tot;
bool isValid = true;
{
cout << "How many numbers will you be entering?\n";
cin >> tot;
}
cout << "Please enter a number. To end, enter 0.\n";
cin >> number;
large = number;
while (number != 0)
{
if (number > large )
{
large = number;
}
}
cout << "The largest number you entered was " << large << endl;
isValid = false;
system ("PAUSE");
return 0;
}
|
A few comments, some more important than others.
Line 9 and 33,
bool isValid
- this variable doesn't seem to serve any purpose at present.
Lines 11 and 14, open and closing braces not needed.
The
while
loop at line 21 has a couple of problems. One is that we want to allow the user to enter a specific number of numbers, which will involve some sort of counter (missing). Also missing, inside the loop there is no user input (cin) so the loop never terminates, as there is no way to change any variable.
One way to control the loop is to use the variable
tot
. This contains the count of how many numbers will be entered, we can just keep subtracting 1 from this value until it reaches zero, and make that the condition for ending the loop.
At line 35, there is
system("pause");
, the use of system() is discouraged for many reasons, there is a whole thread discussing that topic, at the top of this forum.
Here's a modified version of the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
using namespace std;
int main ()
{
int number;
int large;
int tot;
cout << "How many numbers will you be entering?\n";
cin >> tot;
cout << "Please enter " << tot << " numbers\n";
cin >> number;
large = number;
while (--tot > 0)
{
cin >> number;
if (number > large)
{
large = number;
}
}
cout << "The largest number you entered was " << large << endl;
cin.ignore(1000, '\n');
cin.get();
return 0;
}
|
Notice lines 31 and 32, this is one of many alternatives to the use of system("pause");
After the earlier
cin>> number;
there is still a newline character remaining in the input buffer. cin.ignore is used as a way of ignoring in this case, up to 1000 characters, or until the newline is encountered.
Hope this helps.