943,568 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3509
  • C++ RSS
Nov 12th, 2006
0

finding the smallest number

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcweezle is offline Offline
3 posts
since Nov 2006
Nov 12th, 2006
0

Re: finding the smallest number

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 11:20 pm.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Nov 12th, 2006
0

Re: finding the smallest number

try something like this. because you are limited to while/if loops its kinda of primitive but it should work.
C++ Syntax (Toggle Plain Text)
  1. int i=0;
  2. temp=0;
  3. cin>>Nitems>>endl;
  4. while(i<(number of items)){
  5. cin>>number>>endl;
  6. if(number>temp)
  7. number = max;
  8. i++;
  9. }

putting that into a function and make it return max should get you what you want.
Last edited by Barefootsanders; Nov 12th, 2006 at 11:23 pm.
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Nov 12th, 2006
0

Re: finding the smallest number

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?

C++ Syntax (Toggle Plain Text)
  1. while (number != SENTINEL)
  2. { // open while 3
  3. counter++;
  4. cin >> number;
  5. {
  6. cin >> number;
  7. if (number < temp2)
  8. temp2 = number;
  9. }
  10. }
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Nov 12th, 2006
0

Re: finding the smallest number

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.
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Nov 13th, 2006
0

Re: finding the smallest number

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;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcweezle is offline Offline
3 posts
since Nov 2006
Nov 13th, 2006
0

Re: finding the smallest number

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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Nov 13th, 2006
0

Re: finding the smallest number

Click to Expand / Collapse  Quote originally posted by paradox814 ...
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?
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Nov 13th, 2006
0

Re: finding the smallest number

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 1:25 am. Reason: giving thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcweezle is offline Offline
3 posts
since Nov 2006
Nov 13th, 2006
0

Re: finding the smallest number

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.
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is online now Online
7,716 posts
since May 2006

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: What did I do wrong?
Next Thread in C++ Forum Timeline: Barcode creation in Dev c++....





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


Follow us on Twitter


© 2011 DaniWeb® LLC