Thread: How do you use float????

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    7

    How do you use float????

    Hi all,

    My professor wants us to perform computations that will result in decimals. I know this sounds stupid, but how do you use the float function? I know thatr I did it wrong.....Any help would be appreciated!

    Code:
    /* Assignment 1 */
    
    #include <stdio.h>
    #include <float.h>
    
    int main()
    {
        /* Menu of Choices */
        int i;
        int j;
            {
            printf("1: cm to in\n");
            printf("2: cm to ft\n");
            printf("3: cm to mi\n");
            printf("4: in to cm\n");
            printf("5: in to ft\n");
            printf("6: in to mi\n");
            printf("7: ft to cm\n");
            printf("8: ft to in\n");
            printf("9: ft to mi\n");
            printf("10: mi to cm\n");
            printf("11: mi to in\n");
            printf("12: mi to ft\n");
            scanf("%d", &i);
            switch (i)
    
            {
                case 1: /* cm to inches */
                printf("Enter quantity: \n");
                scanf("%f", &i);
                j=i/2.4;
                printf("There are %f inches\n", j);
                break;
    
                case 2: /* cm to feet */
                printf("Enter quantity: \n");
                scanf("%f", &i);
                j=i/30.5;
                printf("There are %f ft\n", j);
                break;
    
                case 3: /* cm to mi*/
                printf("Enter quantity: \n");
                scanf("%f", &i);
                j=i/160934.4;
                printf("There are %f mi\n", j);
                break;
    
                case 4: /* inches to cm */
                printf("Enter quantity: \n");
                scanf("%f", &i);    
                j=i*2.4;
                printf("There are %f cm\n", j);
                break;    
    
    
                case 5: /* inches to feet */
                printf("Enter quantity: \n");
                scanf("%f", &i);    
                j=i/12;
                printf("There are %f ft\n", j);
                break;
    
                case 6: /* inches to miles */
                printf("Enter quantity: \n");
                scanf("%f", &i);
                j=i/63360;
                printf("There are %f mi\n", j);
                break;
    
                case 7: /* feet to cm */
                printf("Enter quantity: \n");
                scanf("%f", &i);    
                j=i*30.5;
                printf("There are %f cm\n", j);
                break;
    
                case 8:  /* feet to inches */
                printf("Enter quantity: \n");
                scanf("%f", &i);    
                j=i*12;
                printf(" There are %f in\n", j);
                break;
    
                case 9: /* feet to miles */
                printf("Enter quantity: \n");
                scanf("%f", &i);    
                j=i/5280;
                printf("There are %f mi\n", j);
                break;
    
                case 10: /* miles to cm */
                printf("Enter quantity: \n");
                scanf("%f", &i);    
                j=i*160934.4;
                printf("There are %f cm\n", j);
                break;
    
                case 11: /* miles to inches */
                printf("Enter quantity: \n");
                scanf("%f", &i);    
                j=i*63360;
                printf("There are %f in\n", j);
                break;
    
                case 12: /* miles to feet */
                printf("Enter quantity: \n");
                scanf("%f", &i);    
                j=i*5280;
                printf("There are %f ft\n", j);
                break;
                }
            }
        }

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    i dont think float is a function. float is a type specifier . your function returns a data type in float, maybe thats what your teacher meant.
    Last edited by InvariantLoop; 02-02-2005 at 06:32 PM.
    When no one helps you out. Call google();

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Floats are data types like ints. The difference is that floats handle floating point numbers like 1.5, and 2.333. Here is how you would input a float from a user:

    Code:
    #include <stdio.h>
    
    int main()
    {
        float f = -1.0f;
    	while(f != 0.0f)
    	{
    		scanf("%f",&f);
    		printf("You entered %f\n",f);
    	}
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    first of all float is not a function, it is a datatype. probably you teacher wants you result to be printed out in decimal format.

    as per you code, yhou have declared a variable j as int which stored the result in integer format. but the same i you are printing it as float format which is not good practice better you declare the variable j as float or double(recommanded) to store the result and the print it out.

    hope this will help you out. if there is any problem let me know

    good luck

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    7
    Quote Originally Posted by ssharish
    first of all float is not a function, it is a datatype. probably you teacher wants you result to be printed out in decimal format.

    as per you code, yhou have declared a variable j as int which stored the result in integer format. but the same i you are printing it as float format which is not good practice better you declare the variable j as float or double(recommanded) to store the result and the print it out.

    hope this will help you out. if there is any problem let me know

    good luck

    Unfortunately, I am so lost. I have never programmed anything like this before. I have only done HTML. OK....using the info given here and the stuff our professor gave us I cam up with the following. Now my switch isn't working. Can you see what I am doing wrong?

    Code:
    /* Assignment 1 */
    
    #include <stdio.h>
    
    main()
    {
    	
    	int i;
    	int j;
    	float f;
    	f=1.23;
        	
    		
    	{
    		
    		/* Menu of Choices */
    		printf("1: cm to in\n");
    		printf("2: cm to ft\n");
    		printf("3: cm to mi\n");
    		printf("4: in to cm\n");
    		printf("5: in to ft\n");
    		printf("6: in to mi\n");
    		printf("7: ft to cm\n");
    		printf("8: ft to in\n");
    		printf("9: ft to mi\n");
    		printf("10: mi to cm\n");
    		printf("11: mi to in\n");
    		printf("12: mi to ft\n");
    		printf(" Enter your selection: \");
    		scanf("%f", &i);
    		switch (i)
    
    		{
    			case 1: /* cm to inches */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);
    			j=i/2.4;
    			printf("There are %f inches\n", j);
    			break;
    
    			case 2: /* cm to feet */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);
    			j=i/30.5;
    			printf("There are %f ft\n", j);
    			break;
    
    			case 3: /* cm to mi*/
    			printf("Enter quantity: \n");
    			scanf("%f", &i);
    			j=i/160934.4;
    			printf("There are %f mi\n", j);
    			break;
    
    			case 4: /* inches to cm */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);	
    			j=i*2.4;
    			printf("There are %f cm\n", j);
    			break;	
    
    
    			case 5: /* inches to feet */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);	
    			j=i/12;
    			printf("There are %f ft\n", j);
    			break;
    
    			case 6: /* inches to miles */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);
    			j=i/63360;
    			printf("There are %f mi\n", j);
    			break;
    
    			case 7: /* feet to cm */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);	
    			j=i*30.5;
    			printf("There are %f cm\n", j);
    			break;
    
    			case 8:  /* feet to inches */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);	
    			j=i*12;
    			printf(" There are %f in\n", j);
    			break;
    
    			case 9: /* feet to miles */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);	
    			j=i/5280;
    			printf("There are %f mi\n", j);
    			break;
    
    			case 10: /* miles to cm */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);	
    			j=i*160934.4;
    			printf("There are %f cm\n", j);
    			break;
    
    			case 11: /* miles to inches */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);	
    			j=i*63360;
    			printf("There are %f in\n", j);
    			break;
    
    			case 12: /* miles to feet */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);	
    			j=i*5280;
    			printf("There are %f ft\n", j);
    			break;
    		}
    	}		
    }

  6. #6
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Switch statements only handles types char and int.

    In the statement...
    Code:
    scanf("%f", &i);
    int i is promoted to a floating point number because the string manipulator tells scanf() to input a float.

    You can fix it by changing it to...
    Code:
    scanf("%d", &i);

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    check out this code, which could probabaly help you.

    i have made some changes in the code. so that i could satisfy your requirment

    Code:
    /* Assignment 1 */
    
    #include <stdio.h>
    
    main()
    {
        
        int i;
      float qua;
        float res;
               
            /* Menu of Choices */
            printf("1: cm to in\n");
            printf("2: cm to ft\n");
            printf("3: cm to mi\n");
            printf("4: in to cm\n");
            printf("5: in to ft\n");
            printf("6: in to mi\n");
            printf("7: ft to cm\n");
            printf("8: ft to in\n");
            printf("9: ft to mi\n");
            printf("10: mi to cm\n");
            printf("11: mi to in\n");
            printf("12: mi to ft\n");
            printf(" Enter your selection: \n");
            scanf("%d", &i);
            switch (i)
    
            {
                case 1: /* cm to inches */
                printf("Enter quantity: \n");
                scanf("%f", &qua);
                res=qua/2.4;
                printf("There are %f inches\n", res);
                break;
    
                case 2: /* cm to feet */
                printf("Enter quantity: \n");
                scanf("%f", &qua);
                res=qua/30.5;
                printf("There are %f ft\n", res);
                break;
    
                case 3: /* cm to mi*/
                printf("Enter quantity: \n");
                scanf("%f", &qua);
                res=qua/160934.4;
                printf("There are %f mi\n", res);
                break;
    
                case 4: /* inches to cm */
                printf("Enter quantity: \n");
                scanf("%f", &qua);    
                res=qua*2.4;
                printf("There are %f cm\n", res);
                break;    
    
    
                case 5: /* inches to feet */
                printf("Enter quantity: \n");
                scanf("%f", &qua);    
                res=qua/12;
                printf("There are %f ft\n", res);
                break;
    
                case 6: /* inches to miles */
                printf("Enter quantity: \n");
                scanf("%f", &qua);
                res=qua/63360;
                printf("There are %f mi\n", res);
                break;
    
                case 7: /* feet to cm */
                printf("Enter quantity: \n");
                scanf("%f", &qua);    
                res=qua*30.5;
                printf("There are %f cm\n", res);
                break;
    
                case 8:  /* feet to quanches */
                printf("Enter quantquaty: \n");
                scanf("%f", &qua);    
                res=qua*12;
                printf(" There are %f quan\n", res);
                break;
    
                case 9: /* feet to mquales */
                printf("Enter quantquaty: \n");
                scanf("%f", &qua);    
                res=qua/5280;
                printf("There are %f mqua\n", res);
                break;
    
                case 10: /* mquales to cm */
                printf("Enter quantquaty: \n");
                scanf("%f", &qua);    
                res=qua*160934.4;
                printf("There are %f cm\n", res);
                break;
    
                case 11: /* mquales to quanches */
                printf("Enter quantquaty: \n");
                scanf("%f", &qua);    
                res=qua*63360;
               printf("There are %f quan\n", res);
                break;
    
                case 12: /* mquales to feet */
               printf("Enter quantquaty: \n");
                scanf("%f", &qua);    
                res=qua*5280;
               printf("There are %f ft\n", res);
                break;
            }
            fflush(stdin);
           getchar();
    }
    if any problem let me know

    good luck

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > main()
    Be specific, say int main
    Sure not saying it means the same as int, but implicit types are a deprecated feature of C, so it's a good idea to get used to saying what you mean.

    > printf("Enter quantity: \n");
    > scanf("%f", &qua);
    While you were copy/pasting (and adding spelling mistakes in the process), didn't it occur to you that every single case was doing exactly the same thing?
    Putting these two lines just before the switch saves an awful lot of code.

    > fflush(stdin);
    Gee, I wonder if there's a FAQ on this?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM