943,724 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2140
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 26th, 2008
0

Print highest

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
sfurlow2 is offline Offline
33 posts
since Feb 2008
Feb 26th, 2008
0

Re: Print highest

suggestion
C++ Syntax (Toggle Plain Text)
  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).
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Feb 26th, 2008
0

Re: Print highest

How would I keep track of the highest number using one variable?
Reputation Points: 10
Solved Threads: 0
Light Poster
sfurlow2 is offline Offline
33 posts
since Feb 2008
Feb 26th, 2008
0

Re: Print highest

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
evilsilver is offline Offline
84 posts
since Feb 2005
Feb 26th, 2008
0

Re: Print highest

I'm still having problems with this. So far, I have this:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
sfurlow2 is offline Offline
33 posts
since Feb 2008
Feb 26th, 2008
0

Re: Print highest

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Feb 26th, 2008
0

Re: Print highest

ok, should more look something like this:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
evilsilver is offline Offline
84 posts
since Feb 2005
Feb 26th, 2008
0

Re: Print highest

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Feb 26th, 2008
0

Re: Print highest

(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....)
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
evilsilver is offline Offline
84 posts
since Feb 2005
Feb 26th, 2008
0

Re: Print highest

Click to Expand / Collapse  Quote originally posted by evilsilver ...
(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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ looping program
Next Thread in C++ Forum Timeline: Class Template used for stack





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC