系统未在范围内声明?

发布于 2024-09-26 17:46:30 字数 2925 浏览 0 评论 0原文

我知道它的简单代码,如何解决“系统未在范围内声明”问题?

#include<iostream>
using namespace std;

int main(void)
{
    system ( "TITLE Calculator" );
    system ( "COLOR 2" );
    char cChar;
    double dfirstnumber;
    double dsecondnumber;
    char cDoagain;

    do
    {
        system("CLS");
        cout << "Please enter the first number you would like to use."<< endl;
        cin >> dfirstnumber;
        cout<< "Please enter the operation you would like to perform." << " (+,-,*,or /)" << endl;
        cin >> cChar;
        cout<< "Please enter the second number you would like to use." << endl;
        cin >> dsecondnumber;

        switch (cChar)
        {
            case '+':
                cout << "The answer is: " << dfirstnumber << "+" << dsecondnumber << "=" <<
                (dfirstnumber + dsecondnumber) << endl;
                break;
            case '-':
                cout << "The answer is: " << dfirstnumber << "-" << dsecondnumber << "=" <<
                (dfirstnumber - dsecondnumber) << endl;
                break;
            case '*':
                cout << "The answer is: " << dfirstnumber << "*" << dsecondnumber << "=" <<
                (dfirstnumber * dsecondnumber) << endl;
                break;
            case 'x':
                cout << "The answer is: " << dfirstnumber << "x" << dsecondnumber << "=" <<
                (dfirstnumber * dsecondnumber) << endl;
                break;
            case 'X':
                cout << "The answer is: " << dfirstnumber << "X" << dsecondnumber << "=" <<
                (dfirstnumber * dsecondnumber) << endl;
                break;
            case '/':
                if(dsecondnumber == 0){
                cout<< "That is an invalid operation." << endl;}
                else{
                cout << "The answer is: " << dfirstnumber << "/" << dsecondnumber << "=" <<
                (dfirstnumber / dsecondnumber) << endl;

        }
                break;
                default:
                    cout << "That is an invalid operation." << endl;
                    break;
    }
                cout << "Would you like to start again? (Y/N)" << endl;
                cin >>  cDoagain;
    }while (cDoagain == 'Y' or cDoagain == 'y');
    system("PAUSE");
    return 0;
}

这是我的结束语:

C:\Documents and Settings\Nilo\My Documents\Work\Testing\main.cpp||In 函数'int main()':| C:\Documents and Settings\Nilo\My Documents\Work\Testing\main.cpp|8|错误:“系统”未在中声明 这个范围||

|=== 构建完成:1 个错误,0 个警告 ===|

I know its simple code, How do I fix "System not declared in scope" problem?

#include<iostream>
using namespace std;

int main(void)
{
    system ( "TITLE Calculator" );
    system ( "COLOR 2" );
    char cChar;
    double dfirstnumber;
    double dsecondnumber;
    char cDoagain;

    do
    {
        system("CLS");
        cout << "Please enter the first number you would like to use."<< endl;
        cin >> dfirstnumber;
        cout<< "Please enter the operation you would like to perform." << " (+,-,*,or /)" << endl;
        cin >> cChar;
        cout<< "Please enter the second number you would like to use." << endl;
        cin >> dsecondnumber;

        switch (cChar)
        {
            case '+':
                cout << "The answer is: " << dfirstnumber << "+" << dsecondnumber << "=" <<
                (dfirstnumber + dsecondnumber) << endl;
                break;
            case '-':
                cout << "The answer is: " << dfirstnumber << "-" << dsecondnumber << "=" <<
                (dfirstnumber - dsecondnumber) << endl;
                break;
            case '*':
                cout << "The answer is: " << dfirstnumber << "*" << dsecondnumber << "=" <<
                (dfirstnumber * dsecondnumber) << endl;
                break;
            case 'x':
                cout << "The answer is: " << dfirstnumber << "x" << dsecondnumber << "=" <<
                (dfirstnumber * dsecondnumber) << endl;
                break;
            case 'X':
                cout << "The answer is: " << dfirstnumber << "X" << dsecondnumber << "=" <<
                (dfirstnumber * dsecondnumber) << endl;
                break;
            case '/':
                if(dsecondnumber == 0){
                cout<< "That is an invalid operation." << endl;}
                else{
                cout << "The answer is: " << dfirstnumber << "/" << dsecondnumber << "=" <<
                (dfirstnumber / dsecondnumber) << endl;

        }
                break;
                default:
                    cout << "That is an invalid operation." << endl;
                    break;
    }
                cout << "Would you like to start again? (Y/N)" << endl;
                cin >>  cDoagain;
    }while (cDoagain == 'Y' or cDoagain == 'y');
    system("PAUSE");
    return 0;
}

Heres my end message:

C:\Documents and Settings\Nilo\My Documents\Work\Testing\main.cpp||In
function 'int main()':| C:\Documents and Settings\Nilo\My
Documents\Work\Testing\main.cpp|8|error: 'system' was not declared in
this scope||

|=== Build finished: 1 errors, 0 warnings ===|

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悍妇囚夫 2024-10-03 17:46:30

您需要添加:

 #include <cstdlib>

以便编译器能够看到 system() 的原型。

You need to add:

 #include <cstdlib>

in order for the compiler to see the prototype for system().

幻梦 2024-10-03 17:46:30

您可能没有包含声明 system() 的头文件。

为了能够编译使用您未(手动)自己声明的函数的 C++ 代码,您必须提取声明。这些声明通常存储在所谓的头文件中,您可以使用#include 预处理器指令将其拉入当前翻译单元。由于代码中没有#include声明system()的头文件,因此编译失败。

要解决此问题,请找出哪个头文件为您提供了 system() 声明并将其包含在内。正如其他几个答案中提到的,您很可能需要添加 #include

Chances are that you've not included the header file that declares system().

In order to be able to compile C++ code that uses functions which you don't (manually) declare yourself, you have to pull in the declarations. These declarations are normally stored in so-called header files that you pull into the current translation unit using the #include preprocessor directive. As the code does not #include the header file in which system() is declared, the compilation fails.

To fix this issue, find out which header file provides you with the declaration of system() and include that. As mentioned in several other answers, you most likely want to add #include <cstdlib>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文