|
-
April 12th, 2010, 06:47 AM
#6
Re: No output for "smallest number"? Why?
Thank you very much for your input! I got case C working now, so it quits properly when that case is chosen. However, I removed "temp_B = 0" and left it as "temp_B". I still have the same problem....after I enter a group of numbers, it immediately loops back to the menu.
So, the answer has already been given but it sounded like you didn't understand it. The way you check temp_B is your problem. Yes, you removed its initialization to 0, but you didn't re-consider any of your logic in checking it. When you don't explicitly initialize a value in C++, it will generally leave whatever value happened to already be there, though it's very possible that in visual studio it auto-initializes it to 0 for you.
What you want to do is make sure that temp_B has a valid value before you try to check it against anything. If temp_B does not have a valid value for your current data range, you need to give it one instead of checking it. You can do this either with an if statement inside your loop, or by accepting the first input outside of the loop. Which you go with depends on many factors regarding performance and constraints that I don't have information about, so I'll just pick the easiest to implement in place as a demonstration.
Code:
case 'B':
case 'b':
cout <<"You chose B - Find the smallest number with an unknown quanity of numbers. \n\n";
cout <<"Begin to type in your numbers: ";
for(; groupNbrs >= 0; count++)
{
cin >> groupNbrs;
if (count == 0)
temp_B = groupNbrs;
if(groupNbrs <= temp_B)
{
temp_B = groupNbrs;
cout <<"The smallest number in the group is: "
<<groupNbrs;
}
}
break;
}
Hope that helps.
-Thinias
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|