Stupid C++ Question
DY357LX
Playing since day 1. Still can't Comm.England Join Date: 2002-10-27 Member: 1651Members, Constellation
<div class="IPBDescription">I know the answer but can't remember it!</div> OK, i'm learning C++ from an 800 page book
and understand everything fairly well so far.
i've reached the end of the first chapter and completed
the test at the end without too much difficulty. But
when i tried to run the program outside of the C++
development enviroment (Microsoft Visual C++) it
simply flashes onscreen.
The program is running but quickly closing itself.
I've had this problem in the past when trying out some C, but
can't remember how I got past it.
The program will actually compile, link and execute whilst using the
msdev "Execute" button. But whilst msdev is closed and the program
is ran in a normal Windows Explorer window.... its flops.
Here's the code, i'm pretty sure I only need another line
of code to keep the DOS window open.
Example
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->#include <iostream.h>
int main()
{
cout << "Hello World!\n";
return 0;
}<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
and understand everything fairly well so far.
i've reached the end of the first chapter and completed
the test at the end without too much difficulty. But
when i tried to run the program outside of the C++
development enviroment (Microsoft Visual C++) it
simply flashes onscreen.
The program is running but quickly closing itself.
I've had this problem in the past when trying out some C, but
can't remember how I got past it.
The program will actually compile, link and execute whilst using the
msdev "Execute" button. But whilst msdev is closed and the program
is ran in a normal Windows Explorer window.... its flops.
Here's the code, i'm pretty sure I only need another line
of code to keep the DOS window open.
Example
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->#include <iostream.h>
int main()
{
cout << "Hello World!\n";
return 0;
}<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
Comments
something else. Damn my gold-fish type memory!
edit: maybe it even is keyget ?
Nope, C++ ain't having it.
"error C2065: 'getchar' : undeclared identifier"
Same for getch <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html/emoticons/sad.gif' border='0' style='vertical-align:middle' alt='sad.gif'><!--endemo--> BAAAAH!!!!!!!!
getchar();
should work
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->#include <conio.h>
main()
{
char key;
int direction;
if (kbhit())
{
key = getch();
if (key == 119)
direction = 2;
else if (key == 100)
direction = 4;
else if (key == 115)
direction = 6;
else if (key == 97)
direction = 8;
}
}<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
this reads in the WASD keys and changes an integer accordingly.
In Windows 2000 (and any other Microsoft OS where the command line shell acts the same way) this has a side-effect: when you open a DOS-type program (like the one you're trying to make) by double-clicking on it, it'll close the window the moment it's done doing its thing. The fact that it closes right after you open it isn't a bad thing, it's a sign that you're programming it properly. Try going Start>programs>accessories>command prompt, use DOS commands to navigate to the directory with your program, and run it from there. It shouldn't close the window when it's over with.
Run your console program from the console instead of from the IDE.
Run your console program from the console instead of from the IDE. <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
Yeah - I made a shortcut to my scripts directory simply for that reason. I've done shortcuts for C++ and Perl by editing MS-DOS shortcuts to open in my scripts directories.
I've also only just started the C++ version of Hello World too! <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin.gif' border='0' style='vertical-align:middle' alt='biggrin.gif'><!--endemo-->
<cough> GUI <cough>
something else. Damn my gold-fish type memory! <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
When I write console based stuff I usually leave a console window open and run it from that. But what you're looking for would probably require <conio.h>. Note that it's NOT part of the ANSII C++ standard, in case you were thinking of getting used to using it and then expect to be able to port it (but you're learning, so ignore that <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif'><!--endemo-->).
You could have a "press enter to continue" scenario quite easily, if you don't want to use conio.h:
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
#include <iostream.h>
void main()
{
//blah blah blah
char cPressEnter;
cout << "Press enter to continue" << endl;
cin >> cPressEnter;
}
<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
Although to stick with the C++ Standard the code would be this:
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->#include <iostream>
int main()
{
cout << "H3110 W0R1D" << endl;
char x;
cout << "Press Enter to Exit";
cin >> x;
return 0;
}<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
The reason why this code is "better" is not required for you to know, but here it is if you're intersted: While having main() as void or int compiles, its the official C++ standard to have main() return an int, and so all your recent books (and more importantly, school classes) are going to do it this way. You return 0 at the end signifying the code ended with no error. Also, the included header file does not have the .h extension for standard libraries, although again putting the .h is taken by the compiler too.
Oh and Talesin, you awnry linux zealot you, IDE is the correct usage of the term here. :P
Thanks for all the answers, i'm getting the hang of it now.
I'm alittle confused about the new line code. I've browsed
multiple " Hello World" tutorials around the net and some
seem to use "\n" and others use "endl;". I think i'll stick
to running the apps from the console and sticking to what
the book says until I have a firmer grasp on things.
To answer Mullet's question.... I have 4 <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo-->
Sam's Teach Yourself C++ in 21 Days
C Programming In Easy Steps
Visual Basic In Easy Steps
Sam's Teach Yourself Borland C++ Builder 3 (out of date I think)
I've done a fair bit in Visual Basic because I liked the fast results.
But i eventually realised it wasn't what I wanted to do.
So i'm sticking to the Sam's Teach Yourself C++ in 21 Days book.
Don't want to confuse myself with 4 different books, each with different
code for the same program do I? <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin.gif' border='0' style='vertical-align:middle' alt='biggrin.gif'><!--endemo-->
The very book I've started learning from! And I've found the answer to your problem:
In your code, just before the "return 0;" line, place this as a line above it:
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->system("PAUSE");<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
That way you can run the .exe and it will not close itself until you hit any key. And if you don't know what key I mean by "any key" I mean this one:
<img src='http://internet.ls-la.net/pictures/images/Computer/The-Any-key.jpg' border='0' alt='user posted image'> <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin.gif' border='0' style='vertical-align:middle' alt='biggrin.gif'><!--endemo-->
endl is the fancy schmancy C++ way of doing things, using the stream classes. If you really, really wanted to,
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->cout << "Hello, world!\n";<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
should work the same as
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->cout << "Hellow, world!" << endl;<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
(I've just tested it, and it's identical for me).
There's no difference, but it's probably better to do it the c++ way. Using escape sequences is the c way. I couldn't tell you exactly why, though.
[edit]On the subject of books, I learned from "C++ In Plain English". The problem with "Teach yourself X in 21 days" books is that they only *last* you 21 days (I've got one on Direct X.) Whatever floats your boat. C++ doesn't make for a very good first language, though.[/edit]
[edit again]Except for the fact that you've already bought books and decided on a language to start with, Java isn't so bad as a first language. It's kinda like c++ with all the dangerous bits removed. It makes it less fun, but easier to learn. As much as I dislike VB, any one of the BASIC languages (Visual Basic, QBASIC, any of the hundred other incarnations) are very good languages for newbies. They don't teach you any good habits, but they do teach you the basics of programming. If you can stomach c++ from scratch, that's even better <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin.gif' border='0' style='vertical-align:middle' alt='biggrin.gif'><!--endemo-->[/edit again]
As I'd read it, looked like he'd compiled it and was running it from the GUI. Sure, if he's compiling on the fly from the IDE then that's correct. But in any case, it's much simpler to just run stuff from the console and ignore the pointy-clicky.
And DY357LX, any of those pause methods are fine.. just never include the line
<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
cout << smoke.blue.magic(*void_ptr);
<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
Damn shite book teaching us C-style habits! <!--emo&:angry:--><img src='http://www.unknownworlds.com/forums/html/emoticons/mad.gif' border='0' style='vertical-align:middle' alt='mad.gif'><!--endemo-->
As for the 21 days thing - surely it gives you a solid enough foundation to then go on to fully immerse yourself in C++?
<b>EDIT:</b> I've just looked on Google for C++ character constants that equate to & update the old-style backslashed escape sequences, but I can only find <i>endl;</i> as an example. Could you please help me find the others as I really want to program C++ with C++ habits. <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif'><!--endemo-->
Pretty please? <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html/emoticons/sad.gif' border='0' style='vertical-align:middle' alt='sad.gif'><!--endemo-->
I've been told countless times that c++ is a difficult language to learn
but i feel that i'm a fast(ish) learner and I'm more than willing to constantly
type out an 800 page book until the info sticks in my head and I understand
it. <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif'><!--endemo-->
I was originally accepted to do a 2 year course. The full title was
"National Diploma In I.T Practitoners" (sp?, it's too hot today!)
But the course was going to last 2 years, cost me almost ?400
and eat up my evenings. The ?400 bit, is now being saved for
new hard-ware for September 30th. coughHALF-LIFE 2cough.
My evenings are spent at friends, reading these forums or doing
some gaming with the <a href='http://www.lowstress.co.uk' target='_blank'>[=LS=)</a> people.
So when I downloaded the <a href='http://www.serverquery.com' target='_blank'>Server Query</a> source code and
tried to make sense of it, I decided I still wanted to learn to code and
after asking around a few forums (this one included, many moons ago!)
i was told that books were the next best bet.
But the problem with books is, you have no-one to turn to when you get stuck.
You can ask for advice and answers on forums like this but you often get
swamped with P.M's and several different answers to the one simple question <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo-->
Now its back to my book for me. Page 26, "A Brief Look at cout" <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin.gif' border='0' style='vertical-align:middle' alt='biggrin.gif'><!--endemo-->
Keep the P.M's coming, its nice to know i'm not the only one struggling to
get a grip with complex stuff like this. Keep the answers, responses and
comments coming because this has some-how turned into an interesting
little topic (if you have interests in coding etc anyway).
Apologies if there's any spelling mistakes or poor grammer, its so damn hot today!
just remember <iostream.h>!
its against the standard to put the .h in standard header files, I'm only harping this cause when I took my C++ classes in college I kept doing that and the professor kept counting it wrong. heh
Pretty please? <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html/emoticons/sad.gif' border='0' style='vertical-align:middle' alt='sad.gif'><!--endemo--> <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
*shrug* I don't know of any others. There aren't really any others which you actually need, other than maybe tab (which will probably work if you simple hit your tab key) and backslash, which you might as well just type '\\' for, anyway. I've never used any of the other escape sequences.
Hey, whatever works. I'm not a purist.