Ending a loop help

Hey, I'm in my second semester of college. In my c++ class we were given 7 things to do for the midterm, one was:
"5. Ask the user how many numbers he or she wants to input and then find the largest of these numbers."

We already had to create a program that picked out the smallest number, so I used that and messed with it a little. I'm just having problem with inputting the amount of numbers. I assume I'd just end it with a loop but don't know how to go about it because we haven't had anything like this the whole first half... surprising, I know.

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;
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;
}

closed account (28poGNh0)
This tow expressions have no sence

1
2
cout << "Please enter a number. To end, enter 0.\n";
cin >> number;


and Since you write large = number;
the block
1
2
3
4
if (number > large )
{
      large = number;
}

never gonna get checked

also dont use system("anything");like Duoas said

finaly I suggest this 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
# include <iostream>
# include <limits.h>
using namespace std;

int main()
{
    int tot = 0;
    int iMax = 0;
    int iMin = 9999;
    int number = 0;

    cout << "How many numbers will you be entering?\n";
    cin >> tot;

    for(int i=0;i<tot;i++)
    {
        cout << "Enter your number -> " ;cin >> number;
        iMax = max(iMax,number);
        iMin = min(iMin,number);
    }cout << endl;

    cout << "The max is : " << iMax << endl;
    cout << "The min is : " << iMin << endl;

    cin.ignore(INT_MAX);
}


enjoy
@Techno01
Output:
How many numbers will you be entering?
3
Enter your number -> -5
Enter your number -> -7
Enter your number -> -20

The max is : 0
The min is : -20

Notice the maximum value entered is -5, but the program incorrectly reports 0 as the maximum.

There are a couple of solutions to this problem. One way is to copy the very first value as the maximum and the compare the subsequent values to that. That's what anthonyxmarinaro correctly did.

An alternative is to initialise the maximum to the smallest possible value.
1
2
    int iMax =  std::numeric_limits<int>::min();
    int iMin =  std::numeric_limits<int>::max();

or
1
2
    int iMax =  INT_MIN;
    int iMin =  INT_MAX;

(depending on whether the C or C++ version of the limits header file is used.
closed account (28poGNh0)
stupid and cairless

thanks a lot Chervil
Last edited on
I've done the same thing myself, don't worry. If the range of the input is known (e.g. score in an exam) it might not matter.
closed account (28poGNh0)
I want to change my code abouve but this fault could me help someone and this how Chervil want It ,or at less I hope

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
# include <iostream>
# include <limits.h>
# include <limits>// for numeric_limits
using namespace std;

int main()
{
    int tot = 0;
    /*int iMax = INT_MIN;
    int iMin = INT_MAX;*/// This is the first way that Chervil suggest 
    
    // This is the second way
    int iMax = numeric_limits<int>::min();
    int iMin = numeric_limits<int>::max();
    
    //My third way is to build functions to do max and min for us
    
    int number = 0;

    cout << "How many numbers will you be entering?\n";
    cin >> tot;

    for(int i=0;i<tot;i++)
    {
        cout << "Enter your number -> " ;cin >> number;
        iMax = max(iMax,number);
        iMin = min(iMin,number);
    }cout << endl;

    cout << "The max is : " << iMax << endl;
    cout << "The min is : " << iMin << endl;

    cin.ignore(INT_MAX);
}


Hope you Chervil to explain more
" One way is to copy the very first value as the maximum and the compare the subsequent values to that. That's what anthonyxmarinaro correctly did."
with exemple if you may
"One way is to copy the very first value as the maximum and the compare the subsequent values to that."

Example:
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
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int rnd(int from, int to);

int main()
{
    srand(time(0));
    
    int max; 
    int min;
    bool firsttime = true;
    
    for (int i=0; i<20; i++)
    {
        int number = rnd(-100, +100);
        
        cout << setw(4) << number << endl;
        
        if (firsttime)
        {
            firsttime = false;
            max = number;
            min = number;
        }
        else
        {
            if (number > max)
                max = number;
            if (number < min)
                min = number;
        }
    }
    
    cout << "Min: " << min << "    Max: " << max <<  endl;
    cin.get();
}

int rnd(int from, int to)
{
    int range = to - from + 1;
    return rand() % range + from;    
}


Alternatively, rather than doing it this way, the max/min can often be set to the first value before the loop begins.
Last edited on
closed account (28poGNh0)
Thanks a lot @Chervil hope we see all your best
I really appreciate the replies, I'd like some feedback though so I know what NOT to do next time.
Also what does the "cin.ignore(INT_MAX);" mean?
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.
Last edited on
Topic archived. No new replies allowed.