3
다음 코드에서 사용자가 올바른 입력을 제공 할 때까지 루프를 실행하고 싶습니다. 그러나 시도했을 때 논스톱 루프가되었습니다.
Please Enter Valid Input.
while 루프가 없어도 동일합니다. while 루프 여기잘못된 입력에 대한 std :: cin 무한 루프
: while 루프없이 여기
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <sstream>
using namespace std;
class library {
public:
library() {
int mainOption;
cout<<"Please choose the option you want to perform."<<endl;
cout<<"1. Member Section"<<"\n"<<"2. Books, Lending & Donate Section"<<"\n"<<"3. Returning Section"<<endl;
bool option=true;
while (option==true) {
cin>>mainOption;
if (mainOption==1) {
cout<<"section 1"<<endl;
option=false;
} else if (mainOption==2) {
cout<<"section 1"<<endl;
option=false;
} else if (mainOption==3) {
cout<<"section 1"<<endl;
option=false;
} else {
cout<<"Please Enter Valid Input. "<<endl;
//option still true. so it should ask user input again right?
}
}
}
};
int main(int argc, const char * argv[])
{
library l1;
return 0;
}
. 그러나 똑같은 일이 일어나고 있습니다.
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <sstream>
using namespace std;
class library {
public:
library() {
int mainOption;
cout<<"Please choose the option you want to perform."<<endl;
cout<<"1. Member Section"<<"\n"<<"2. Books, Lending & Donate Section"<<"\n"<<"3. Returning Section"<<endl;
cin>>mainOption;
if (mainOption==1) {
cout<<"section 1"<<endl;
} else if (mainOption==2) {
cout<<"section 1"<<endl;
} else if (mainOption==3) {
cout<<"section 1"<<endl;
} else {
cout<<"Please Enter Valid Input. "<<endl;
library();//Calling library function again to input again.
}
}
};
int main(int argc, const char * argv[])
{
library l1;
return 0;
}
정상적인 기능처럼 생성자를 사용하지 마십시오. – clcto
@clcto 매개 변수가있는 계약자를 사용해야합니다. – IamBatman
생성자는 객체의 초기 상태를 설정하는 데 사용됩니다. 이 모든 것은 실제로 void run() 함수에 있어야합니다. 당신은 이것을위한 대상조차 필요하지 않습니다. – clcto