我遇到一个问题,程序在运行一次后再次运行它自己。当它运行时,每个选择都正确运行,没有错误,但它永远不会退出程序。任何人?有菜单重新发生的问题。使用switch语句
我觉得哑巴,我把while语句放到了它自己的位置。好的,现在如果我采用这个while语句,我还需要起飞哪些东西才能运行呢?
#include <iostream>
using namespace std;
int main()
{
int in1, in2, in3;
char selection;
do
{
cout << " Welcome to the CS221 Homework 2 Menu\n";
cout << " ====================================\n";
cout << " 1. Multiply two integers\n";
cout << " 2. Divide two integers\n";
cout << " 3. Check if a number is within the range 10-20\n";
cout << " 4. Find the minimum of a list of 3 numbers\n";
cout << "\n";
cout << " 0. Exit\n";
cout << " ====================================\n";
cout << " Enter selection: ";
cin >> selection;
cout << endl;
switch (selection)
{
case '1':
cout << "Please enter two integers: ";
cin >> in1 >> in2;
cout << in1 << " times " << in2 << " is " << (in1 * in2) << endl;
break;
case '2':
cout << "Please enter two integers: ";
cin >> in1 >> in2;
cout << in1 << " divided by " << in2 << " is " << ((double) in1/in2) << endl;
break;
case '3':
cout << "Please enter an integer: " ;
cin >> in1;
if ((in1 >= 10) && (in1 <= 20))
{
cout << in1 << " is within the range 10-20.\n";
}
else
{
cout << in1 << " is NOT within the range of 10-20.\n";
}
break;
case '4':
cout << "Please enter three integers: ";
cin >> in1 >> in2 >> in3;
cout << "The minimum is ";
if((in1 <= in2) && (in2 <= in3))
{
cout << in1;
}
else if((in2 <= in1) && (in2 <=in3))
{
cout << in2;
}
else
{
cout << in3;
}
cout << ".\n";
break;
case '0':
cout << "Goodbye.\n";
default: cout <<selection << "is not a valid menu item.\n";
cout << endl;
}
}while (selection != '0');
return 0;
}
您的输入是什么?张贴一些输入,因为它适用于我:http://ideone.com/Oe1Mi – Nawaz
请注意,您在那里缺少一些'break'。 –