简体   繁体   English

g++:错误:<descriptive name> : 没有这样的文件或目录</descriptive>

[英]g++: error: <descriptive name>: No such file or directory

When I am using while loop, vscode will be unable to run my code... Platform: Windows 10 Compiler: Visual Studio Code and Powershell Core Language: C++当我使用 while 循环时,vscode 将无法运行我的代码... 平台:Windows 10 编译器:Visual Studio 代码和 Powershell 核心语言:C++

Not using while loop, then it works, but I need while loop to run my program again.不使用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!!!

You have to put your call of the application and compile calls in quotes.您必须调用应用程序并将调用编译为引号。 It has nothing to do with your code at all:它与您的代码完全无关:

g++ "Guess The Celebrity2.cpp" -o "Guess The Celebrity2" && "d:\Programming Files\C++\vscode\Guess The Celebrity2"

There are several errors in this code:这段代码有几个错误:

#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;
}

First input isn't initialized.第一个输入未初始化。 Second you declare a variable and then compare it to replay (you overwrite input).其次,您声明一个变量,然后将其与重播进行比较(您覆盖输入)。 Third, in your last if statement, you set input to 1, isntaead of comparing it.第三,在最后一个 if 语句中,您将 input 设置为 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM