I just started class about 4 wks ago. I just got my deitel's c++ how to program book and I already cannot figure out how to do this homework assignment... (I got shiested online...getting refund, just had to purchase book late.)

use a while statement to determine and print the largest number of 10 numbers input by the user. The program should use three variables:

counter: A counter to count to 10, (to keep track of how many numbers have been input and to determine when all numbers have been processed.)

number: the current number input to the program

Largest: The largerst number found so far.

Now..from what I have, I don't quite understand how to put the while loops to find the largest # out of the 10 entered....heres what I have after working on this for 3 hrs!!! I got an error message on the while part...on that line. I've got a headache too!

as im reading...i think what i can't find is where to assign the values to the counter (if im saying that right)...

Thanks!
ps...IM NEW I SAID!!! DON'T BE LAUGHIN.....:lol:

#include <iostream>
using namespace std;
int main ()
{

int counter; 	
int larger;
int number; 

if   ( number < 10 )
cin << largest < number;

while 


{
cout << "enter your number: " ; 
cin >> number;
}
//end of while loop

return 0;
}

Dani AI

Generated

A few clarifications and a safe do-while pattern that address the common issues in this thread (uninitialized variables, counter handling and negative inputs). 's reply demonstrates the basic read/compare/update loop; the main things to watch for are initializing largest correctly and incrementing the counter only when a valid number has been read.

A reliable approach is to read the first valid integer, store it in largest, then run a loop for the remaining nine inputs. That avoids the pitfall of initializing largest to 0 (which fails if all inputs are negative). Also ensure the counter reflects how many valid numbers have been processed and use input validation to handle non-numeric input.

Example do-while implementation (reads first number, then loops until 10 valid numbers have been accepted):

#include <iostream>
#include <limits>
using namespace std;

int main() {
    int number, largest, counter = 1;

    cout << "Enter number 1: ";
    while (!(cin >> number)) {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid input. Enter an integer: ";
    }
    largest = number;

    do {
        cout << "Enter number " << (counter + 1) << ": ";
        if (!(cin >> number)) {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Invalid input, try again.\n";
            continue; // do not count bad input
        }
        if (number > largest) largest = number;
        ++counter;
    } while (counter < 10);

    cout << "Largest number: " << largest << '\n';
    return 0;
}

Notes: using the first-input method or numeric_limits<int>::min() for initialization handles negative ranges. Keep variable names consistent (counter, number, largest) and always initialize variables before use — that fixes the undefined-behavior error seen in the original post by .

Recommended Answers

All 2 Replies

See this

#include <iostream>
using namespace std;
int main ()
{

int counter=0;
int largest=0;
int number=0;
largest=number;

while ( counter < 10 )
{
    cin>>number;
    if(largest<number) 
        largest = number;
    ++counter;
}
cout<<"Largest Number:"<<largest;

return 0;
}

thanks so much! You will not believe if I hadda started reading chapter 5 before I asked this question, I probably would had understood it better then...chap. 5 covers this in detail!!! :eek:
I modified it just a bit but it did work. I couldn't figure out what what the if statement was supposed to say and how it worked. when I read yours aloud, It made perfect sense. again, thanks for your help. I also have to now modify it to the " do- while" loop!

I hope one of these days I can help too...(one day...:-| )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.