I need to compile code on my linux system. This is simple code and I don't know what's wrong:
I have this code and I can't compile it:
#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;
}
When I try to compile it:
gcc file.c
I get:
test.c:1: fatal error: iostream: No such file or directory
compilation terminated.
As far as I know, I have all the libraries needed, what am I doing wrong?
If you have not installed gcc compiler and trying to use gcc command then the error message is obvious as the functionality is not available on your computer. If you have not installed gcc compiler, then install MinGW gcc compiler on your development environment.
The current version is GCC 7.3, released on 2018-01-25. GCC is a key component of so-called "GNU Toolchain", for developing applications and writing operating systems. The GNU Toolchain includes: GNU Compiler Collection (GCC): a compiler suite that supports many languages, such as C/C++ and Objective-C/C++.
G++ is the name of the compiler. (Note: G++ also compiles C++ code, but since C is directly compatible with C++, so we can use it.).
You are trying to compile C++ with a C compiler. Try g++ file.c
instead.
Also, it's good practice to name your file file.cpp
instead - naming it .c
won't stop it compiling, but it will help tools like make
. Also, it'll help others who come across your source code later (including yourself).
Edit: Your code has some other problems which aren't related to your question, but you'll run in to them as soon as you get it to compile:
( char1 != "T" || "F")
should be ( char1 != "T" && char1 != "F")
(note the &&
instead of ||
)system("PAUSE")
These are pretty common mistakes for newbies to C (Welcome! I'd recommend starting with some tutorials or introductory books. Here is an excellent list of C books and tutorials).
If you run in to anything you can't solve on your own, feel free to open another question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With