Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 21st, 2005, 12:28 AM   #1
jayson
Newbie
 
Join Date: Jul 2005
Posts: 4
Rep Power: 0 jayson is on a distinguished road
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.
jayson is offline   Reply With Quote
Old Jul 21st, 2005, 12:42 AM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
You need to add a puase, or a simple getchar() function after your final printf().
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Jul 21st, 2005, 12:47 AM   #3
jayson
Newbie
 
Join Date: Jul 2005
Posts: 4
Rep Power: 0 jayson is on a distinguished road
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.
jayson is offline   Reply With Quote
Old Jul 21st, 2005, 1:02 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 21st, 2005, 1:15 AM   #5
jayson
Newbie
 
Join Date: Jul 2005
Posts: 4
Rep Power: 0 jayson is on a distinguished road
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
jayson is offline   Reply With Quote
Old Jul 21st, 2005, 1:45 AM   #6
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Just getchar() should be sufficient, without anything in the parenthesis.
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Jul 21st, 2005, 9:07 AM   #7
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Jul 21st, 2005, 9:46 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 21st, 2005, 1:31 PM   #9
jayson
Newbie
 
Join Date: Jul 2005
Posts: 4
Rep Power: 0 jayson is on a distinguished road
Right on, thanks for the help. You all really cleared a lot up for me, and for that I am grateful.
jayson is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC