Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 11-08-2005, 05:29 AM   #1
Daemon Poster
 
george's Avatar
 
Join Date: Mar 2005
Posts: 553
Send a message via MSN to george Send a message via Yahoo to george
Default C++ not working properly

I'm teaching myself c++ and I did the Hello World program and saved it into a folder and everything got it compiled and then ran it when I did that all it did was flash a Dos text box for a second and then close it. What did I do wrong. Here's the code.


#include <iostream>
using namespace std;

int main ()
{
cout << "Hello World!";
return 0;
}
__________________

george is offline   Reply With Quote
Old 11-08-2005, 07:21 AM   #2
CJ
In Runtime
 
CJ's Avatar
 
Join Date: May 2004
Posts: 399
Default Re: C++ not working properly

you need to add

getchar();

before return 0;

This waits for keyboard input from the user. (Return, I think).
__________________

CJ is offline   Reply With Quote
Old 11-08-2005, 03:33 PM   #3
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,006
Default Re: C++ not working properly

you could also add system(pause);

in reality there is nothing wrong with the program, it runs, gives an output and then finishes, try running it from a dos box and you'll see the output inside that box.
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 11-08-2005, 04:15 PM   #4
CJ
In Runtime
 
CJ's Avatar
 
Join Date: May 2004
Posts: 399
Default Re: C++ not working properly

But that won't let him pass his degree.
CJ is offline   Reply With Quote
Old 11-08-2005, 04:46 PM   #5
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,006
Default Re: C++ not working properly

nor will adding getchar() since thats not a hello world program.

the correct syntax for using get char would be

int main();
{
char x;
cout << "Hello World!";
x = getchar();
return 0;
}

since it scans the keyboard loading the key pressed into variable, (so marks off for not using the function properly,

besides getchar is not needed, as I said in the previous post, the program acualy works perfectly, (it runs) and it finishes. without error...
getchar() or system(pause) simply arn't needed.

marks off for using the function because he simply doesn't need it!

besides which, who said anything about a degree?
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 11-09-2005, 01:48 AM   #6
Daemon Poster
 
george's Avatar
 
Join Date: Mar 2005
Posts: 553
Send a message via MSN to george Send a message via Yahoo to george
Default Re: C++ not working properly

Hey thanks for that I know that the program works but I just wanted to know how to stop it from just flashing on the screen for a second.
george is offline   Reply With Quote
Old 11-09-2005, 02:14 PM   #7
CJ
In Runtime
 
CJ's Avatar
 
Join Date: May 2004
Posts: 399
Default Re: C++ not working properly

...
CJ is offline   Reply With Quote
Old 11-10-2005, 04:26 AM   #8
Daemon Poster
 
george's Avatar
 
Join Date: Mar 2005
Posts: 553
Send a message via MSN to george Send a message via Yahoo to george
Default Re: C++ not working properly

Well thank you very much for that CJ
george is offline   Reply With Quote
Old 11-14-2005, 04:58 PM   #9
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: C++ not working properly

http://computerforums.org/showthread.php?t=6279

Using an input function to pause something is bad practice.

Someone could overflow the buffer and give random results, crash the program, could even crash the system, rewrite to memory it's not suppose to. Big security risk. It's not probable that they could do it to a hello world program cause of the speed of execution is too quick.
__________________
Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
RewtGuy is offline   Reply With Quote
Old 11-30-2005, 12:50 AM   #10
BSOD
 
JustinMcG67's Avatar
 
Join Date: Sep 2005
Posts: 2,519
Send a message via AIM to JustinMcG67 Send a message via MSN to JustinMcG67 Send a message via Yahoo to JustinMcG67
Default Re: C++ not working properly

I never use the #include <iostream>, I use the #include <stdio.h> I should learn this different functions...
__________________

JustinMcG67 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 12:32 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0