![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 4
Rep Power: 0
![]() |
New to C, need some help
Hi there folks,
I'm new to C, as the title implies. Anyways, Kyle pointed me onto Dev-C++ and I am having some problems with it. I hope you can help me out here. I start a new project > Console Application > C programming language. I set up my source like so, according to the tutorial in the book I am reading: #include <stdio.h>
int radius, area;
int main(){
printf("Enter radius: ");
scanf("%d", &radius);
area = (int) (3.14159 * radius * radius);
printf("\n\nArea = %d\n", area);
return 0;
}Now when my friend compiles this on his comp with his Visual Studio 6, it works like a charm. However, when I compile and run, when I enter a number for the radius and hit enter, it closes the window. Likewise, even on simply "hello world" programs, it will also show the text for an instant (barely even enough to read) and closes the window. Any ideas of why this is happening with Dev-C++? Is there some sort of setting that I have accidentally altered and must change back? Thanks for your time. |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
You need to add a puase, or a simple getchar() function after your final printf().
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jul 2005
Posts: 4
Rep Power: 0
![]() |
I am afraid I understand neither. Would you elaborate please?
I mean, I understand what you mean by pause, but I do not know the command. As well, I do not know how to utilize the getchar() command, either. Thanks again. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
This is in another thread, just as of today. Maybe we need to start a FAQ....
Programs run until they finish, then they go away. If you run a console program from the IDE or by double clicking on the icon, it will run until it's done, then go away and disappear. If you put a statement that requires user input at the bottom, it will not FINISH until you give it that input....consequently, it will not go away before you have a chance to see its handiwork. Second scenario: start up a Command Prompt window. Issue a command to run the program (change to the proper directory). The program will run, output its stuff, and go away. The command prompt window will not go away, however, because it is its OWN program. The output from your program will still be in that existing screen. Don't utilize the pause or getchar thangys. USE them, instead. It's shorter .system (.....) spawns a command shell and runs whatever command is in the parentheses. That might be "pause" or "PAUSE" if you want to pause, but it varies from platform to platform. getchar () is a portable function that, well, ummmm, gets a char..... That will make the program wait for input until it sees some, plus an ENTER.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jul 2005
Posts: 4
Rep Power: 0
![]() |
Ahh, ok, the system("PAUSE"); worked like a charm, but you are saying this only works on certain systems.
So, in that case, what would I has to put between the getchar() parentheses? I tried "k", since I assumed that you would have to punch in a K before it moved along. Unfortunately, this did not work. Please excuse my elementary questions... I'm trying! :o |
|
|
|
|
|
#6 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Just getchar() should be sufficient, without anything in the parenthesis.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#7 |
|
Programming Guru
![]() ![]() ![]() |
Yep. But if you are not intending to take your program over to a different operating system, the system ("PAUSE") will work find in Windows.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The thing about PAUSE is that it will issue a message saying something like, "Press any key to continue..." then wait for any key to be pressed. When you use something like "getchar ()" the user will have to use ENTER (eventually, if not as the first key) before input is effective. Any other input mechanism which responds to keystrokes individually (no ENTER required) is non-portable, also. One picks and chooses.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Jul 2005
Posts: 4
Rep Power: 0
![]() |
Right on, thanks for the help. You all really cleared a lot up for me, and for that I am grateful.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|