I need you guys!...again

I'm writing a program for calculating the circumference of a circle being entered by the user....only problem is...hypergrade won't accept it. I'll pot a screenshot.

http://snag.gy/Ocphn.jpg

In the black box it's not matching for some reason.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <stdio.h>
#define Pi 3.1416
int main()

{
	float radius;
	float circumference;
	
	circumference = 2*Pi*radius;
	
	printf("Enter the radius of a circle:\n");
	scanf("%f", radius);
	printf("\nCircumference of circle:  %f6.2", circumference);
	
	return 0;
}
Last edited on
closed account (3hM2Nwbp)
Looks as though you have an extra '\n' in there on either line 11 or 13.
Maybe it is the extra newline on line 13.
Also, another question....every time I compile and run and enter in a value my program crashes...any suggestions?
@luc, I just tried taking the \n off, both, one from 11 or from 13. All 3 didn't work.
closed account (3hM2Nwbp)
You have to pass scanf a pointer to 'radius'. Missed that on first glance.

 
scanf("%f", &radius);


* Otherwise, scanf will attempt to write to (probably) invalid memory and will likely crash the program
Last edited on
@luc

its like you know what you're doing! hahaha

=
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#define Pi 3.1416
int main()

{
	float radius;
	float circumference;
	
	circumference = 2*Pi*radius;
	
	printf("Enter the radius of a circle:\n");
	scanf("%f", &radius);
	printf("Circumference of circle:  %.2f", circumference);
	
	return 0;
}


This is what I have, it worked earlier without the &radius, now it just gives me 0s.
Have you tried stepping through your programming manually and seeing what variables are at each line?
@Kevin

on line 9, you calc circumference before you have the radius! You should get into the habit of initialising your variables to something at declaration, zero is not always the best option.

Also, scanf returns an int which is the number of valid input read. You should make use of this in order to see if it worked. There are a variety ways of doing this depending on how many values are being read. You could use a while for 1 value, a switch for several values.

HTH

Edit:
You can still use double with the %f in scanf & printf

Edit 2:
Rather than use #define , have a const double in main()
Last edited on
@IdeasMan,

you are my knight in shining armor. Thank you so much! This is my second week ever programming so I'm bound to mess up on obvious things. Thank you for being patient!

Edit: ill try to get into the habit of using double and initializing my variables.
Last edited on
No worries, pleased to help :+)

Also, there might be a M_PI already in the math.h, but this is not standard.
Topic archived. No new replies allowed.