遇到菜单重新出现的问题。使用 switch 语句

发布于 2024-12-09 21:48:19 字数 2358 浏览 1 评论 0原文

我遇到一个问题,程序运行一次后再次自行运行。当它运行时,每个选择都正确运行,没有错误,但它永远不会退出程序。有人吗?


我觉得很愚蠢,我放置了 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;
}

I am having a problem where the program runs itself again after it runs it one time. When it runs every selection is ran correctly with no errors but it never exits the program. anyone?


I feel dumb, I put the while statement so that it does repeat itself. Ok now if I take of the while statement, what else do I need to take off so that I can run it?


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

∝单色的世界 2024-12-16 21:48:19

即使它可以在 ideone 上运行,我想如果有任何问题,那么问题就出在 类型上 selection,因为使用它意味着逐字符读取,包括换行符和全部。因此,selection 的更好的类型选择是 int,因为它只会读取整数,跳过所有其他可能引发问题的字符。

我建议您将 selection 的类型从 char 更改为 int,并使用 0,12 等,而不是 '0''1''2'等等。

顺便说一下,你忘记在 case 中使用 break '0'

case 0: //<--- I changed it from '0' to 0, assuming selection's type is int
    cout << "Goodbye.\n";
    break; //add this line!

不要忘记更改它(以及在所有 case 语句中):

while(selection != 0); //changed '0' to 0

Even though it works at ideone, I guess if there is any problem, then the problem is with the type of selection, as using it means reading character by character, including newline and all. So a better choice of type for selection would be int, as it will read only integers, skipping all other characters which might be inviting problems.

I would suggest you to change the type of selection from char to int, and use 0,1, 2 etc, rather than '0','1', '2' etc.

By the way, you forgot to use break in case '0':

case 0: //<--- I changed it from '0' to 0, assuming selection's type is int
    cout << "Goodbye.\n";
    break; //add this line!

Don't forgot to change this (and in all case statements):

while(selection != 0); //changed '0' to 0
孤单情人 2024-12-16 21:48:19
case '0':
        cout << "Goodbye.\n";

我认为您在这里缺少 return 0; 语句。

case '0':
        cout << "Goodbye.\n";

I think you're missing a return 0; statement here.

唐婉 2024-12-16 21:48:19

Exit 对我有用,但你的最低限度检查仍然不正确。 (尝试whit 1 3 2)

我会这样写那部分。

 #include <algorithm>
 .
 .
 .

 int m = min(in1,in2);
 m = min(m,in3);
 cout << m;

Exit works for me, but you still have incorrect minimum checking. (try whit 1 3 2)

I would write that part like.

 #include <algorithm>
 .
 .
 .

 int m = min(in1,in2);
 m = min(m,in3);
 cout << m;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文