我知道它的简单代码,如何解决“系统未在范围内声明”问题?
#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 function'int main()':| C:\ Documents and Settings \ Nilo \ My Documents \ Work \ Testing \ main.cpp | 8 |错误:未声明'system' 这个范围||
| ===构建完成:1个错误,0个警告=== |
答案 0 :(得分:54)
您需要添加:
#include <cstdlib>
以便编译器查看system()
的原型。
答案 1 :(得分:13)
您可能没有包含声明system()
的标头文件。
为了能够编译使用你没有(手动)自己声明的函数的C ++代码,你必须引入声明。这些声明通常存储在所谓的头文件中,您可以使用#include
预处理器指令将其拉入当前转换单元。由于代码没有#include
声明system()
的头文件,编译失败。
要解决此问题,请找出哪个头文件为您提供system()
的声明并包含该声明。正如其他几个答案中所提到的,您很可能想要添加#include <cstdlib>