I bought the textbook C++ How to program 9th edition and I have come across a question that I am just stumped on, even though it is probably pretty simple. The question all summed up is this: "Use a while statement to determine and print the largest number of 10 numbers entered by the user". But here is the part that stumps me. The question wants me to use only 3 variables. counter, number, and largest. I know to make counter variable go up by 1 for each number entered until it gets to 10, and I know the number variable is used for input. I just can't seem to find out how to use the largest variable, or how to check to see what value is the largest without using other variables. This is all I have so far. Right now I put a break in the code so it wouldn't be an infinite loop.
UPDATED CODE
#include <iostream> using namespace std; void main() { int counter = 0; int number = 0; int largest = 0; cout << "Please enter up to 10 numbers and I will print the largest one on the screen.\n\n"; while (counter <= 10) { cout << "Number: "; cin >> number; counter++; if (number > largest) { largest = number; } else if (counter == 10) { cout << "The largest number was: " << number; break; } } }
ifstatements.