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!

// 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

Recommended Answers

All 9 Replies

You can't do this:

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:

if ((menuItem >= 'A' || menuItem <= 'a') && (menuItem <= 'C' || menuItem <= 'c'))
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
             }

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]

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.

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;
     }
}

You can't do this:

if ((menuItem >= 'A' || 'a') && (menuItem <= 'C' || 'c'))

....

actually many compilers (including mine) will let you do it, even though it is considered bad form.

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?

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 temp2 .

By the way, here's what I think it should look like:

temp2 = -99;
                
                
            while (number != SENTINEL)
            {               // open while 3
                counter++;
                cin >> number;

                if (number == -99) {
                    break;
                }
                if (number < temp2 || temp2 == -99)
                    temp2 = number;
             }

Probably not the best way of doing it, but it works.

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?

You still haven't initalized temp2 .

By the way, here's what I think it should look like:

temp2 = -99;
                
                
            while (number != SENTINEL)
            {               // open while 3
                counter++;
                cin >> number;

                if (number == -99) {
                    break;
                }
                if (number < temp2 || temp2 == -99)
                    temp2 = number;
             }

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!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.