| | |
C compiler help (beginner question)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 3
Reputation:
Solved Threads: 0
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?
#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?
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.
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.
•
•
•
•
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?
•
•
Join Date: Nov 2004
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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.
. 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(...)'
•
•
•
•
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(...)'
Since you are writing a C program use getchar() to wait.
C++ Syntax (Toggle Plain Text)
// you can also use just #include <iostream> // this works in DevCpp because iostream chain-includes down // to sdtio.h (not recommended for portable code) #include <stdio.h> int main() { printf("Hello, world!\n"); getchar(); // wait return 0; }
May 'the Google' be with you!
![]() |
Similar Threads
- C++ beginner's question (C++)
- Im new (C++)
Other Threads in the C++ Forum
- Previous Thread: Quit abruptly in MFC app
- Next Thread: c++ digit code
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets







