이름 목록을 관리하는 간단한 프로그램을 작성했습니다 (프로그램 세그먼트는 아래 참조). 변수 "option"에 대한 사용자 입력이 4 일 때 함수 "choice()"가 끝나고 main()으로 돌아가서 프로그램을 끝내기를 기대했습니다. 그러나 choice()는 몇 번 반복되는 경향이 있습니다 "옵션"에 4를 지정한 후에도 다음과 같이 설정하십시오.void 함수의 끝에 도달하면 예기치 않은 결과가 발생합니다.
Would you like to: (1) - Add name(s) to the list (2) - Remove name(s) from the list (3) - View the list (4) - Exit the program (type 1, 2, 3, or 4): 4 Exiting Program... Would you like to: (1) - Add name(s) to the list (2) - Remove name(s) from the list (3) - View the list (4) - Exit the program (type 1, 2, 3, or 4): 4 Exiting Program... ryan$ _
전체 프로그램이 없으면 문제의 원인은 무엇이라고 생각하십니까? 추천 할 수있는 정보가 충분하지 않다면 죄송합니다.
네 번째 if 문 선택 끝에서 "exit (0)"주석을 달았습니다. choice()가 끝나고 main()으로 돌아 가야합니다. 나는이 진술을 피하는 것을 선호 할 것이다. 그러나 그것은 내가 지금 일할 수있는 유일한 해결책이다. 당신의 도움을 주셔서 감사합니다.
- 라이언
int main()
{
cout << endl
<< "WELCOME TO THE NAME LIST PROGRAM" << endl
<< "--------------------------------" << endl;
choice();
return 0;
}
void choice()
{
string option = "0";
cout << "\nWould you like to:" << endl
<< "\t(1) - Add name(s) to the list" << endl
<< "\t(2) - Remove name(s) from the list" << endl
<< "\t(3) - View the list" << endl
<< "\t(4) - Exit the program" << endl
<< "(type 1, 2, 3, or 4): ";
getline(cin,option);
while(option != "1" && option != "2" &&
option != "3" && option != "4" )
{
cout << "\nInvalid input, reenter: ";
getline(cin,option);
}
if(option == "1")
{
appendNames();
choice();
}
else if(option == "2")
{
string flag = "menu";
removeNames(flag);
choice();
}
else if(option == "3")
{
viewNames();
choice();
}
else if(option == "4")
{
cout << "\n\nExiting Program...\n\n";
// exit(0);
}
}
메인 실행 전에'choice() '를 선언 했습니까? – lightandlight
예, main이 호출되기 전에 함수 원형을 포함합니다. 전체 프로 그램은 ~ 480 라인이므로 나는 가장 적절하다고 생각한 것을 포함 시켰습니다. – AnthonyBeach
게시 된 코드를 테스트 한 결과 문제를 재현 할 수 없습니다.OS/컴파일러 명령/실행 명령은 무엇입니까? – lightandlight