finding the largest number. HELP!

I am trying to hone my skills a little here (or what little skills I have obtained so far :P) and I found a problem that asks you to prompt the user to input how many numbers they want to enter and then find the largest of those numbers. I want to try and test myself a bit but I cannot for the life of me determine what the best course of action would be. Would I use a while or for loop? Any tips would be greatly appreciated as I am brand new to this. Thanks.

If you know in advance how many times you want to run a loop, you can use a for loop. So if the user tells you they want to enter x numbers, you can run the for loop until the counter reaches x.

If you didn't know in advance how many loop iterations would need to be done, you could use a while loop. The while would check for a condition to be met to exit the loop.
I think I have a start here but I don't quite know how to store the numbers into a variable and how to compare them using a function (I think I am using that term right but feel free to correct me). I have attached the start to my code below with a note as to where I am stuck.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
    int a;
    int x;

    cout << " This program will take a set amount of numbers and determine which is the largest. Enter how many numbers you would like to compare. " << endl;
   cin >> a;
cout << "Please enter your first number" << endl;
   for(int x=1; x<=a;x++){
        cout << "Please enter your next number." << endl;
   cin >> *Here is where my problems begin, I don't know what to use as a variable and how to continue to enter numbers until the user input amount for a is reached*
   }
   return 0;

}
 
I would do something like this:
Ask the user to input a number
Set that number as the current maximum
for i = 1 to however many numbers the user wants to enter - 1
    Have the user input a number
    If that number is bigger than your current maximum number
        Set that number as the current maximum

So you would need four variables: one representing how many numbers the user wants to enter (a in your code), one for the loop counter (x in your code), one for the user's input, and one for the current maximum number.

By the way, don't be afraid to give your variables more descriptive names -- it won't slow your program (or the compiler) down, don't worry. :)
(Although single letter variables are okay for loop counters, like x in your example -- though i is more often used than x for that purpose, but whatever.)
Thank you for that, it helped me when i relabeled things with more descriptive titles. I have an error somewhere in here that I cannot find. It allows me to enter in numbers correctly but when it computes which is the largest it does not give me what I need. Any advice with this chunk of code?

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
#include <iostream>

using namespace std;

int main()
{
    int amountofnumbers;
    int loopcounter;
    int inputnumber;
    int maximum;

    cout << " This program will take a set amount of numbers and determine which is the largest. Enter how many numbers you would like to compare. " << endl;

   cin >> amountofnumbers;


   for(int loopcounter=1; loopcounter <= amountofnumbers; loopcounter++)
    {
        cout << "Please enter a number." << endl;
   cin >> inputnumber;
   if (inputnumber > maximum)
    {
    maximum = inputnumber;
   }

    }
   cout <<"The largest number input was" << maximum<< endl;
   return 0;

}
Since you're comparing inputnumber to maximum, you'll want to initialize maximum to 0 so that there's a valid value in there the first time you check. Uninitialized variables have unknown garbage values in them, so you can have unexpected results when you try to use them.

You don't need to declare the loopcounter at the top since you're declaring it in the for loop itself.
wildblue to the rescue! Thank you very much once again :). I think I am better understanding the application of = vs. == now. A little weird at first but then again my teacher doesn't exactly explain much in layman's terms. I got all the bugs worked out and am happily experimenting with other examples to hone my skills in. A BIG thank you to all who contributed to this thread. I am sure I will be returning here with some more tips very soon. Cannot say thank you enough to all that helped me out.
Technically, you would want to initialize maximum to the smallest value possible, since otherwise, if you enter all negative numbers, your program will think 0 is the maximum:
9
10
11
int maximum = -99999999; // Eh, close enough...
// If you want to be fancy, #include <limits> and use this instead:
// int maximum = numeric_limits<int>::min(); 

Another way is to pull the first input out of the loop and assign that to maximum:
14
15
16
17
18
19
20
21
22
23
cin >> amountofnumbers;

cout << "Please enter a number: ";
cin >> inputnumber;
maximum = inputnumber;
for (int i = 2; i <= amountofnumbers; ++i) // Starting at 2 since we already have our first number
{ // Also using 'i' instead of 'loopcounter' since I don't feel like typing all of that :)
    cout << "Please enter a number: ";
    cin >> inputnumber;
    // ... 

Or you could check for i == 1 in the loop instead:
14
15
16
17
18
19
20
21
22
23
24
cin >> amountofnumbers;


for (int i = 1; i <= amountofnumbers; ++i)
{
    cout << "Please enter a number: ";
    cin >> inputnumber;
    if (i == 1)
        maximum = inputnumber;
    else if (inputnumber > maximum)
        // ... 
Technically, you would want to initialize maximum to the smallest value possible, since otherwise, if you enter all negative numbers, your program will think 0 is the maximum:


Good point - I was assuming positive numbers.
Topic archived. No new replies allowed.