Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot compile C++ with gcc

Tags:

c++

gcc

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?

like image 977
Joanna Lancaster Avatar asked Dec 22 '11 00:12

Joanna Lancaster


People also ask

Why my GCC compiler is not working?

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.

Can GCC run C?

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++.

Can I use g ++ to compile C code?

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.).


1 Answers

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:

  1. Your ( char1 != "T" || "F") should be ( char1 != "T" && char1 != "F") (note the && instead of ||)
  2. You're reading another character when you get bad input, but because your code has no loops, the program will exit before it does anything with the next character.
  3. As David Schwartz points out, it's worth removing 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.

like image 183
Timothy Jones Avatar answered Nov 16 '22 01:11

Timothy Jones