Newbie Question (Hello World)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2005
Posts: 4
Reputation: ehsen is an unknown quantity at this point 
Solved Threads: 0
ehsen ehsen is offline Offline
Newbie Poster

Newbie Question (Hello World)

 
0
  #1
Sep 3rd, 2007
I started to learn C++ few days ago. When I Run a program it show a console window for a moment then return to IDE. I even couldn't see the output.
for example followig Hello World Program

  1. // a small C++ program
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. cout << "Hello, world!" << endl;
  7.  
  8. }

what should I add in this code (infact every code) to stop this problem. I tried to fix it myself by adding cin.ignore(); at the end of the program. But this doesn't workout where program requires the input from user.
Last edited by ehsen; Sep 3rd, 2007 at 2:56 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: Newbie Question (Hello World)

 
0
  #2
Sep 3rd, 2007
i am not in front of windows, so i can't test the advise::

try inserting system("pause"); and in the beginning <include> <cstdlib>

-i am not sure about which library to include...i am sure about the system command

another way would be to declare a dummy variable and read it at the end of the programm....
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 120
Reputation: ft3ssgeek is an unknown quantity at this point 
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: Newbie Question (Hello World)

 
0
  #3
Sep 3rd, 2007
I would not recommend using
  1. system("pause");
as it is not portable between various operating systems...try something like
  1. cin.get();
at the end instead...
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Newbie Question (Hello World)

 
0
  #4
Sep 3rd, 2007
I tried to fix it myself by adding cin.ignore(); at the end of the program. But this doesn't workout where program requires the input from user.
It doesn't work because there's still something in the stream and ignore gets it right away. You should clear the stream first then do ignore or get.
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void BlockPause() {
  6. // Set the state so cin can read.
  7. cin.clear();
  8.  
  9. // Clear everything in the stream
  10. cin.sync();
  11.  
  12. // Do the pause
  13. cin.ignore();
  14. }
  15.  
  16. int main() {
  17. // All of your other code goes here
  18.  
  19. // Pause at the end with BlockPause
  20. cout<<"Press enter to continue...";
  21. BlockPause();
  22.  
  23. return 0;
  24. }
cin.clear() makes the stream state good. If it's not good, cin won't read anything ever. cin.sync() clears all of the characters left in the stream so that the next cin.ignore() or cin.get() stops the program waiting for input.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 33
Reputation: fzafarani is an unknown quantity at this point 
Solved Threads: 1
fzafarani's Avatar
fzafarani fzafarani is offline Offline
Light Poster

Re: Newbie Question (Hello World)

 
0
  #5
Sep 3rd, 2007
You should use
getch();
at the end!

#include <stdio.h>
#include <conio.h>
#include <iostream.h>

int main()
{
cout << "Hello world!";
getch();
return 0;
}

or press alt+F5 at the end of program.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Newbie Question (Hello World)

 
0
  #6
Sep 3rd, 2007
Originally Posted by fzafarani View Post
You should use
getch();
at the end!
No you really shouldn't. It's non-portable.

Better would be to learn how to execute a command-line program from the command line.

And please use [code][/code] tags.
Last edited by Dave Sinkula; Sep 3rd, 2007 at 4:03 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1
Reputation: sd.lamba is an unknown quantity at this point 
Solved Threads: 0
sd.lamba sd.lamba is offline Offline
Newbie Poster

Re: Newbie Question (Hello World)

 
0
  #7
Sep 4th, 2007
add
#include<iostream.h>
#include<conio.h>
int main()
{
cout<<"hello world";
getch(); //is used to hold the o/p screen
return 0; //will return the value to O.S.
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC