私は返信を要求するプログラムを作成していて、何回返信して表示したいのですか?私はプログラムでwhileループとswitch caseを使用しました。 しかし、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」を試したとは思わない(つまり、文字リテラルは使用しない)。そして、あなたの次のwhileループで 'i'も' x'も変更されないので、最初の問題を修正しても、無限に回転することになります。私は*強く* [C + +の良い本を提案](https:// stackoverflow。com/questions/388242/the-definitive-c-book-guide-and-list)を参照してください。 – WhozCraig
C++教科書のサンプルのように、コードを正しくフォーマットしてください。 –
'input'を' integer'として宣言し、 'input = 'a''を代入しました。その後、 'switch(input)'を入れます。 – kiLLua