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?

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).

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

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....

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.

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 <iostream> at the top of your program and add the using namespace std; statement or add std:: 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.

Edited 3 Years Ago by diafol: fixed formatting

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

>>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.

(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....)

(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.

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

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.

This question has already been answered. Start a new discussion instead.