很抱歉,如果这是一个简单的问题,但我对C ++还不熟悉。我写的任何方式和简单的气象站接收用户输入并最终打印出来。在这种特定情况下,要求用户使用给定的N,NE,NW ......等选项在风向上进入。我的问题是我可以进行某种循环,如果他们没有输入那些合适的字符串值,则说“无效再试一次”并重复进行。
以下是代码:
//Asks the user to input the current wind direction and stores it as windDirection
cout << "Please enter the current wind direction(N, NE, NW, S, SE, SW, E, W), then press enter." << endl;
getline(cin, windDirection);
答案 0 :(得分:0)
这个while循环最终为我工作,感谢那些帮助过的人。它可能会更好,但它可以做到这一点!
//Asks the user to input the current wind direction and stores it as windDirection
while (cout << "Please enter the current wind direction(N, NE, NW, S, SE, SW, E, W), then press enter." << endl &&
cin >> windDirection && windDirection != "N" && windDirection != "NE" && windDirection != "NW" && windDirection != "S"
&& windDirection != "SE" && windDirection != "SW" && windDirection != "E" && windDirection != "W"){
cout << "That is an Invalid wind direction input, try again.\n";
cin.clear();
cin.ignore();
}