Returning highest value

Hello. When I was given several options about which extra class to pick I picked programming. However our teacher "hit the wall" and was given a vaccation so now we don't have any teacher in the course. We are reading after a book which gave us this problem: Write a program which loops until someone hits 0, and when it is done return the highest inputted value.

Now I dont want you to solve it, just give a hint how we might solve it, because none of us have a clue.

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

using namespace std;

int main()
{
    for( ; ; ){
        int temp=0;
    cout<<"Enter a number: "; cin>>temp;
    cout<<temp<<endl;
    
    if (temp!=0) continue;
    break;

    }
    return 0;
}


The only thing i've figured out so far..
 
for( ; ; )


you can (should) write
 
while(true)
here instead.

You should #include <vector>

Read here for details:
http://cplusplus.com/reference/stl/vector/

reply if you still have problems

@hanst99: He doesn't need the other values, so there is little point in using a vector.

@shooninjo: These are the steps; you've already done some of them:
-Keep a global value 'max'.
-Read in the input into a temp variable. [Done!]
-Check if temp > 0. Quit if not. [Done!]
-Check if temp > max. If not, do nothing. If so, give 'max' the value of 'temp'.

closed account (zb0S216C)
What you could do is create a separate variable that stores the highest given value. When zero is given, the loop ends and the highest value is printed. Here's a example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main( )
{
    // integer highest_value( 0 );
    // integer user_input( 0 );

    while( /* the value of user_input is not equal to zero */ )
    {
        // Get the users input and put it into user_input.
        
        // If the value within highest_value is less than the value 
        // within user_input, give highest_value the value of user_input.
    }

    // Print the value of highest_value to the screen.
    
    return 0;
}


Wazzak
Firstly, both "break" and "continue" end a cycle. That is, whatever temp is, line 14 will not be executed.

Now, the idea is to have a variable "max" in main() that holds the maximum. When temp is greater than "max", set "max = temp".
Note, that if the initial value of "max" is greater than all inputs, this won't work. If you set "max = 0", negative input's won't work (I don't know if that matters to you). You could set "max = std::numeric_limits<int>::min()" or simply "max = -2147483648" so that no value is lower than initial "max". A nicer solution would be to ask for a number before the loop starts and assign that first input to "max".

By the way, if you do name your variable "max" and keep that "using namespace std;", your variable may be confused with function std::max. Give it another name then..
He doesn't need the other values, so there is little point in using a vector.


I personally think first collecting the values and then calculating the max would be better.
Aight. So now my code looks like this:

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

using namespace std;

int main()
{
    for( ; ; ){
        int temp=0, max=0;
    cout<<"Enter a number: "; cin>>temp;
    cout<<temp<<endl;
    if (temp>max)
    temp=max;


    cout<<"write a new number: "; cin>>temp;
    if (temp!=0) continue;
    break;
    cout<<max;



    }
    return 0;
}


My though was that if temp was bigger than max, max would be equal to temp. Storing the temp value in max. However when I break the loop no max value comes out. Why?
Cause you first break and then cout max in the loop.
Oh I only saw the first 2 answers since I did not update the page. OKay so if I want to print the "max" variable before I break the loop, where would it be? In between if (temp!=0) continue; and break; or?
Last edited on
Firstly I saw something. When I moved cout<<max; so that it was before the loop broke the value was still zero.
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>

using namespace std;

int main()
{
    for( ; ; ){
        int temp=0, max=0;
    cout<<"Enter a number: "; cin>>temp;
    if (temp>max) temp=max;



    cout<<"write a new number: "; cin>>temp;

    cout<<max;


    if (temp!=0) continue;
    break;



    }
    return 0;
}


In this one max=0. Even if i input a number, for example. I put in 8 in temp. Temp is now biggen than max, since max is 0. So therefore I want temp to be equal to max. Therefore the temp=max. Why does this not work?
if (temp>max) temp=max;

I guess that will be max = temp
Look where you put line 8. It's in the loop. "max" will be reset to 0 on every cycle and no information will be kept there. You should put that line before the loop so that "max" is initialized only once.
Similarly, consider where line 16 is supposed to be (before loop, in the loop or after the loop). Think of what the value of "max" will be in each case.
Topic archived. No new replies allowed.