Thread: One Last Question....I swear (its due at midnight!)

1. One Last Question....I swear (its due at midnight!)

My program seems to work now byt all of the answers come out to zero - any ideas????

Code:
```/* Assignment 1 */

#include <stdio.h>

main()
{

int i;
int j;
float f;
f=1.23;

{

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/  ( float ) 2;
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;
}
}
return 0;
}```

2. Code:
`scanf("%f", &i);`
i is an int not a float
Code:
`j=i/12;`
will result in an integer always

3. ok...then I am seriously lost as to what to do...I tried the following:

Code:
```case 1: /* cm to inches */
printf("Enter quantity: \n");
scanf("%f", &f);
j=f/  ( float ) 2.4;
printf("There are %f inches\n", j);
break;```
This is still resulting in a zero answer ( actually the answer is coing out 0.00000)

Any hep would be EXTREMELY appreciated!

4. You're still trying to use type int as floating point numbers.
Code:
```    int i;  /* don't reuse this variable in your switch statements */
int j;  /* should be type float */
/* <--------- Should add another variable of type float for use inside case functions*/
float f;
f=1.23;
.
.
.
case 1: /* cm to inches */
printf("Enter quantity: \n");
scanf("%f", &i); /* i is declared as int, the statement requires a float
declare and use a new variable type float as mentioned above */
j=i/  ( float ) 2;
printf("There are %f inches\n", j);
break;```

5. Code:
`/* <--------- Should add another variable of type int for switch cases */`

6. Originally Posted by jverkoey
Code:
`/* <--------- Should add another variable of type int for switch cases */`
gack!

Actually that's a bad case of wording. I meant it to mean declare a new variable type float for use in the functions inside each switch/case. I edited it to reflect proper wording .

7. Thank you all so much! You have been wonderful!