Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ Help

Started by HuntersPrey Aug 7, 2007 at 11:49 AM 17 replies 1.5k views
Original Post
HuntersPrey
HuntersPrey
I recently picked up "C++ for Dummies" by Jeff Cogswell, because I need to start somewhere right? Well i got around 50 pages, it's really easy and quick, but i constantly find things different. If i type exactly what is on the page, to Dev-C++, it doesnt compile and I get a giant amount of errors. Example #include int main () { int i; cin > i; if (i > 10) { cout << "It's greater than 10." << endl; } else if (1 == 10) { cout << "It's equal to 10." << endl; } else { cout << "It's less than 10." << endl; } return 0; } I know this is simple work, but I really need help. When I started like it said, some of the things that it said would be there were altered a bit. (Instead of 'return 0;', i had something like 'return END SUCCESS;' or close to that. And also, there was no mention of what 'using namespace std;' was, and it didnt say if i needed it or not. I just need some clarification on these things.
Dwiff
Dwiff
If you put your error messages and use the C Code tag it makes it easy to debug. Right off the see:

cin > i;
should be
cin >> i;
HuntersPrey
HuntersPrey
Error : 'cin undeclared (first use this function)' ?
Svenjamin
Svenjamin
Also, cout and cin are in the standard namespace, so they must be prefixed with std:: so it looks like this: std::cin >> i;
or you can put "using namespace std;" at the top of your program and then you won't have to use the std:: prefix.

Svenjamin
ksjetski
ksjetski
after the #include on the next line put

using namespace std;
TheUnbeliever
TheUnbeliever
1. Don't use #include it's non-standard and is a relic from textbooks published before the ISO standardization of C++ is 1998. Instead, you are looking for #include . This goes for all of the standard library headers: exclude the .h at the end, because it doesn't exist.

2. Your current code shouldn't compile. You need the using namespace std; line. However, the general recommendation is to avoid using that line and instead use std::cin instead of cin, std::cout instead of cout, and so on (prepending all std namespace identifiers with std:: — the namespace resolution operator being ::).


*Cut off mid message, will edit later*
[TheUnbeliever]
Huskey
Huskey
I just installed Dev-C++ (5.0beta or sth) and the code seems to work without any problems (apart from the mentioned cin >> i;) I get no errors.

But you should use:

#include // without .h
using namespace std;
HuntersPrey
HuntersPrey
i think i have it...

#include
using namespace std;

int main () {
int i;
cin >> i;
if (i > 10) {
cout << "It's greater than 10." << endl;
}
else if (1 == 10) {
cout << "It's equal to 10." << endl;
}
else {
cout << "It's less than 10." << endl;
}
return 0;
}

and it compiles, but comes up with just the blank command screen.
juanpaco
juanpaco
It comes up with a blank command screen because it's waiting for you to input something. The line cin >> i; is telling the program to sit and wait for the user to type something in on the keyboard and hit return.

When you do a cin or other equivalent, it is a good practice to put some sort of a prompt up on the screen beforehand. That way, the user (and you at a later time) will know what is supposed to happen. For example, you could have

cout << "Please enter a number:" << endl;
cin >> i;

Of course, now you have to get into making sure that the input you get is a valid number, but that's another can of worms.


EDIT: *some spelling
HuntersPrey
HuntersPrey
after i put in a number tho, the screen disappears instead of saying what i tell it to.
Zahlman
Zahlman
It shows what you told it to (more or less - you probably want to double-check the part where you check for equality :) ) and then the command window (which was a temporary one, spawned for your program) goes away, because your program has finished running. That's how these things are supposed to work; don't fight it, but instead run the program from a permanent command window (e.g. Start->Run, run 'cmd', cd to the appropriate folder and give the executable name).
HuntersPrey
HuntersPrey
so what code should i do to make that stop from disapearing?
DeafManNoEars
DeafManNoEars
Quote:
Original post by HuntersPrey
so what code should i do to make that stop from disapearing?


You should read Zahlman's post.

OR

Add a breakpoint at the line:
"return 0;"

OR add the line

'system("pause");' just before 'return 0;' -- not an ideal solution but it will work for you.

OR add the line

'getch();' just before 'return 0;'
jpetrie
jpetrie
Or use the appropriate IDE command to keep the window open, rather than hacking up your code (Dev-C++ has two run/debug commands, one will keep the IDE open; VS has similar functionality).
nobodynews
nobodynews
Try this:

1)Open notepad(start->all programs->accessories->Notepad

2)enter the name of the program one the first line(ie., "program.exe")

3)on the next line enter "pause"

4)save this file in the same folder as program.exe and name it "program.bat". Under "save as type" select "all files" instead of "text documents"

5)to run the program, double click "program.bat" instead of "program.exe"

This will run your program and immediately after run another program: 'pause'. The program pause does what you want to do.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
HuntersPrey
HuntersPrey
Sry to bug again, but I have a problem with looping.
here's the code, typed exactly as the book says...

#include
using namespace std;

int main() {
int x = 0;
int i;
for (i = 1; i <= 100; i++) {
x += i;
}
cout << x << endl;
system("PAUSE");
return 0;
}


trying to loop the number from 1 - 100, but when i run it, the console says '5050 and 'Press any key to continue'.

(Should I post this in the Beginners forum?)
Zakwayda
Zakwayda
Quote:
Original post by HuntersPrey
(Should I post this in the Beginners forum?)
Yeah, probably. Just start a new thread there on this latest question (regarding the loop) - I'm guessing since it's a separate question, and since you did ask if it should be posted in For Beginners, that it won't be considered a cross post (that's just my guess though - I don't want to get you in trouble, so you might check with a mod first if you're not sure).

Also, when you post code, putting it in [source][/source] tags will preserve the formatting (in most cases at least).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.