# How do you use float????

• 02-02-2005
LittleLotte
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;             }         }     }```
• 02-02-2005
InvariantLoop
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.
• 02-02-2005
bithub
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; }```
• 02-02-2005
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
• 02-02-2005
LittleLotte
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;                 }         }                }```
• 02-02-2005
Scribbler
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);`
• 02-02-2005
ssharish

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
• 02-03-2005
Salem
> 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?