我需要在我的 linux 系统上编译代码。这是简单的代码,我不知道出了什么问题:
我有这段代码,但无法编译:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string char1, char2, char3, char4, char5, char6;
cout<<"Hello this is your standard True and False quiz"<<endl;
cout<<"Please enter 'T' or 'F'"<<endl;
cout<<"No#1 George Washington invented the toilet?"<<endl;
cin>>char1;
if ( char1 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char1;
}
if ( char1 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char1;
}
if ( char1 == "T" )
{
cout<<"You entered the incorrect answer. The answer is False"<<endl;
}
cout<<"No#2 The Squareroot of 3136 is 56?"<<endl;
cin>>char2;
if ( char2 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char2;
}
if ( char2 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char2;
}
if ( char2 == "F" )
{
cout<<"You entered the incorrect answer. The answer is True"<<endl;
}
cout<<"No#3
system("PAUSE");
return 0;
}
当我尝试编译它时:
gcc file.c
我明白了:
test.c:1: fatal error: iostream: No such file or directory
compilation terminated.
据我所知,我拥有所有需要的库,我做错了什么?
最佳答案
您正在尝试使用 C 编译器编译 C++。试试 g++ file.c
而是。
另外,最好将文件命名为 file.cpp
相反 - 将其命名为 .c
不会停止它的编译,但它会帮助像 make
这样的工具.此外,它将帮助以后遇到您的源代码的其他人(包括您自己)。
编辑:您的代码还有一些与您的问题无关的其他问题,但您会在编译后立即遇到它们:
- 您的
( char1 != "T" || "F")
应该是( char1 != "T" && char1 != "F")
(注意&&
而不是||
) - 当您输入错误时,您正在读取另一个字符,但由于您的代码没有循环,因此程序将在对下一个字符执行任何操作之前退出。
- 正如 David Schwartz 指出的那样,it's worth removing
system("PAUSE")
对于 C 语言的新手来说,这些是很常见的错误(欢迎!我建议从一些教程或介绍性书籍开始。这里有一本优秀的 list of C books and tutorials)。
如果您遇到无法自行解决的问题,请随时提出另一个问题。
关于c++ - 无法使用 gcc 编译 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8597975/