是否有其他方式来键入此代码?
我希望它更清楚.
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main(){
string x;
for (x; x != "EXIT";){
cin >> x;
cin.ignore();
if (x == "EXIT"){
return EXIT_SUCCESS;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在控制台中键入"EXIT"时,代码应该关闭.
我发现在Google上找到合适的答案很难,所以我决定问你们.
因为我知道Stackoverflow根本不是noob友好的,所以我在这里犹豫不决.
你不需要测试if (x == "EXIT")你是否也要进行测试x != "EXIT".做就是了:
int main(){
string x;
while (x != "EXIT"){
cin >> x;
cin.ignore();
}
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
或者,使用(n为空)for循环:
int main() {
for (string x; x != "EXIT"; (cin >> x).ignore()) ;
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)