View Single Post
Join Date: Nov 2006
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

 
0
  #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
Reply With Quote