RSS Forums RSS

finding the smallest number

Please support our C++ advertiser: Programming Forums
Reply
Posts: 3
Reputation: mcweezle is an unknown quantity at this point 
Solved Threads: 0
mcweezle's Avatar
mcweezle mcweezle is offline Offline
Newbie Poster

finding the smallest number

  #1  
Nov 12th, 2006
So I have been plugging away at this program. I had to create a menu and have 3 sections. Section A asked for a limit, then the user enters that many numbers and it tells them which number was the largest. DONE that.
Section C quits the program.. that works too.

Section B however... supposed to let the user enter as many numbers as they want, inputing -99 when they are done. After this it tells them what the smallest number was. I've tried all sorts of things, but I either get a 0 answer or -99. What am I missing? I'm limited to loops or if / else statements.

HELP!

  1.  
  2. // Week 5 - Control Structures II- Philip McCrary - 11-08-06
  3.  
  4. #include<iostream>
  5.  
  6. using namespace std;
  7.  
  8. char menuItem;
  9. int number;
  10. int limit, counter;
  11. int temp1, temp2;
  12. const int SENTINEL = -99;
  13.  
  14. int main()
  15. { //open main
  16.  
  17. while (menuItem != 'C' || 'c')
  18. { // open while 1
  19. cout << "Welcome to CS106 Week 5: Control Structures II." << endl;
  20. cout << "Please choose from the following options below." << endl;
  21. cout << "A: What's the largest number?" << endl;
  22. cout << "B: What's the smallest number?" << endl;
  23. cout << "C: Flee to Windows" << endl;
  24. cout << "Please choose now: ";
  25. cin >> menuItem;
  26. cout << endl;
  27.  
  28.  
  29. if ((menuItem >= 'A' || 'a') && (menuItem <= 'C' || 'c'))
  30. { // open if main
  31. switch (menuItem)
  32. { // open switch 1
  33. case 'a':
  34. case 'A':
  35.  
  36. cout << "This section will find the largest number? " << endl;
  37. cout << "How many numbers do you want to enter? ";
  38. cin >> limit;
  39. cout << endl;
  40. cout << "Enter " << limit << " numbers: " << endl;
  41.  
  42.  
  43. for (counter = 0; counter < limit; counter++)
  44. {
  45. cin >> number;
  46. if (number > temp1)
  47. temp1 = number;
  48. }
  49.  
  50. cout << "\nYour largest number out of " << limit << " was " << temp1 << "." << endl;
  51. cout << endl;
  52.  
  53. break;
  54.  
  55. case 'b':
  56. case 'B':
  57.  
  58. cout << " Enter a group of numbers. I will tell you the smallest number. \n Enter " << SENTINEL << " to exit." << endl;
  59. counter = 0;
  60.  
  61.  
  62. while (number != SENTINEL)
  63. { // open while 3
  64. counter++;
  65. cin >> number;
  66.  
  67. {
  68. cin >> number;
  69. if (number < temp2)
  70. temp2 = number;
  71. }
  72. }
  73.  
  74. cout << "Your smallest number out of " << counter << " was " << temp2 << "." << endl;
  75. cout << endl;
  76. break;
  77. case 'c':
  78. case 'C':
  79. cout << " Goodbye. " << endl;
  80. return 0;
  81. } //close switch 1
  82. } // close if main
  83. else
  84. {
  85. cout << "Please enter a valid menu option; A, B, or C." << endl;
  86. cin >> menuItem;
  87. cout << endl;
  88. } // end else
  89.  
  90. } // close while 1
  91. return 0;
  92. } // close main
  93.  
  94.  
AddThis Social Bookmark Button
Reply With Quote  
Posts: 4,810
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 324
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: finding the smallest number

  #2  
Nov 12th, 2006
You can't do this:
  1. if ((menuItem >= 'A' || 'a') && (menuItem <= 'C' || 'c'))
