13

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?

Deanie
  • 2,316
  • 2
  • 19
  • 35
Joanna Lancaster
  • 741
  • 1
  • 6
  • 13
  • 10
    [Get rid of the `system("PAUSE");`!](http://www.gidnetwork.com/b-61.html) – David Schwartz Dec 22 '11 at 00:10
  • `system("PAUSE")` is fine on Windows (MinGW and Cygwin should both be fine with it). But yeah, it'll break on Linux. – cHao Dec 22 '11 at 00:21
  • @cHao: It's also one of the most ridiculous ways to wait before a program exits. Spawning a whole separate process just to wait?! It can be implemented in a few lines of code! – dreamlax Dec 22 '11 at 00:58
  • @dreamlax: Or, *one*. That's all but unmistakable as to its intent, and has no real effect on functionality or performance. Face it, `system("PAUSE")` is the very, very, *very* least of this code's sins, if you can even call it that. It's also the last thing i'd worry about fixing, because frankly, it's the most correct line in the whole program. – cHao Dec 22 '11 at 01:33
  • @cHao: Well, the "pause" program prints a line saying "press any key to continue", and also quits upon first keypress (so unbuffered input). The biggest issue here is that whoever taught this person to put `system("pause");` in their program *should not be teaching C++!!*. – dreamlax Dec 22 '11 at 02:06
  • 3
    @dreamlax: It's used once, at the end of the program, most likely so that someone can see the results when clicking the "run" button in their IDE. Quit acting like it's the most horrible thing ever, cause it's not. As far as what it does, i'd *rather* see a `system("PAUSE")` than a C++ function that does the exact same thing, because neither one of them is going to be in a real (non-school, non-learning) program -- and if someone has to take time to write "press any key" code, that's time they're not using to solve a real problem. – cHao Dec 22 '11 at 03:13
  • @cHao: It's antiquated. Anyone teaching such rubbish shouldn't be teaching, and people who learn from these people continue antiquated practices. It's like someone teaching Python 1.0 when nobody uses it any more. – dreamlax Dec 22 '11 at 04:28
  • @dreamlax: "Antiquated" is overstating quite a bit. Sure, it's 20+ years old. But it's still around because *it looks and works the same as it did 20 years ago*. Try saying that about Python. Or C, for that matter. Truth is, unless there's a standard "show a prompt, ignore the buffer, and wait for any key" function, `system("pause")` actually works better than most of the alternatives. :P And it's less of a code footprint for stuff that's just not going to be in real programs anyway. Why make someone write code to do stuff that doesn't solve the real problem? – cHao Dec 22 '11 at 09:45
  • @cHao: Antiquated isn't overstating it at all. The fact is, we have IDEs that don't close windows once the process exits. There are plenty of these. Teaching the use of `system("pause")` is much worse than simply teaching someone the right tools to use in the first place. – dreamlax Dec 22 '11 at 19:07
  • @dreamlax: If they're teaching C or C++, that's what they should be teaching. Not the nuances of some particular IDE. `system("pause")` works in all versions of Windows, regardless of what IDE you use. "Tools > Options > Running Stuff > Please don't close my window", on the other hand, doesn't help anyone who uses some other IDE. – cHao Dec 22 '11 at 20:54
  • 1
    @cHao: I know I'm getting rather unjustifiably worked up over this, but it's only because I know someone who teaches C++ as a profession and encourages the use of `system("pause")` (amongst other less-portable/implementation-defined things) and has no knowledge of more important principles such as RAII. When I see `system("pause")` I immediately think of this person and it enrages me that he somehow still has a job and many people are learning poor coding practices from him. `system("pause")` is just the tip of iceberg with this guy. Mix this with excessive global variables, `void main()` ... – dreamlax Dec 22 '11 at 21:08
  • 1
    ... and excessive reliance on implementation-defined behaviour and you've got this guy. *shudder* – dreamlax Dec 22 '11 at 21:11
  • @dreamlax: LOL...i was gonna offer a chill pill. :) I get it, though. We all have our triggers. Just keep in mind that not everything that teacher said is totally wrong, even if most of it is. :) Implementation-defined behavior has its place, if you know exactly what environment you'll be in. And especially if you're only doing it to test/debug in that environment, and remove the implementation-specific stuff when testing's done. – cHao Dec 22 '11 at 21:30
  • 1
    @cHao: Yeah, you're quite right, he must be teaching something right... and implementation-defined behaviour definitely has its place, but I get the feeling that he doesn't stress the fact that different systems can behave differently with the same code. People will finish this guy's lessons and think they know C++ but really what they know is C++ on Windows (if you catch my drift). I definitely need to take a breather each time I see `system("pause")`, in hindsight it seems like such a stupid thing to get worked up over... :) – dreamlax Dec 22 '11 at 21:43

3 Answers3

35

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.

Community
  • 1
  • 1
Timothy Jones
  • 21,495
  • 6
  • 60
  • 90
  • 2
    I was actually surprised to discover that `g++ file.c` treats `file.c` as C++ code rather than C code. I suppose it makes sense, but it's not obvious. The best advice is to *both* use `g++` and to use the right suffix for the language (typically `.cpp`, but others possible). – Keith Thompson Dec 22 '11 at 03:42
6

That is C++ code, not C, and must be compiled accordingly. (It also has numerous logical errors, but we can worry about that later.)

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
6

You have C++ code:

#include <iostream>
#include <string>
using namespace std;
/* ... */

But are trying to compile it as if it were C:

gcc file.c

C++ is not C and you shouldn't treat it as if it were.

Try renaming your file to file.cpp and use g++ instead of gcc:

g++ file.cpp
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • The file renaming isn't strictly necessary but the changed name would let you use `make` more effectively. – sarnold Dec 22 '11 at 00:11
  • 1
    Some compilers choose how to interpret a file based on the extension (if it is not told what language it is in otherwise). – dreamlax Dec 22 '11 at 01:08
  • That seems like a completely reasonable optimization to make. :) – sarnold Dec 22 '11 at 01:09