| | |
finding the smallest number
![]() |
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!
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!
c Syntax (Toggle Plain Text)
// Week 5 - Control Structures II- Philip McCrary - 11-08-06 #include<iostream> using namespace std; char menuItem; int number; int limit, counter; int temp1, temp2; const int SENTINEL = -99; int main() { //open main while (menuItem != 'C' || 'c') { // open while 1 cout << "Welcome to CS106 Week 5: Control Structures II." << endl; cout << "Please choose from the following options below." << endl; cout << "A: What's the largest number?" << endl; cout << "B: What's the smallest number?" << endl; cout << "C: Flee to Windows" << endl; cout << "Please choose now: "; cin >> menuItem; cout << endl; if ((menuItem >= 'A' || 'a') && (menuItem <= 'C' || 'c')) { // open if main switch (menuItem) { // open switch 1 case 'a': case 'A': cout << "This section will find the largest number? " << endl; cout << "How many numbers do you want to enter? "; cin >> limit; cout << endl; cout << "Enter " << limit << " numbers: " << endl; for (counter = 0; counter < limit; counter++) { cin >> number; if (number > temp1) temp1 = number; } cout << "\nYour largest number out of " << limit << " was " << temp1 << "." << endl; cout << endl; break; case 'b': case 'B': cout << " Enter a group of numbers. I will tell you the smallest number. \n Enter " << SENTINEL << " to exit." << endl; counter = 0; while (number != SENTINEL) { // open while 3 counter++; cin >> number; { cin >> number; if (number < temp2) temp2 = number; } } cout << "Your smallest number out of " << counter << " was " << temp2 << "." << endl; cout << endl; break; case 'c': case 'C': cout << " Goodbye. " << endl; return 0; } //close switch 1 } // close if main else { cout << "Please enter a valid menu option; A, B, or C." << endl; cin >> menuItem; cout << endl; } // end else } // close while 1 return 0; } // close main
You can't do this:
(Well you can, but the results aren't going to be what you expect.)
You have to write out each expression:
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
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]
c Syntax (Toggle Plain Text)
if ((menuItem >= 'A' || 'a') && (menuItem <= 'C' || 'c'))
You have to write out each expression:
c Syntax (Toggle Plain Text)
if ((menuItem >= 'A' || menuItem <= 'a') && (menuItem <= 'C' || menuItem <= 'c'))
c Syntax (Toggle Plain Text)
while (number != SENTINEL) { // open while 3 counter++; cin >> number; { // why do you have these braces cin >> number; // you already did a cin above ^_^ if (number < temp2) // temp2 has never been initalized in this section! // how I would do it is set temp2 to -99, and then do the following if: // if (number < temp2 || temp2 == -99) { ... temp2 = number; } // you really don't need them }
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.
"Technological progress is like an axe in the hands of a pathological criminal."
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
try something like this. because you are limited to while/if loops its kinda of primitive but it should work.
putting that into a function and make it return max should get you what you want.
C++ Syntax (Toggle Plain Text)
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 11:23 pm.
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)
while (number != SENTINEL) { // open while 3 counter++; cin >> number; { cin >> number; if (number < temp2) temp2 = number; } }
•
•
•
•
You can't do this:
....c Syntax (Toggle Plain Text)
if ((menuItem >= 'A' || 'a') && (menuItem <= 'C' || 'c'))
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?
c Syntax (Toggle Plain Text)
case 'b': case 'B': cout << " Enter a group of numbers. I will tell you the smallest number. \n Enter " << SENTINEL << " to exit." << endl; counter = 0; while (number != SENTINEL) { // open while 3 counter++; cin >> number; if (number > SENTINEL && temp2 != SENTINEL) temp2 = number; } cout << "Your smallest number out of " << counter << " was " << temp2 << "." << endl; cout << endl;
You still haven't initalized
By the way, here's what I think it should look like:
Probably not the best way of doing it, but it works.
temp2.By the way, here's what I think it should look like:
c Syntax (Toggle Plain Text)
temp2 = -99; while (number != SENTINEL) { // open while 3 counter++; cin >> number; if (number == -99) { break; } if (number < temp2 || temp2 == -99) temp2 = number; }
"Technological progress is like an axe in the hands of a pathological criminal."
•
•
•
•
actually many compilers (including mine) will let you do it, even though it is considered bad form.
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? "Technological progress is like an axe in the hands of a pathological criminal."
•
•
•
•
You still haven't initalizedtemp2.
By the way, here's what I think it should look like:
Probably not the best way of doing it, but it works.c Syntax (Toggle Plain Text)
temp2 = -99; while (number != SENTINEL) { // open while 3 counter++; cin >> number; if (number == -99) { break; } if (number < temp2 || temp2 == -99) temp2 = number; }
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
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.
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Help in finding 1500`th ugly number. (C++)
- to secomd smallest number in array (Java)
- Looking for the SMALLEST number between x1, x2, x3, x4, x5 (C#)
- Second smallest number? (C++)
Other Threads in the C++ Forum
- Previous Thread: Fun with TicTacToe - need help
- Next Thread: Barcode creation in Dev c++....
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






