A problem with this?
Hey everyone, when i put this in and then open it up it works fine, but then when i put in a number and press enter it shuts itself off, any idea why it would do that? any way i can fix it?
is this a common problem? thanks.
/* Program 2.8 calculations on a table */
#include <stdio.h>
#include <stdlib.h>
void main()
{
float radius = 0.0f;
float diameter = 0.0f;
float circumference = 0.0f;
float area = 0.0f;
float Pi = 3.14159265f;
printf("Input the diameter of the table:");
scanf("%f", diameter);
radius = diameter/2.0f;
circumference = 2.0f*Pi*radius;
area = Pi*radius*radius;
printf("\nThe circumference is %.2f", circumference);
printf("\nThe area is %.2f\n", area);
}
Put getch() at the end. While you''re at it, learn to use C++ as there is no reason at all to use plain C.
erm im using C becuase i got the game programming guru book (lots of people suggested it) but i need to know C to use it. So I''m going to learn C, then learn C++, then read the book so i have a basic knowledge of both before i start. Oh and i prolly should have mentioned that it gives me a windows error, not shuts down and it still does it.
quote:Original post by Talvern
Oh and i prolly should have mentioned that it gives me a windows error, not shuts down and it still does it.
Care to tell us what that error is?
Although from the looks of it, I think I know what''s wrong. Try...
scanf("%f", &diameter);
scanf() requires pointers to work with.
Hey everyone, when i put this in and then open it up it works fine, but then when i put in a number and press enter it shuts itself off, any idea why it would do that? any way i can fix it?
a) Because console applications are intended to be run in a pre-existing console (a.k.a "DOS commnd prompt"). If you don''t have a console when the program starts up, the C run-time creates one but, as with every other resource, it must be destroyed when the program terminates.
b) Make a foo.bat file (or equivalent) and execute it instead of directly calling your executable:
foo.exe
pause
a) Because console applications are intended to be run in a pre-existing console (a.k.a "DOS commnd prompt"). If you don''t have a console when the program starts up, the C run-time creates one but, as with every other resource, it must be destroyed when the program terminates.
b) Make a foo.bat file (or equivalent) and execute it instead of directly calling your executable:
foo.exe
pause
quote:Original post by RuneLancer
Care to tell us what that error is?
Although from the looks of it, I think I know what''s wrong. Try...
scanf("%f", &diameter);
scanf() requires pointers to work with.
Heh yeah, I found that out last night and didnt bother coming on the forums after that, thanks for the help though =D
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement