我正在创建一个程序,询问用户的回复以及要打印回复并显示它的次数。我在程序中使用了一个while循环和开关盒。 但是,当我在std::cin
的帮助下将输入存储在变量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;
}
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;
}
'case'1'' - um。很少有机会为你工作。我不假设你试过'案例1'(即不要使用字符文字)。既然'i'和'x'都不会在你的'while'循环中改变,即使你解决了第一个问题,它也注定要旋转到无穷大。我*强烈*建议[关于C++的好书](https:// stackoverflow。COM /问题/ 388242 /对,最终-C-书指南和列表)。 – WhozCraig
请正确格式化您的代码,就像您C++教科书中的示例一样。 –
您声明'input'为'integer',然后分配'input ='a''。之后你把'switch(input)'。 – kiLLua