(Well you can, but the results aren't going to be what you expect.)
You have to write out each expression:
  1. if ((menuItem >= 'A' || menuItem <= 'a') && (menuItem <= 'C' || menuItem <= 'c'))


  1. while (number != SENTINEL)
  2. { // open while 3
  3. counter++;
  4. cin >> number;
  5.  
  6. { // why do you have these braces
  7. cin >> number; // you already did a cin above ^_^
  8. if (number < temp2) // temp2 has never been initalized in this section!
  9. // how I would do it is set temp2 to -99, and then do the following if:
  10. // if (number < temp2 || temp2 == -99) { ...
  11. temp2 = number;
  12. } // you really don't need them
  13. }
Also, the way you're doing that loop, -99 is registered as the "lowest number" because it only bales out after it's done the comparison with temp2.

Not unnormal (you don't really need to worry about this), but when you input especially large numbers, it throws the program into an endless loop that has to be manually terminated.

[edit]By the way, in the menu loop, you need to somewhere reset your variables, because otherwise if you try to run section B twice, for example, you'll find that it doesn't allow input -- it just jumps right to the final output.[/edit]
Last edited by John A : Nov 12th, 2006 at 10:20 pm.
Come join the official DaniWeb IRC community chat
Reply With Quote  
Posts: 140
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 2
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Re: finding the smallest number

  #3  
Nov 12th, 2006
try something like this. because you are limited to while/if loops its kinda of primitive but it should work.
int i=0;
temp=0;
cin>>Nitems>>endl;
while(i<(number of items)){
    cin>>number>>endl;
    if(number>temp)
        number = max;
i++;
}

putting that into a function and make it return max should get you what you want.
Last edited by Barefootsanders : Nov 12th, 2006 at 10:23 pm.
Reply With Quote  
Posts: 338
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 2
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: finding the smallest number

  #4  
Nov 12th, 2006
Your error lies in here, you should make sure the number you are reading IS NOT the sentinel AND smaller than the current smallest number before assigning it to temp2 (use an if then statement). Second of all is why are you performing cin >> number two times in a row?

while (number != SENTINEL)
{ // open while 3
     counter++;
     cin >> number;  
     {
          cin >> number;
          if (number < temp2)
          temp2 = number;
     }
}
Reply With Quote  
Posts: 338
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 2
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: finding the smallest number

  #5  
Nov 12th, 2006
Originally Posted by joeprogrammer View Post
You can't do this:
  1. if ((menuItem >= 'A' || 'a') && (menuItem <= 'C' || 'c'))
....

actually many compilers (including mine) will let you do it, even though it is considered bad form.
Reply With Quote  
Posts: 3
Reputation: mcweezle is an unknown quantity at this point 
Solved Threads: 0
mcweezle's Avatar
mcweezle mcweezle is offline Offline
Newbie Poster

Re: finding the smallest number

  #6  
Nov 12th, 2006
Okay.... i tried this, but now i get a value of whatever the last number entered was. So, if my input is 1, 4, 8, -99... it ignores the -99, which is what I want, but it says the smallest number would be 8. .... what am I missing?

  1.  
  2. case 'b':
  3. case 'B':
  4.  
  5. cout << " Enter a group of numbers. I will tell you the smallest number. \n Enter " << SENTINEL << " to exit." << endl;
  6. counter = 0;
  7.  
  8.  
  9. while (number != SENTINEL)
  10. { // open while 3
  11. counter++;
  12. cin >> number;
  13.  
  14. if (number > SENTINEL && temp2 != SENTINEL)
  15. temp2 = number;
  16.  
  17. }
  18.  
  19. cout << "Your smallest number out of " << counter << " was " << temp2 << "." << endl;
  20. cout << endl;
  21.  
Reply With Quote  
Posts: 4,810
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 324
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: finding the smallest number

  #7  
Nov 12th, 2006
You still haven't initalized temp2.

By the way, here's what I think it should look like:
  1. temp2 = -99;
  2.  
  3.  
  4. while (number != SENTINEL)
  5. { // open while 3
  6. counter++;
  7. cin >> number;
  8.  
  9. if (number == -99) {
  10. break;
  11. }
  12. if (number < temp2 || temp2 == -99)
  13. temp2 = number;
  14. }
Probably not the best way of doing it, but it works.
Come join the official DaniWeb IRC community chat
Reply With Quote  
Posts: 4,810
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 324
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: finding the smallest number

  #8  
Nov 12th, 2006
Originally Posted by paradox814 View Post
actually many compilers (including mine) will let you do it, even though it is considered bad form.
I've never heard of a compiler that will insert expressions in like that. Maybe I'm just ignorant. But I certainly wouldn't want that, because although a statement like this:
if (myvar == 4 || 5) { ...
might translate into:
if (myvar == 4 || myvar == 5) { ...

I certainly don't want
if (myvar == 4 || myothervar) {...
to turn into
if (myvar == 4 || myvar == myothervar) {...

Because who's to say that I didn't want to see if myvar is equal to 4 or myothervar is a nonzero value?
Come join the official DaniWeb IRC community chat
Reply With Quote  
Posts: 3
Reputation: mcweezle is an unknown quantity at this point 
Solved Threads: 0
mcweezle's Avatar
mcweezle mcweezle is offline Offline
Newbie Poster

Re: finding the smallest number

  #9  
Nov 13th, 2006
Originally Posted by joeprogrammer View Post
You still haven't initalized temp2.

By the way, here's what I think it should look like:
  1. temp2 = -99;
  2.  
  3.  
  4. while (number != SENTINEL)
  5. { // open while 3
  6. counter++;
  7. cin >> number;
  8.  
  9. if (number == -99) {
  10. break;
  11. }
  12. if (number < temp2 || temp2 == -99)
  13. temp2 = number;
  14. }
Probably not the best way of doing it, but it works.



Hey! that works! I had something very similar to that at one time, but I had temp2 != -99.... I turned your code into this and it failed. Turned it back and it worked... that one little ! symbol. geez!

Thanks guys! I appreciate the assistance!
Last edited by mcweezle : Nov 13th, 2006 at 12:25 am. Reason: giving thanks
Reply With Quote  
Posts: 3,026
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 267
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: finding the smallest number

  #10  
Nov 13th, 2006
Jeez, lot's of bad ideas here....

Read in all the numbers into an array (ary[]) first, do NOT store the -99. If you enter 5, 8, 6, 3, -99, your count should be 4, not 5.

Then set lowval to your first element, ary[0] because it could be the lowest. Then loop from 1 to count for the test.
Sometimes, when I look at my children, I say to myself, "Lillian, you should have remained a virgin."
-- Lillian Carter (mother of Jimmy Carter)
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 2784 | Replies: 9 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:05 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC