PDA

View Full Version : Learning C++ with Dev-C++, Have basic questions


demoneyejin
11-02-2009, 02:43 PM
My main problem with learning something without a teacher is that I cannot ask a lot of questions, which I'm sure will be answered later on in the online book I found...

"http://newdata.box.sk/bx/c/htm/ch01.htm"

(I added it, just incase you guys think that's a bad place to start. )

Okay I honestly do not understand what errors I'm making when I'm following the instructions completely to the ++:p

the book wants me to do this..

1: #include <iostream.h>
2: int main()
3: {
4: int x = 5;
5: int y = 7;
6: cout "\n";
7: cout << x + y << " " << x * y;
8: cout "\n";
9:return 0;
10: }

When I type it in though and try to compile I get an error and..

7 C:\Documents and Settings\math solution.cpp `cout' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)

7 C:\Documents and Settings\math solution.cpp expected `;' before string constant

9 C:\Documents and Settings\math solution.cpp expected `;' before string constant

C:\Documents and Settings\Makefile.win [Build Error] ["math solution.o"] Error 1


if it helps here a screen shot too.. lol yes I'm obsessed but I'm also obsessed with learning c++!

http://i38.photobucket.com/albums/e124/demoneyejin/Cerrors.jpg



Now I think the main problem is because of the compiler I'm using, it may be too sophisticated for me to begin with, but then again it could be something else..

if you guys can help..

tomws
11-02-2009, 03:27 PM
You've included a header file, but you're not telling the compiler that you want to use cout. For that, you need to use the using statement. A quick workaround that probably shouldn't be used in production code is:
using namespace std;

Place it right after the include.

The tutorial you're following doesn't mention this at all, which suggests either it's not really meant for someone starting from scratch, or it's incomplete and will mess with your head.

oracleguy jumps into lots of the c/c++ threads here, so maybe he'll have an idea of a good book or online resource for you. I've not done any c/c++ since completing my degree.

EDIT: You may also need to change from the older c-style header (iostream.h) to the cpp style (iostream). Not sure on that. I had to do it here for testing.

demoneyejin
11-02-2009, 03:44 PM
Thank you for that insight, I guess I better look around for something else. I added the using namespace std; but it still brings up the same error, and I have no idea why.

Lol I have no idea what a namespace std; is either. I wish there was a C++ book that would just take the first few chapters and explain what everything is and does that I will be using.. ugh..

tomws
11-02-2009, 04:10 PM
I wish there was a C++ book that would just take the first few chapters and explain what everything is and does that I will be using.. ugh..

Considering that C++ is one of the most robust application development languages, you'd probably give up before ever getting to the coding portion of the book. A mixture of simple code and explanation is usually best. The one you're using is flawed, that's all.

We used the Deitel textbook for C++ when I was in school. You can probably pick up an older version cheap on Amazon. I just checked, and the old version 5 we used is available from $17. Search for ISBN 0131857576.

What does your current code look like and what is the latest error? We can help debug what you have now.

demoneyejin
11-02-2009, 04:25 PM
Ah Yeah, Lol Broke and jobless, can't spend money :p Gotta use my online resources!

I have a lot of free time and I don't plan on giving up any time soon, I've got a lot of patience and am willing to do whatever to learn the concepts etc. I just get frustrated that I can't get past day 1 on that tutorial haha, makes me feel dumb.

Currently this is what I have..


http://i38.photobucket.com/albums/e124/demoneyejin/Cerrors-1.jpg

tomws
11-02-2009, 04:37 PM
On line 9 you have:

cout "\n";

Should be:

cout << "\n";

Note the << operator.

EDIT: Also on line 11.

demoneyejin
11-02-2009, 04:44 PM
Yarr!! it worked! neatttt so awesome. I love this whole programming thing. I hope you don't mind but I have a million questions now lmao..

#include <iostream>( this is a header)

using namespace std;( what's this?)

int main()( this is a function..right? why do functions need a "()"?)

{
int x = 5;( integer)
int y = 7;(integer)
cout << "\n"; (what does cout stand for? I know it has to do with output?)
cout << x + y << " " << x * y;
cout << "\n";
return 0; (returning something means to end the function, right?)
}

tomws
11-02-2009, 04:55 PM
The using namespace std; tells the compiler what components you want to use from the header file. In this case, you're telling it you want everything. Good, old programming 101 (and probably good programming practice) would have you rather specifically list your requirements so you don't suffer from code bloat. Since you're using cout, the syntax would be, as I recall:
using std::cout;

I'm not a C coder, though, so maybe someone can clarify the details here.

As for what cout means, I think it's "console output".

Functions use parentheses to allow arguments to be passed in for processing. You'll get to those in a few lessons, I suspect.

And, yes, return means, more or less, that's the end of the function. What it does is to return the value to the caller. For the main function, it's the program's return value. Useful for determining success/failure.

demoneyejin
11-02-2009, 04:59 PM
Haha, thanks man. You've def helped me a lot today. I can finally make it to day 2 of this e-book until I find something better to go by. :)

oracleguy
11-02-2009, 05:28 PM
The using namespace std; tells the compiler what components you want to use from the header file. In this case, you're telling it you want everything. Good, old programming 101 (and probably good programming practice) would have you rather specifically list your requirements so you don't suffer from code bloat. Since you're using cout, the syntax would be, as I recall:
using std::cout;

That is pretty close. The using namespace std; means you want to include everything in a namespace called 'std'. And a namespace can span more than one header file. All the standard C++ library stuff is in that namespace. As for what namespaces are, I'd suggest this link: http://www.cplusplus.com/doc/tutorial/namespaces/

And tomws is correct about the using std::cout;. In production code you would want to avoid bringing in the whole namespace, only bring in stuff as needed.