当我使用while循环时,vscode将无法运行我的代码... 平台:Windows 10 编译器:Visual Studio代码和Powershell核心 语言:C ++
不使用while循环,它可以工作,但是我需要while循环才能再次运行程序。
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main(){
int replay = 0;
int input;
while(input == replay)
{
//Creating variables to store
string celebrity = "Keeves Reeves";
string guess;
//Prompt the user
cout << "Guess this celebrity: He acted as John Wick" << endl;
getline(cin, guess);
//Check the answer by using switch statement
if(guess == celebrity){
cout << "Congratulations! You are right" << endl;
input = 3;
} else if(guess != celebrity)
cout << "Whoops, that not the right answer..." << endl;
cout << "If you want to try again, enter '0' to replay, enter '1' to view the answer: ";
cin >> input;
if(input = 1){
cout << "The celebrity was " << celebrity << " ." << endl;
}
}
//Break from while loop
cout << "Thank you for playing. The end." << endl;
system("pause");
return 0; } ```
D:\Programming Files\C++\vscode>cd "d:\Programming Files\C++\vscode\" && g++ Guess The Celebrity2.cpp -o Guess The Celebrity2 && "d:\Programming Files\C++\vscode\"Guess The Celebrity2
g++: error: Guess: No such file or directory
g++: error: The: No such file or directory
g++: error: Celebrity2.cpp: No such file or directory
g++: error: The: No such file or directory
g++: error: Celebrity2: No such file or directory
g++: fatal error: no input files
compilation terminated.
****NOTE THAT: DO NOT BE LIKE ME, THIS ERROR OCCURS ONLY BECAUSE I PUT SPACE ON MY FILE NAME!!! HOPE THIS HELPS!!!
答案 0 :(得分:2)
您必须对应用程序进行调用,并将调用编译为引号。完全与您的代码无关:
g++ "Guess The Celebrity2.cpp" -o "Guess The Celebrity2" && "d:\Programming Files\C++\vscode\Guess The Celebrity2"
此代码中有几个错误:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main(){
int replay = 0;
int input;
while(int input >= replay)
{
//Creating variables to store
string celebrity = "Keeves Reeves";
string guess;
//Prompt the user
cout << "Guess this celebrity: He acted as John Wick" << endl;
getline(cin, guess);
//Check the answer by using switch statement
if(guess == celebrity){
cout << "Congratulations! You are right" << endl;
input = 3;
} else if(guess != celebrity){
cout << "Whoops, that not the right answer..." << endl;
cout << "If you want to try again, enter '0' to replay, enter '1' to view the answer: ";
cin >> input;
if(input = 1){
cout << "The celebrity was " << celebrity << " ." << endl;
}
}
}
//Break from while loop
cout << "Thank you for playing. The end." << endl;
system("pause");
return 0;
}
第一个输入未初始化。其次,您声明一个变量,然后将其比较以重播(覆盖输入)。第三,在最后一个if语句中,将输入设置为1,即进行比较。
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main(){
int replay = 0;
int input = 0;
while(input == replay)
{
//Creating variables to store
string celebrity = "Keeves Reeves";
string guess;
//Prompt the user
cout << "Guess this celebrity: He acted as John Wick" << endl;
cin.ignore();
getline(cin, guess);
//Check the answer by using switch statement
if(guess == celebrity){
cout << "Congratulations! You are right" << endl;
input = 3;
} else if(guess != celebrity){
cout << "Whoops, that not the right answer..." << endl;
cout << "If you want to try again, enter '0' to replay, enter '1' to view the answer: ";
cin >> input;
if(input == 1){
cout << "The celebrity was " << celebrity << " ." << endl;
}
}
}
//Break from while loop
cout << "Thank you for playing. The end." << endl;
system("pause");
return 0;
}