Thread: This multivariable problem seems so simple but I can't figure it out.

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    20

    This multivariable problem seems so simple but I can't figure it out.

    I need to make a program that calculates the area of a circle but I have to use an int, char, and float. No matter what I do though I can't get the second and third variables to output meaningful values, only the first. This is what I have written down so far.
    Code:
    #include <stdio.h>
    #include "conio.h"
    
    #define PI 3.14159
    
    int main()
    
    {
    	/* The following is a program to demonstrate several different c numerical input funtions:interger, float, character.*/
    
    	float radius1, area_float;  //declares the radius that will be used for float
    	int radius, area_int;
    	char radius3, area_char;
    	
    
    	printf("Input radius for float, please? ");  // Promts the user to enter a value for float
    	scanf("%f", &radius1 ); // obtains the value from the float promt
    
    	area_float = PI * radius1 * radius1; //Equation for area of a cricle
    	printf("area1 = %f\n", area_float); //Prints the answer for float
    	 //Forces the command console to pause because c is used.
    
    
    	
    	printf("Input radius for int value, please? ");
    
    	area_int = PI * radius * radius;
    	scanf("%d", &radius );
    	
    	printf("area2 =%d\n", area_int);
    	_getch();
    	
    	
    
    
    	printf("Input radius for char value, please? ");
    	scanf("%c", &radius3);
    	area_char= PI * radius3*radius3;
    	printf("area3 =%c2\n", area_char);
    	_getch();
    	return 0;
    
    	}
    I'm not sure what to do and I've been working on this for hours Thanks for any help or advice!
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    printf("Input radius for int value, please? ");

    area_int = PI * radius * radius;
    scanf("%d", &radius );

    printf("area2 =%d\n", area_int);
    _getch();
    You are calculating area first (the variable area_int has some arbitrary value stored upon declaration and it is this arbitrary value that is used in your calculation) and then you are asking the user to input the value of radius. So the area that you see is actually garbage.

    The order should be reversed as below
    Code:
    printf("Input radius for int value, please? ");
    
    	scanf("%d", &radius );
            area_int = PI * radius * radius;
    	
    	printf("area2 =%d\n", area_int);
    	_getch();
    In the part where you are using char, note that char uses the ascii value of the variable and does NOT read 1 as the number 1. rather it interprets it as the ascii character for 1. Hence the final answer that you see is actually the ascii symbol for the value that is obtained upon calculation of pi*r^2.


    You need to convert the characters to their respective values (in int).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > area_int = PI * radius * radius;
    > scanf("%d", &radius );
    > printf("area2 =%d\n", area_int);
    Should you read the value, before trying to do some maths on it?




    > scanf("%c", &radius3);
    > area_char= PI * radius3*radius3;
    Let's say you type in '4', where the ASCII value is 52.
    So 52 * 52 * 3, will give you some value a lot larger than a char, and will not produce something meaningful when printed as a single char.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    20
    But I don't understand why the int value for the radius still returns nonsense, it returns a small negative number. Not only that but I get an error saying that radius is being used without being initialized.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because you are using radius without initializing it, that is why you get the error saying that you are using radius without initializing it. Don't do that. Initialize first, use later.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by caysonmars View Post
    But I don't understand why the int value for the radius still returns nonsense, it returns a small negative number. Not only that but I get an error saying that radius is being used without being initialized.
    Did you make the changes that were suggested?

    Radius is being use without being intialized because you are using in your calculations it before you load it with any meaningful value.
    Hense, everyone's suggestion that you change the order of those lines.

    C isn't smart --and especially not mind reader smart-- it simply does stuff in the order it finds it.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Not only that but I get an error saying that radius is being used without being initialized.
    You should pay attention to it.

    Consider this
    > area_int = PI * radius * radius; <-- switch on kettle
    > scanf("%d", &radius ); <-- add water to kettle

    Do you understand cause and effect?

    C doesn't do "deferred execution", where you can just list a bunch of instructions in any order, and then add information later on and have it re-calculate all dependencies. If radius doesn't have the right value by the time you hit the calculation, then you're stuffed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    20
    Jeez I can't believe how many simple mistakes I made. I switched the code around so many times I think that's how the area got messed up. This is what I have now, the first two variables actually work! But for some reason the program doesn't allow me to enter in a character. It just displays ASCII instead of prompting me for input after the first two variables are entered. I'm not sure why this is. I erased and retyped the code a couple of times but I can't fix the problem.

    Code:
    #include <stdio.h>
    #include "conio.h"
    
    #define PI 3.14159
    
    int main ()
    {
    	
    	int radius, area;
    	float radius1, area1;
    	char radius2, area2;
    	
    	printf("Input char radius, please?");
    	scanf("%d", &radius );
    	area = PI * radius * radius;
    	printf("Area = %d\n", area);
    	_getch();
    
    	printf ("Input float radius, please?");
    	scanf("%f", &radius1);
    	area1 = PI * radius1 * radius1;
    	printf("Float area= %f\n", area1);
    	_getch();
    
    	printf ("Input Char radius, please?");
    	scanf ("%c", &radius2);
    	area2 = PI * radius2 * radius2;
    	printf("Char Area = %c\n", area2);
    	_getch();	
    
    		
    
    		return 0;
    
    }

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Assuming you don't want enter-key to count as a character, then you need to use " %c". Also, if the area is, let's say 74, then printing 74 as a character gets you J -- you don't get to see 74.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    20
    I did use "%c" after scanf

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by caysonmars View Post
    I did use "%c" after scanf
    Which is why it's wrong. (I.e., read the post again.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multivariable issues
    By thintheherd in forum C Programming
    Replies: 1
    Last Post: 05-31-2010, 11:56 PM
  2. Replies: 19
    Last Post: 09-28-2009, 01:45 AM
  3. C++ simple problem that I can't figure out
    By dvessey in forum C++ Programming
    Replies: 9
    Last Post: 05-25-2008, 10:56 AM
  4. I simple little problem but i cant figure it out
    By SebastionV3 in forum C++ Programming
    Replies: 9
    Last Post: 05-24-2006, 05:55 PM
  5. Simple code :: Cant figure out the problem...
    By pritin in forum C++ Programming
    Replies: 11
    Last Post: 09-02-2005, 04:02 AM