Print highest

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 26
Reputation: sfurlow2 is an unknown quantity at this point 
Solved Threads: 0
sfurlow2 sfurlow2 is offline Offline
Light Poster

Print highest

 
0
  #1
Feb 26th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Print highest

 
0
  #2
Feb 26th, 2008
suggestion
  1. int main()
  2. {
  3. // put your code here
  4.  
  5. return 0;
  6. }

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).
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 26
Reputation: sfurlow2 is an unknown quantity at this point 
Solved Threads: 0
sfurlow2 sfurlow2 is offline Offline
Light Poster

Re: Print highest

 
0
  #3
Feb 26th, 2008
How would I keep track of the highest number using one variable?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 84
Reputation: evilsilver is an unknown quantity at this point 
Solved Threads: 1
evilsilver's Avatar
evilsilver evilsilver is offline Offline
Junior Poster in Training

Re: Print highest

 
0
  #4
Feb 26th, 2008
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....
Last edited by evilsilver; Feb 26th, 2008 at 6:44 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 26
Reputation: sfurlow2 is an unknown quantity at this point 
Solved Threads: 0
sfurlow2 sfurlow2 is offline Offline
Light Poster

Re: Print highest

 
0
  #5
Feb 26th, 2008
I'm still having problems with this. So far, I have this:
  1. int main()
  2. {
  3. int x;
  4. int highest;
  5. int counter;
  6.  
  7. for (counter=1;counter<=5;counter++)
  8. {
  9. cin >> x;
  10.  
  11. if (highest > x);
  12. cout << highest << endl;
  13. }
  14.  
  15. cin.ignore(INT_MAX);
  16.  
  17.  
  18. getchar();
  19. return 0;
  20. }
I'm still not sure what to do.
Last edited by Ancient Dragon; Feb 26th, 2008 at 6:57 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Print highest

 
0
  #6
Feb 26th, 2008
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 [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.
Last edited by Ancient Dragon; Feb 26th, 2008 at 7:02 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 84
Reputation: evilsilver is an unknown quantity at this point 
Solved Threads: 1
evilsilver's Avatar
evilsilver evilsilver is offline Offline
Junior Poster in Training

Re: Print highest

 
0
  #7
Feb 26th, 2008
ok, should more look something like this:
  1. #include <iostream> //needed for cout and cin
  2.  
  3. using namespace std; //also needed...
  4.  
  5. int main(){
  6.  
  7. int counter = 0;
  8. int highest = 0;
  9. int input = 0;
  10.  
  11. for(counter = 1; counter <= 5; counter++){
  12. //input the number from the user.
  13. cin >> input;
  14.  
  15. //if the number they input is higher than what we already have,
  16. //store that number instead
  17. if(input > highest)
  18. highest = input;
  19. }//end the fore loop
  20.  
  21. cout << highest << endl;
  22.  
  23. return 0;
  24. }
Last edited by evilsilver; Feb 26th, 2008 at 8:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Print highest

 
0
  #8
Feb 26th, 2008
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 84
Reputation: evilsilver is an unknown quantity at this point 
Solved Threads: 1
evilsilver's Avatar
evilsilver evilsilver is offline Offline
Junior Poster in Training

Re: Print highest

 
0
  #9
Feb 26th, 2008
(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....)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Print highest

 
0
  #10
Feb 26th, 2008
Originally Posted by evilsilver View Post
(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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC