954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Print highest

For my homework assignment, I'm supposed to get five numbers from a user and print the highest one using a loop. I'm not sure what to do. Any suggestions?

sfurlow2
Light Poster
33 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

suggestion

int main()
{
   // put your code here

   return 0;
}


Ok, so lets assume you know how to do the above. You need a for/next loop that counts from 0 to 5, display a prompt, get input using cin, then keep track of the highest number entered (you need another variable to do that).

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

How would I keep track of the highest number using one variable?

sfurlow2
Light Poster
33 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

by having a variable ( i.e. int highest; ) and using an if statement after the cin to check if it is higher or lower than the one they just input, then keep the higher of the two stored there....

evilsilver
Junior Poster in Training
84 posts since Feb 2005
Reputation Points: 10
Solved Threads: 1
 

I'm still having problems with this. So far, I have this:

int main()
{
   int x;
   int highest;
   int counter;
   
   for (counter=1;counter<=5;counter++)
   {
       cin >> x;
       
       if (highest > x);
       cout << highest << endl;
   }
       
       cin.ignore(INT_MAX);
   

    getchar();
	return 0;
}

I'm still not sure what to do.

sfurlow2
Light Poster
33 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

All you did on line 12 is display the value of highest. You never set it to anything. Initialize the value of highest to 0 before the loop starts, then when the if condition is true set highest = x

you also need to incude at the top of your program and add the using namespace std; statement or add[icode]std::[/b] before each cin statement. I use the using statement because it doesn't require as much typing, but many programmers don't like that. Unless your teacher says otherwise its your call which way to do it.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

ok, should more look something like this:

#include <iostream> //needed for cout and cin

using namespace std; //also needed...

int main(){

    int counter = 0;
    int highest = 0;
    int input = 0;

    for(counter = 1; counter <= 5; counter++){
        //input the number from the user.
         cin >> input;

        //if the number they input is higher than what we already have, 
        //store that number instead
         if(input > highest)
              highest = input;
    }//end the fore loop

    cout << highest << endl;

    return 0;
}
evilsilver
Junior Poster in Training
84 posts since Feb 2005
Reputation Points: 10
Solved Threads: 1
 

>>ok, should more look something like this:
Did you compile and run that if there are no compile errors or warnings? That's the easiest way to find out if your code is correct or not.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

(ummm.... i wasn't the one having the problem with the code, i know for a fact that the code i posted works... i was posting it so sfurlow2 could see how to do it, else i wouldn't have commented it i would just have put a code snipet for all to see and copy... sorry just sayin....)

evilsilver
Junior Poster in Training
84 posts since Feb 2005
Reputation Points: 10
Solved Threads: 1
 
(ummm.... i wasn't the one having the problem with the code, i know for a fact that the code i posted works... i was posting it so sfurlow2 could see how to do it, else i wouldn't have commented it i would just have put a code snipet for all to see and copy... sorry just sayin....)

My apologies -- I misunderstood. I thought you were asking if it worked.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Hey, thanks for the help.

sfurlow2
Light Poster
33 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

If I wanted to do the same thing, only print the highest and lowest value, what would I have to do differently?

sfurlow2
Light Poster
33 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 
If I wanted to do the same thing, only print the highest and lowest value, what would I have to do differently?

Same logic. Adding code to find the lowest doesn't change anything you've done already. See my comments in the code.

#include <iostream> //needed for cout and cin

using namespace std; //also needed...

int main(){

    int counter = 0;
    int highest = 0;
    int input = 0;
    int lowest;         // add this line

    for(counter = 1; counter <= 5; counter++){
        //input the number from the user.
         cin >> input;

        //if the number they input is higher than what we already have, 
        //store that number instead
         if(input > highest)
              highest = input;

         if (input < lowest)     // add this line
              lowest = input;    // add this line

    }//end the fore loop

    cout << highest << endl;

    // add line to print out lowest

    return 0;
}


One more thing. I would initialize both highest and lowest to the value of the variable "input" for the first number read in. If you initialize either highest or lowest to 0, what if all numbers are higher or all numbers are lower than 0? You'll get bad results. It's safer to initialize highest and lowest to the first number.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Thanks. I kept initializing int lowest at zero.

sfurlow2
Light Poster
33 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You