Stupid C++ Question

DY357LXDY357LX Playing since day 1. Still can't Comm.England Join Date: 2002-10-27 Member: 1651Members, Constellation
edited July 2003 in Off-Topic
<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-->
«1

Comments

  • DY357LXDY357LX Playing since day 1. Still can&#39;t Comm. England Join Date: 2002-10-27 Member: 1651Members, Constellation
    I'm sure i should be using a cout "Press Any Key To Continue..." and
    something else. Damn my gold-fish type memory!
  • yoda_143yoda_143 Join Date: 2003-07-10 Member: 18074Members
    edited July 2003
    you need the equivalen to keyget, can't remember either what it's called in c++

    edit: maybe it even is keyget ?
  • DY357LXDY357LX Playing since day 1. Still can&#39;t Comm. England Join Date: 2002-10-27 Member: 1651Members, Constellation
    getch maybe? I'm sure its something similar.
  • yoda_143yoda_143 Join Date: 2003-07-10 Member: 18074Members
    yeah, getchar sounds right
  • DY357LXDY357LX Playing since day 1. Still can&#39;t Comm. England Join Date: 2002-10-27 Member: 1651Members, Constellation
    <!--QuoteBegin--yoda_143+Jul 11 2003, 10:22 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (yoda_143 @ Jul 11 2003, 10:22 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> yeah, getchar sounds right <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    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!!!!!!!!
  • yoda_143yoda_143 Join Date: 2003-07-10 Member: 18074Members
    I am pretty sure
    getchar();
    should work
  • InfinityXInfinityX Join Date: 2003-04-07 Member: 15275Members
    You haven't included the right header files

    <!--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.
  • Marik_SteeleMarik_Steele To rule in hell... Join Date: 2002-11-20 Member: 9466Members
    The "main" function's return type of "int" and the "return 0" all have a purpose. In theory, you can get away with making this and many other programs in C++ without it, and they'll run just fine. What "return 0" does is it tells the computer "Okay, I'm really done now." It's particularly important for batch files, which can be thought of as scripts or shortcuts that run multiple related programs in succession, sometimes using the same files for saved data and passing them on; the computer often <i>needs</i> to know for certain that one program is done before the next can start.

    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.
  • InfinityXInfinityX Join Date: 2003-04-07 Member: 15275Members
    edited July 2003
    Edit : Good god I am stupid <!--emo&???--><img src='http://www.unknownworlds.com/forums/html/emoticons/confused.gif' border='0' style='vertical-align:middle' alt='confused.gif'><!--endemo-->
  • p4Samwisep4Samwise Join Date: 2002-12-15 Member: 10831Members
    What Marik said, but I'll say it quicker:

    Run your console program from the console instead of from the IDE.
  • Brave_UlyssesBrave_Ulysses Join Date: 2003-04-28 Member: 15922Members
    <!--QuoteBegin--[p4]Samwise+Jul 11 2003, 05:56 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> ([p4]Samwise @ Jul 11 2003, 05:56 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> What Marik said, but I'll say it quicker:

    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-->
  • TalesinTalesin Our own little well of hate Join Date: 2002-11-08 Member: 7710NS1 Playtester, Forum Moderators
    edited July 2003
    <!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Run your console program from the console instead of from the IDE.<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    <cough> GUI <cough>
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    <!--QuoteBegin--DY357LX+Jul 12 2003, 06:14 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (DY357LX @ Jul 12 2003, 06:14 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> I'm sure i should be using a cout "Press Any Key To Continue..." and
    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-->
  • DOOManiacDOOManiac Worst. Critic. Ever. Join Date: 2002-04-17 Member: 462Members, NS1 Playtester
    edited July 2003
    Whoa whoa whoa, InfinityX's answer is WAY WAY overkill, and too complicated for a beginner. Go with SoulSkorpion's answer.

    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
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    Yeah, the business about the .h is pretty much optional. It's so that they can do fiddly things with the header files, like not necessarily have them in just one file. I always add the .h because I can never remember which standard library files are just that without the .h or whether they add other fiddlies.
  • MulletMullet Join Date: 2003-04-28 Member: 15910Members, Constellation
    What book is this that you are learning from, DY357LX? (I'm kind of interested) <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo-->
  • DY357LXDY357LX Playing since day 1. Still can&#39;t Comm. England Join Date: 2002-10-27 Member: 1651Members, Constellation
    <!--QuoteBegin--Mullet+Jul 12 2003, 05:29 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Mullet @ Jul 12 2003, 05:29 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> What book is this that you are learning from, DY357LX? (I'm kind of interested) <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo--> <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    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-->
  • InfinityXInfinityX Join Date: 2003-04-07 Member: 15275Members
    <a href='http://www.juicystudio.com' target='_blank'>This site</a> is probably the best developer site i've found. Loads of tutorials and a very good forum too. Check it out.
  • Brave_UlyssesBrave_Ulysses Join Date: 2003-04-28 Member: 15922Members
    <!--QuoteBegin--DY357LX+Jul 12 2003, 09:14 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (DY357LX @ Jul 12 2003, 09:14 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Sam's Teach Yourself C++ in 21 Days <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    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-->
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    edited July 2003
    '\n' is what's called an escape sequence. It corresponds to an ASCII or two (depends on implementation, usually 13 (carriage return) and\or 10 (line feed) ). In other words, backslash something is an escape sequence; an escape sequence is a something which specifies a non-printable character (such as a newline. You can't have a newline in a string constant.) Other escape sequences are '\t' for a tab, '\b' for backspace, and '\\' for if you actually want to print a backslash.

    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]
  • TalesinTalesin Our own little well of hate Join Date: 2002-11-08 Member: 7710NS1 Playtester, Forum Moderators
    <!--QuoteBegin--DOOManiac+Jul 11 2003, 07:24 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (DOOManiac @ Jul 11 2003, 07:24 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Oh and Talesin, you awnry linux zealot you, IDE is the correct usage of the term here. <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo--> <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    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-->
  • Brave_UlyssesBrave_Ulysses Join Date: 2003-04-28 Member: 15922Members
    edited July 2003
    <!--QuoteBegin--SoulSkorpion+Jul 12 2003, 10:44 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SoulSkorpion @ Jul 12 2003, 10:44 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> There's no difference, but it's probably better to do it the c++ way. Using escape sequences is the c way. <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    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-->
  • DY357LXDY357LX Playing since day 1. Still can&#39;t Comm. England Join Date: 2002-10-27 Member: 1651Members, Constellation
    edited July 2003
    Wow I never knew there were so many smart-***'s on this forum! <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin.gif' border='0' style='vertical-align:middle' alt='biggrin.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!
  • JammerJammer Join Date: 2002-06-03 Member: 728Members, Constellation
    Streams are fun :-)
    just remember <iostream.h>!
  • DOOManiacDOOManiac Worst. Critic. Ever. Join Date: 2002-04-17 Member: 462Members, NS1 Playtester
    <iostream>

    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
  • DY357LXDY357LX Playing since day 1. Still can&#39;t Comm. England Join Date: 2002-10-27 Member: 1651Members, Constellation
    <b>I</b>nput <b>O</b>utput <b>Stream</b> hey look mar, I'm learnding! Heh
  • Brave_UlyssesBrave_Ulysses Join Date: 2003-04-28 Member: 15922Members
    Yep - the .h extension = redundant in c++. Yay! <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin.gif' border='0' style='vertical-align:middle' alt='biggrin.gif'><!--endemo-->
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    <!--QuoteBegin--Brave Ulysses+Jul 13 2003, 12:08 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Brave Ulysses @ Jul 13 2003, 12:08 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <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--> <!--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.
  • RevrenRevren Join Date: 2003-05-21 Member: 16585Members
    What's this?!!? You forgot that all mighty \a? How will people manage to make anoying beeping sounds without it..... <img src='http://www.unknownworlds.com/forums/style_images/2/icon5.gif' border='0' alt='user posted image'>
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    Yes, he asked for alternatives to the escape sequences, not the escape sequences themselves <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo-->
Sign In or Register to comment.