Public Group

Archived

This topic is now archived and is closed to further replies.

c++ newbie question

This topic is 5709 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

ok when ever I write a program in either vc or dev I get the same end result (almost anyways dev those the same thing in the program (program= .exe) as the actual program does, but vc will run my program completely 100% normal, but the .exe does the same as in dev). It seem my program run fine until the very last step. Ex 1
quote:
I could writte a very simple program like this #include <iostream.h> int main() { cout << "You Should beable to read this \n\n"; return 0; }
The .exe of this would close and I wouldn''t even get he chance to see nothing (don’t forget IN VC I can get it to work normally) In this program
quote:
#include <iostream.h> int main() { cout << "You Should be able to read this \n\n"; int nReg; cout << "Enter a number to be displayed: "; cin >> nReg; cout << "\nYou''ve typed in" << nReg; return 0; }
the .exe will display the "you should..." But once I enter my value, boom the program gone. Anyone know what wrong, I know c++ has changed (eg #include <iostream> int main() std::cout (ect)) but I get the same result.

Share on other sites
Try using endl after "You should be able to read this" instead of newline.

Share on other sites
Your IDE is probably opening a console, which closes as soon as the program terminates. Have you tried running the .exe from a regular console prompt?

Another solution, commonly used, is to simply prompt the user for input at the end of the program; something along the lines of "Press a key to continue" and a getch() or system("PAUSE").

Share on other sites
quote:
Original post by wild_pointer
Try using endl after "You should be able to read this" instead of newline.

That''s also an issue. Since cout is buffered, you will need to flush the buffer to be absolutely certain that it''s actually going to appear. cout << std::endl will insert a line break and flush the buffer; cout << std::flush will simply flush the buffer.

Share on other sites
hmm... I took of newbie at the top and forgot to put it at the bottom...

Im a newbie. I dont know to much in C++ so dont get to complex.

Share on other sites
doulble post sry

[edited by - zeratulsdomain on December 4, 2002 2:10:39 AM]

Share on other sites
Zerat,

The problem that you''re encountering isn''t so much of a C++ problem as an Operating System problem.

Visual Studio creates its own environment to execute console programs in, that''s why you can see your program run when you do it inside of the IDE.

On the other hand, Windows doesnt really care about console programs anymore. What''s actually happening is Windows is creating a console window to output your program in and then runs the program. As soon as the program finished it closes the window.

One way to solve this is to load up a command line environment and execute your program from within there.

To load up a command line, simply go to your start menu, click run, and then type "cmd". This will load up a console window from which you can run your program without it closing.

Now, you''ll soon realize that when you do this you have to navigate to your project executable...so here''s what you do:

Go to the directory where the .exe is located and create a shortcut. When it asks for the location of the command, just type "cmd.exe /K " and hit next. Then hit finish. Now right click on the shortcut and where it says "start in:", delete the contents of the line. Now whenever you click on that shortcut it will load a console for you, execute your program and remain open.

Best Regards,

Jeromy Walsh
Programmer
Liquid Entertainment
------------------------
"The question isn''t how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" -Boondock Saints

Share on other sites
Well, it's quite simple.

    #include <conio.h>#include <iostream>int main(void){  std::cout << "You should be able to read this." << std::endl;  std::cout << "Press a key to continue" << std::endl;  getch(); // Wait for input!  /* Once the program terminates, the console window will close,  if it was merely openedby your IDE (such as VS). This is why  we ask for user input and -wait-, thereby not closing until  the user had a chance to read what was on the screen. */}

[edited by - Miserable on December 4, 2002 2:16:58 AM]

Share on other sites
Ok heres what happens.
VC runs the program in whats called a console window.
That will automatically terminate(go away/die) whe the programs done running.

So what you do is flush the buffer by uing the endl specifier, and then have a loop where you wait for keyboard input.

Flush the buffer means:

cout maintains a storage of characters called a buffer.
Your cout << statements just add to this bunch of data. :D
When it feels like it, it writes these to the screen.
Pushing all of this data to the output device(aka monitor in this case) is called flushing.

So you flush the buffer, then run a loop like so

while(!kbhit()){}

Which means, while the keyboard is NOT hit, loop.
You use conio.h for kbhit().
Have fun !!!

Bugle4d

Share on other sites
Zerat,

You do not want to modify your code to make it stay open. It requires the inclusion of an extra header file to use either kbhit or getchar. You could do it with cin, but really, you dont want to get in the habit of modifying code to make the OS like it.

The problem you''re experiencing isn''t with your code, it''s with the way Windows now treats console applications. In the future you''ll be dealing with Windows applications rather than console applications, and you''ll even be porting your programs to other operating systems. Although that''s not the case now, dont short change yourself by picking up a habit you''ll need to change later.

Best Regards,

Jeromy Walsh
Programmer
Liquid Entertainment
------------------------
"The question isn''t how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" -Boondock Saints

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
17
5. 5

• 14
• 30
• 13
• 11
• 11
• Forum Statistics

• Total Topics
631774
• Total Posts
3002297
×