944,179 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1761
  • C++ RSS
Sep 3rd, 2007
0

Newbie Question (Hello World)

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ehsen is offline Offline
4 posts
since May 2005
Sep 3rd, 2007
0

Re: Newbie Question (Hello World)

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....
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006
Sep 3rd, 2007
0

Re: Newbie Question (Hello World)

I would not recommend using
C++ Syntax (Toggle Plain Text)
  1. system("pause");
as it is not portable between various operating systems...try something like
C++ Syntax (Toggle Plain Text)
  1. cin.get();
at the end instead...
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Sep 3rd, 2007
0

Re: Newbie Question (Hello World)

Quote ...
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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Sep 3rd, 2007
0

Re: Newbie Question (Hello World)

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
fzafarani is offline Offline
33 posts
since Aug 2007
Sep 3rd, 2007
0

Re: Newbie Question (Hello World)

Click to Expand / Collapse  Quote originally posted by fzafarani ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 4th, 2007
0

Re: Newbie Question (Hello World)

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.
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sd.lamba is offline Offline
1 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: for rookie C++ (template) programmers
Next Thread in C++ Forum Timeline: SIP Gateway





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC