expected `;' before "cout"

I am new to c++, my second day actually. I typed up this code from the tutorial site I am using, http://newdata.box.sk/bx/c/htm/ch02.htm .

I get an error
6 C:\C++\ch2l2.2.cpp expected `;' before "cout"
Whenever I try to compile it.

This is a serious issue to me, I have searched google for an hour and have not found out yet a fix. I know you will tell me, Don't use iostream.h!, However when I don't I get MORE errors.

One more thing, is "\n" the same as endl?


#include <iostream.h>
int main()
{
cout << "Hello there.\n";
cout << "Here is 5: " << 5 << "\n"
cout << "The maipulator endl writes a new line to the screen." << endl;
cout << "Here is a very big number:\t" << 70000 << endl;
cout << "Here is the sum of 8 and 5:\t" << 8+5 << endl;
cout << "Here's a fraction:\t\t" << (float) 5/8 << endl;
cout << "and a very very big number:\t" << (double) 7000 * 7000 << endl;
cout << "Don't forget to replace Charles Hancock with your name...\n";
cout << "Charles Hancock is a C++ programmer!\n";
return 0;
}
Last edited on
cout << "Here is 5: " << 5 << "\n"

You need a semicolon after the "\n" line... it needs to come before each "cout"

As far as the compile complaining if you remove the ".h" from the include, that means you're using an old compiler (pre ISO C++ I believe, or just C). Have you tried Code::Blocks? I've become a big fan of it myself over the past week or so (which is how long I've been doing this). MS VC++ is another one with a much more visual interface, though I prefer CB.

As an alternative to all those end-of-line (;) and couts, you could do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream.h>
int main()
{
cout << "Hello there.\n"
<< "Here is 5: " << 5 << "\n"
<< "The maipulator endl writes a new line to the screen." << endl
<< "Here is a very big number:\t" << 70000 << endl
<< "Here is the sum of 8 and 5:\t" << 8+5 << endl
<< "Here's a fraction:\t\t" << (float) 5/8 << endl
<< "and a very very big number:\t" << (double) 7000 * 7000 << endl
<< "Don't forget to replace Charles Hancock with your name...\n"
<< "Charles Hancock is a C++ programmer!\n";
return 0;
}

e.g. all one line...

hth
Wow, I am dumb. I didn't even think to look BEFORE the cout that was on line 6...

Thanks for the Code::Blocks idea, I am downloading it now. I had spent an hour trying to find a good compiler yesterday, and finally gave up when Dev-C++ worked for me.

The alternative thing there, would that work for all objects? I would assume yes but confirmation would be nice.
Wow, I am dumb. I didn't even think to look BEFORE the cout that was on line 6...

No worries, took me a bit to get used to the compiler messages also.

Dev-C++ worked for me

Apparently, the last version of Dev (4.9.9?) hasn't been updated in about 5 years (and that's beta to boot).

DevC++ did go on to have something to do with wxWidgets I think (another compiler I've seen recommended here), but I haven't played around with it. CB has an addon for wxWidgets I see though, but still haven't gotten to it. I'm pretty partial to CodeBlocks... there's tons of options and customization and compiler seems to be (in my limited experience) good.

The alternative thing there, would that work for all objects? I would assume yes but confirmation would be nice.

Yea, pretty much. It's all one large statement, and the statement doesn't end until a ; is reached (or a closing bracket }, as the brackets {} denote a series of statements). The concept is called whitespace. Spaces, lines, tabs, etc etc, they don't matter. This is perfectly legal:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
  cout

          <<

"yay!"

<< "blah"




;
}


... which is the exact equivelant to:

 
int main(){cout<<"yay!blah";}


It boils down to readability and coding style.

hth


edit: note that there are a few subtleties to the closing line/closing bracket things. Classes, for instace, require a closing line at the end of the bracket:
1
2
3
4
5
class MyClass
{
  public:
    void MyMemberFunction();
};

where "if" statements have a syntax:
1
2
3
4
5
6
7
int SomeFunc()
{
    if (expression)
       DoThis;
    else
       DoThat;
}
Last edited on
Topic archived. No new replies allowed.