【问题标题】:System not declared in scope?系统未在范围内声明?
【发布时间】:2010-10-07 21:08:34
【问题描述】:

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

#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|error: 'system' 未在 这个范围||

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

【问题讨论】:

  • 什么是`系统(“TITLE Calculator”); system ( "COLOR 2" );` 应该做的,因为系统不是内置函数

标签: c++ compiler-errors


【解决方案1】:

你需要添加:

 #include <cstdlib>

为了让编译器看到system()的原型。

【讨论】:

  • 如果使用 C 是 #include
  • @Doug:当然,但问题被标记为C++
  • @PaulR 不过,它对很多人还是有帮助的。 :)
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2011-09-14
    • 2016-08-09
    • 2019-02-17
    相关资源
    最近更新 更多