나는 기능을 가지고 놀고 있는데 메뉴를 만들 수 있지만 기능에 포함시킨 다음 메인에서 함수를 호출하는 것이 가능한지 알고 싶었습니다. 예는 다음과 같습니다.
cout << "Enter 1 for info" << endl;
cout << " " << endl;
cout << "Enter 2 to Start" << endl;
cout << " " << endl;
cout << "Enter 3 to Quit" << endl;
cin >> menu;
나는 이것을 원하지만 if 및 else 문이있는 함수에서 사용자가 선택한 것에 따라 다른 문을 인쇄합니다. C ++ 언어를 사용하고 있습니다.
감사
다음은 시작하는 간단한 예입니다.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int MenuSelect() {
cout << endl;
cout << "Enter 1 for info" << endl;
cout << " " << endl;
cout << "Enter 2 to Start" << endl;
cout << " " << endl;
cout << "Enter 3 to Quit" << endl;
int selected = 0;
string input;
cin >> input;
if (stringstream(input) >> selected) {
return selected;
}
else {
return -1;
}
}
void start() {
}
int main() {
int selected = -1;
while ((selected = MenuSelect()) != 3) {
if (selected < 1) {
cout << "Invalid option" << endl;
}
else if (selected == 1) {
cout << "Info" << endl;
}
else if (selected == 2) {
cout << "START!" << endl;
start();
}
else {
cout << "Invalid option" << endl;
}
}
return 0;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다