C compiler help (beginner question)

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

Join Date: Nov 2004
Posts: 3
Reputation: Moo The Cow is an unknown quantity at this point 
Solved Threads: 0
Moo The Cow Moo The Cow is offline Offline
Newbie Poster

C compiler help (beginner question)

 
0
  #1
Nov 24th, 2004
Hi, this is the first time that I've used C. Anyway, I downloaded a C compiler (dev-C++), and I typed in this:

#include <stdio.h>

main()
{
printf("Hello, world!\n");
return 0;
}

When I run the program, it doesn't print the "Hello, world!", but I just see a dos window quickly flash on my screen. What (and where) should I add to this code to prevent this?
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,040
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 125
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: C compiler help (beginner question)

 
0
  #2
Nov 24th, 2004
First of all, change your main() line to be int main()

The problem that you are having is that the program opens, does what it's supposed to do, and then closes. At the end of the program, right above the return 0 line, try entering:

char x;
cin >> x;

What that will do is prompt for a character to be input right before the program closes. Therefore, the program won't automatically close until it gets user input.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,348
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: C compiler help (beginner question)

 
0
  #3
Nov 24th, 2004
Originally Posted by Moo The Cow
When I run the program, it doesn't print the "Hello, world!", but I just see a dos window quickly flash on my screen. What (and where) should I add to this code to prevent this?
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 3
Reputation: Moo The Cow is an unknown quantity at this point 
Solved Threads: 0
Moo The Cow Moo The Cow is offline Offline
Newbie Poster

Re: C compiler help (beginner question)

 
0
  #4
Nov 25th, 2004
Originally Posted by cscgal
First of all, change your main() line to be int main()

At the end of the program, right above the return 0 line, try entering:

char x;
cin >> x;

What that will do is prompt for a character to be input right before the program closes. Therefore, the program won't automatically close until it gets user input.
I tried the char x; suggestion, but it still doesn't work . When I tried typing in "cin >> x;", I got an error saying that 'cin' is undeclared.

Dave's suggestion to look into the FAQ didn't work either. The faq says to type
system("PAUSE");
before the return 0 line, but I get an error that says:
implicit declaration of function `int printf(...)'
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,348
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: C compiler help (beginner question)

 
0
  #5
Nov 25th, 2004
Originally Posted by Moo The Cow
I tried the char x; suggestion, but it still doesn't work . When I tried typing in "cin >> x;", I got an error saying that 'cin' is undeclared.

Dave's suggestion to look into the FAQ didn't work either. The faq says to type
system("PAUSE");
before the return 0 line, but I get an error that says:
implicit declaration of function `int printf(...)'
Uh, try reading it again and note the differences from what you have.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 3
Reputation: Moo The Cow is an unknown quantity at this point 
Solved Threads: 0
Moo The Cow Moo The Cow is offline Offline
Newbie Poster

Re: C compiler help (beginner question)

 
0
  #6
Nov 25th, 2004
Originally Posted by Dave Sinkula
Uh, try reading it again and note the differences from what you have.
Oops, I now realize that I was somehow looking at the wrong FAQ. Oh well, it works now.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,009
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 929
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: C compiler help (beginner question)

 
0
  #7
Nov 25th, 2004
Since you are writing a C program use getchar() to wait.
  1. // you can also use just #include <iostream>
  2. // this works in DevCpp because iostream chain-includes down
  3. // to sdtio.h (not recommended for portable code)
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9. printf("Hello, world!\n");
  10. getchar(); // wait
  11. return 0;
  12. }
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 68
Reputation: harshchandra is an unknown quantity at this point 
Solved Threads: 1
harshchandra harshchandra is offline Offline
Junior Poster in Training

Re: C compiler help (beginner question)

 
0
  #8
Nov 26th, 2004
u can use getch() at the end of the code for the screen to be still till u press any key.Or else u can use alt+F5 key to see the output window.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC