Alternative to <iostream.h> under gcc or better

Well, I'm getting some error from GCC about a non existing <iostream.h> and was wondering if I would have to configure my Debian box in a way that GCC won't report that header file missing ?

Yes I know that this more of a Linux newbie question but I was wondering if anyone who's using GCC had a similar problem. Also I didn't know if this was something simply solved with additional options for GCC or if I had to mess with the configuration file in Debian.

GCC gives me a fatal error: iostream.h: No such file or directory
compilation terminated.
when I included #include <iostream.h>.

So I'm a total noob with GCC and welcome any RTFM's or worse because this probably sounds like an annoying noob question about GCC.

Thank you for replies and help.
Shouldn't it still just be <iostream>? Even if it's gcc.

http://members.gamedev.net/sicrane/articles/iostream.html

quote:
However, many compilers in recent years have taken a different approach. Instead they either leave out the old style headers completely, so that code depending on iostream.h simply won't compile...
Last edited on
Okay, I tried <iostream> instead of <iostream.h> and I got some new errors with my program.

error messages from GCC

sevens-threes.cpp:20:81: warning: multi-character character constant [-Wmultichar]
sevens-threes.cpp: In function ‘int main()’:
sevens-threes.cpp:11:15: error: ‘cout’ was not declared in this scope
sevens-threes.cpp:11:15: note: suggested alternative:
In file included from sevens-threes.cpp:1:0:
/usr/include/c++/4.7/iostream:62:18: note: ‘std::cout’
sevens-threes.cpp:12:15: error: ‘cin’ was not declared in this scope
sevens-threes.cpp:12:15: note: suggested alternative:
In file included from sevens-threes.cpp:1:0:
/usr/include/c++/4.7/iostream:61:18: note: ‘std::cin’

end error messages

And here's the code that I copied from an OREILLY book about C++ programming from 95 I think.

code

#include <iostream>

int seven_count; // Number of sevens in the data
int data[5]; // The data to count 3 and 7 in
int three_count; // Number of threes in the data
int index; // Index into the data

main() {
seven_count = 0;
three_count = 0;
cout << "Enter 5 numbers\n";
cin >> data[1] >> data[2] >> data[3] >>
data[4] >> data[5];
for (index = 1; index <= 5; ++index) {
if (data[index] == 3)
++three_count;
if (data[index] == 7)
++seven_count;
}
cout << " Threes " << three_count << " Sevens " << seven_count << '/n';
return (0);
}
end code

I'm thinking that it's the age of the source code that I copied from the book personally :/ Thanks for the article by the way Ganado :)
Yeah, it's definitely the age of the code. main() needs to be declared as returning an int, and the standard streams are in the namespace std, so write std::cout and std::cin.
Okay, here's the code now after adding some std::cout and cin with the << std::endl and etc.

code

#include <iostream>

int seven_count; // Number of sevens in the data
int data[5]; // The data to count 3 and 7 in
int three_count; // Number of threes in the data
int index; // Index into the data

int main(int, **) {
seven_count = 0;
three_count = 0;
std::cout << "Enter 5 numbers\n"; << std::endl;
std::cin >> data[1] >> data[2] >> data[3] >> endl;
data[4] >> data[5];
for (index = 1; index <= 5; ++index) {
if (data[index] == 3)
++three_count;
if (data[index] == 7)
++seven_count;
}
std::cout << " Threes " << three_count << " Sevens " << seven_count << '/n'; << std::endl
return (0);
}

end code

GCC is still giving me some errors and I was wondering if I would have to add std::endl to << three_count and seven_cout separately ? Or is there a more better way to ?
You should use the forum's <> Format to put your code in code format to make it readable.

For your purposes, I suggest just putting using namespace std; in a new line after your #include <iostream>, but you also have a problem with having "endl" in a cin statment, as explained in Edit 2.

Edit: Also, just make it
1
2
3
4
int main()
{
    //code
}
at the start of your main function.

Edit 2:

You are using the >> operator for cin, but then you have "endl" in the same line , which should be used with cout << only. You also have a semicolon after the endl, so your next line makes no sense to the compiler.

Make
1
2
std::cin >> data[1] >> data[2] >> data[3] >> endl;
data[4] >> data[5]; 

Be
1
2
std::cin >>  data[0] >> data[1] >> data[2] >>
data[3] >> data[4];


Arrays/Vectors are base-0, so the first element in data is data[0], and data[5] is unpredictable as it isn't defined in the array. Also, if you use a semicolon, that ends the cin input, so your next line doesn't make sense to the compiler.

Sorry I quickly wrote those edits, here is what your code should look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
    int data[5];         // The data to count 3 and 7 in
    int seven_count = 0; // Number of sevens in the data
    int three_count = 0; // Number of threes in the data
    std::cout << "Enter 5 numbers\n" << std::endl;
    std::cin >> data[0] >> data[1] >> data[2] >> data[3] >> data[4];
    for (int index = 0; index <= 4; ++index)
    {
        if (data[index] == 3)
        ++three_count;
        if (data[index] == 7)
        ++seven_count;
    }
    std::cout << " Threes " << three_count << " Sevens " << seven_count << std::endl;
    return 0;
}

Other notes:
'\n' should be double quotation marks, not single (also fixed in above code, well I just deleted it since it is useless at the end of a program to have 2 line enders). I also deleted your global variable declarations and moved them into the corresponding line in your main function. You can keep it your way if you want, though I don't see the point.

I quickly made a bunch of edits after making my original post, so if you don't understand something specific, feel free to ask.
Last edited on
Thank you guys for helping me with this one but I got one more question and/or problem.

The GCC compiler's not liking the code at all but it runs okay in the code::blocks IDE just fine.

Is this because IDE's include their own header files, supply the necessary missing files, ignore different versions of the C code, or is it because code::block's compiler is less advance than GCC ?

I dunno if I should continuing using GCC to compile my programs or just use code::blocks because I'm not sure if I'm really ready to start using command line compilers yet.

Don't get me wrong, I'm not one of those point and click users or anything because I do use the command line, but I'm just not sure if GCC is over my head or not. :/
I thought code::blocks just called gcc for you, so there shouldn't be any real difference, except that you may be passing different parameters when you use the command line.
Make sure to use g++ instead of gcc when compiling C++ code with GCC.
Last edited on
@Peter87 Thanks lol, that made all the difference.
Topic archived. No new replies allowed.