Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 带有cin的输入在开关箱中不工作_C++_Switch Statement_Cin - Fatal编程技术网

C++ 带有cin的输入在开关箱中不工作

C++ 带有cin的输入在开关箱中不工作,c++,switch-statement,cin,C++,Switch Statement,Cin,我正在创建一个程序,它要求用户回复,以及您希望打印和显示回复的时间。我在程序中使用了while循环和开关。 但是,当我在std::cin的帮助下将输入存储在变量a中时,开关盒不会收到相同的输入。感谢您的帮助 #include <iostream> using namespace std; int a; int input; int i=1; void display() { cout << "Select a choice for reply" <<

我正在创建一个程序,它要求用户回复,以及您希望打印和显示回复的时间。我在程序中使用了while循环和开关。 但是,当我在
std::cin
的帮助下将输入存储在变量
a
中时,开关盒不会收到相同的输入。感谢您的帮助

#include <iostream>

using namespace std;

int a;
int input;
int i=1;
void display()
{
    cout << "Select a choice for reply" << endl;
    cout << "1.Thank You" << endl;
    cout << "2.Welcome" << endl;
    cout << "3.Okay" << endl;
}

int main()
{
    display();
    cout << "Enter Choice" << endl;
    cin >> a;
    input='a';
    switch (input)
    {
        case '1': {
            int x;
            cout << "Enter no. of times  you want to print reply line" << endl;
            cin >> x;
            while (i <= x)
            {
                cout << "Thank you" << endl;
            }

            break;
        }
        case '2': {
            int x;
            cout << "Enter no. of times  you want to print line" << endl;
            cin >> x;
            while (i <= x)
            {
                cout << "Welcome" << endl;
            }

            break;
        }
        case '3': {
            int x;
            cout << "Enter no. of times  you want to print line" << endl;
            cin >> x;
            while (i <= x)
            {
                cout << "okay" << endl;
            }

            break;
        }
        default: {
            cout << "wrong choice" << endl;
        }

        cout << "Thank you for replying" << endl;
    }

    return 0;
}
#包括
使用名称空间std;
INTA;
int输入;
int i=1;
无效显示()
{
cout核心问题是:

input = 'a'
以及:

您可能想要的是:

input = a

case 1:
毕竟编译器应该抛出一个警告。 但为什么要复制这个值呢? 照办


实际上,您的代码有很多问题,但我会让其他人详细说明。

您的问题是
input='a';
,输入值将是97,如果您编写
input=(int)'a';

为什么要声明“a”变量?只需使用输入即可
cin>>输入;

#包括
#include <iostream>
using namespace std;
int a;
int input;
int i=1;
void display()
{
    cout<<"Select a choice for reply"<<endl;
    cout<<"1.Thank You"<<endl;
    cout<<"2.Welcome"<<endl;
    cout<<"3.Okay"<<endl;
}

int main()
{
    display();
    cout<<"Enter Choice"<<endl;
    cin>>a;
    input=a;
    switch(input)
    {
        case 1 :
        {
            int x;
            cout<<"Enter no. of times  you want to print reply 
            line<<endl;
            cin>>x;
            while(i<=x)
            {
                cout<<"Thank you"<<endl;
                x--;
            }
            break;
        }
        case 2 :
        {
            int x;
            cout<<"Enter no. of times  you want to print line" <<endl;
            cin>>x;
            while(i<=x)
            {
                cout<<"Welcome"<<endl;
                x--;
            }
            break;
        }
        case 3 :
        {
            int x;
            cout<<"Enter no. of times  you want to print line"<<endl;
            cin>>x;
            while(i<=x)
            {
                cout<<"okay"<<endl;
                x--;
            }
            break;
        }
        default:
        {
            cout<<"wrong choice"<<endl;
        }
        cout<<"Thank you for replying"<<endl;
    }
    return 0;
}
使用名称空间std; INTA; int输入; int i=1; 无效显示() {
您的代码中有几个问题:
1)尝试为整数指定一个字符:
input='a';

要正确执行此操作,应直接使用变量:
input=a;
(不带“)

2) 在您的开关案例中,您将与“1”、“2”(类型char)进行比较,但
输入的类型为integer。请尝试以下操作:
案例1:
案例2:
(不带“)

3) 如果在while循环中不增加/减少i或x,它们将永远不会停止。请尝试以下操作:
i++
x--


一些评论:
1) 可以使用不带花括号({})的开关大小写:


4)最后,但我建议你阅读一些优秀的C++书籍,这是很值得的。()

<代码>案例1(<)/代码>——这对你来说是个小机会。我想你没有尝试过代码>案例1 (也就是说,不要使用字符文字)。因为在< <代码> > < /Cord>循环中,无论是<代码> i <代码>也不是<代码> x >代码>,都注定要旋转到无穷大,即使你修复了第一个问题。我强烈建议A。请正确地格式化代码,就像C++ C++文本中的示例。你声明“代码>输入<代码>为<代码>整数< /代码>,然后分配< COD >。e> 输入class='a'
。然后你把
开关(输入)
。用所有警告和调试信息(例如
g++-Wall-Wextra-g
和…)编译。然后使用调试器(
gdb
)我想你把数字(
'9'
)和数字(
9
)混淆了。您的问题是input='a';输入值将是97,如果您写入input=(int),则相同“a”;也不需要取两个不同的变量,即a和输入,一个就足够了。但由于您声明并使用了这两个变量,我没有从程序中删除它们。当您编写case st.in switch时,请始终记住integer is的语法(case 1:)其中1可以替换为您提供的作为字符选择和语法的任何数字(大小写为“a”)。此外,在while循环中,您忘了给出终止条件(x-;)。
switch(a)
#include <iostream>
using namespace std;
int a;
int input;
int i=1;
void display()
{
    cout<<"Select a choice for reply"<<endl;
    cout<<"1.Thank You"<<endl;
    cout<<"2.Welcome"<<endl;
    cout<<"3.Okay"<<endl;
}

int main()
{
    display();
    cout<<"Enter Choice"<<endl;
    cin>>a;
    input=a;
    switch(input)
    {
        case 1 :
        {
            int x;
            cout<<"Enter no. of times  you want to print reply 
            line<<endl;
            cin>>x;
            while(i<=x)
            {
                cout<<"Thank you"<<endl;
                x--;
            }
            break;
        }
        case 2 :
        {
            int x;
            cout<<"Enter no. of times  you want to print line" <<endl;
            cin>>x;
            while(i<=x)
            {
                cout<<"Welcome"<<endl;
                x--;
            }
            break;
        }
        case 3 :
        {
            int x;
            cout<<"Enter no. of times  you want to print line"<<endl;
            cin>>x;
            while(i<=x)
            {
                cout<<"okay"<<endl;
                x--;
            }
            break;
        }
        default:
        {
            cout<<"wrong choice"<<endl;
        }
        cout<<"Thank you for replying"<<endl;
    }
    return 0;
}
int a = 0;
cin >> 1;
switch(a)
{
    case 1:
        break;
    case 2:
        int tmp = 0;
        cout<<"tmp: "<<tmp<<endl;
        break;
    default:
        break;
}
cout << "Enter Choice" << endl;
cin >> input;
switch (input)