I am in a beginning C++ class and there is an issue I've been having with one particular switch statement (case B, in my case). When I put in an unknown group of numbers, the output spits out the first number I entered..which isn't the smallest number. I intentionally enter my smallest number in the middle for my user input just to see when I change my logic, the correct smallest number would be spit out. However, I'm not having much luck with the logic to do it correctly.
I will post the whole code, but I'm only interested in "case B" logic. I can't seem to get any direction on what could be wrong.(The code tags really overdue the indention, I've noticed).
#include <iostream>
usingnamespace std;
char menuChoice;
int count = 0;
int number;
int groupNbrs;
int temp_A = -9999999;
int temp_B = 9999999;
int limit;
int main()
{
cout <<"This program will determine the largest or smallest number. " <<endl;
cout <<"out of the group of numbers that the user chooses." <<endl;
cout <<"\n";
cout << "\n";
while (menuChoice != 'C' && menuChoice != 'c')
{
cout <<"Choose from one of the following menu choices:" <<endl;
cout <<" " <<endl;
cout <<"A - Find the largest number with a known quantity of numbers." <<endl;
cout <<"B - Find the smallest number with an unknown quanity of numbers." <<endl;
cout <<"C - Quit \n\n\n";
cout <<"Enter your choice now: ";
cin >> menuChoice;
cout <<" " <<endl;
switch(menuChoice)
{
case'A':
case'a':
cout <<"You chose A - Find the largest number with a known quantity. \n\n";
cout <<"Please enter how many numbers you want to enter: ";
cin >> limit;
cout <<"Please enter your " <<limit<< " numbers now: ";
for (; count < limit; count++)
{
cin >> number;
if (number >= temp_A)
temp_A = number;
}
cout <<"\n=====> The LARGEST number out of the " <<limit<< " you entered is: " <<temp_A<< "\n\n\n";
cout <<"\t=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n" <<endl;
break;
case'B':
case'b':
cout <<"You chose B - Find the smallest number with an unknown quantity of numbers. \n\n";
cout <<"Begin to type in your numbers: ";
for(; groupNbrs <= temp_B; count++)
{
cin >> groupNbrs;
if(groupNbrs <= temp_B)
{
cout <<"\n====>The SMALLEST number in the group is: " <<groupNbrs<< "\n\n";
}
break;
}
case'C':
case'c':
break;
return 0;
}
}
return 0;
}
Really? Indentation has that much impact on correct execution? Wow! I wasn't aware of that. That may sound like a sarcastic statement but that's not what was intended. I am grateful for knowing that now.
I believe I put the "break;" where it needs to be but it's still spitting out the first number I enter as the smallest, which is not true. Is the "break" still incorrectly indented?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
case'B':
case'b':
cout <<"You chose B - Find the smallest number with an unknown quantity of numbers. \n\n";
cout <<"Begin to type in your numbers: ";
for(; groupNbrs <= temp_B; count++)
{
cin >> groupNbrs;
if(groupNbrs <= temp_B)
{
cout <<"\n====>The SMALLEST number in the group is: " <<groupNbrs<< "\n\n";
}
break;
}
Really? Indentation has that much impact on correct execution? Wow! I wasn't aware of that. That may sound like a sarcastic statement but that's not what was intended. I am grateful for knowing that now.
Nah, it doesn't actually, it just makes it really hard to figure out what is happening.
Anyway, I thought your break was breaking your 'b' case...sorry, it is instead breaking out of your loop (unconditionally). This means the loop will only runs *once*, never looking at any of the variables.
Although I'm not really understanding what you are doing in that loop. All it does is check to see if the number they entered was smaller than the other numbers, and if so, display a message.