사용자에게 회신 및 회신 인쇄를 원하는 횟수를 물어 보는 프로그램을 만듭니다. 나는 프로그램에서 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'' - 음. 당신을 위해 일할 작은 기회. 나는 'case 1'을 시도하지 않았다고 가정합니다 (즉, 문자 리터럴을 사용하지 마십시오). 그리고 이후 'while'루프에서는'i'도'x'도 변경되지 않으므로 첫 번째 문제를 해결하더라도 무한대로 회전 할 운명입니다. 나는 강력하게 C++에 대한 좋은 책을 제안한다. (https : // stackoverflow.co.kr/questions/388242/the-definitive-c-book-guide-and-list)를 참조하십시오. – WhozCraig
C++ 교과서의 샘플처럼 코드의 형식을 올바르게 지정하십시오. –
'input'을'integer'로 선언하고'input = 'a''라고 지정했습니다. 그 후에 당신은'switch (input)'을 넣는다. – kiLLua