converting problem

hi there I'm trying to write a code to convert feet and inches to meters and centimetres. but I'm having trouble with getting my program to pick up how many inches I have enterd it is converting how many feet to meters but it wont convert my inches to centimetres . if anyone can point me towards my problem it would be a great help thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
 #include <stdio.h>
int main()
{	
  float m,f,c;
  printf("Type feet : ");
  scanf("%f",&m);
  printf("Type inches : ");
  scanf("%d",&c);
  f = 0.3048 * m;
  c = 2.54 * c;
  printf(" hight in meters and centimetres %f+%c c",f,c);
  return 0; 
}
Last edited on
There are mismatches in the scanf and printf statements. All your variables are type float.

scanf("%d",&c); %d tries to read an int.
printf(" hight in meters and centimetres %f+%c c",f,c); %c tries to output a char.


Also the conversion seems wrong. The simplest way is to convert the feet to inches. Add the inches to get total inches. Then convert to total inches to metres. (39.37 inches to the metre). Finally , the integer part of the result is the metres. the fractional part * 100 is the centimetres.


I tried to re write the code but seam to be getting more problems now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <math.h>

void input(int *, int *);
void calculation(int, int, int*, int*);
void output(int, int, int, int);
int main()
{
{	
int  feet, inches, meters, centimeters;
input(&feet, &inches);
calculation(feet, inches, &meters, &centimeters);
output(feet, inches, meters, centimeters);
return 0;
}
void input(int *f, int *i)
{
printf("Enter feet:\n");
scanf("%d", f);
printf("Enter inches:\n");
scanf("%d", i);
}
void calculation(int f, int i, int *m, int *c)
{
// calculation here to convert f and i to m and c
i = 12*f;
c = i*2.54;
m = c/100;
c - = m*100;
}
void output(int f, int i, int m, int c)
{
printf("%d feet and %d inches  converted is %d meters and %d  centimeters \n", f, i, m, c);
}
do
{
printf("To go again      press Y\n"); // send back to start 
printf("To exit program  press N\n"); // exits program            
scanf("%c", &choice);
} 
while(choice != 'Y' && choice != 'y' && choice != 'N' && choice != 'n');
while(choice == 'Y' || choice == 'y'); 
return 0;
}
closed account (48T7M4Gy)
Convert to millimeters. And since you are doing integer calculations everywhere the conversion is to the nearest centimetre which is quite coarse.

Also the while loop needs to be redesigned and repositioned from your original.

Note all the changes to addressing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <math.h>

void input(int &, int &);
void calculation(int, int, int&, int&);
void output(int, int, int, int);

int main()
{
    int  feet, inches, meters, centimeters;
    
    input(feet, inches);
    calculation(feet, inches, meters, centimeters);
    output(feet, inches, meters, centimeters);
    return 0;
}

void input(int &f, int &i)
{
    printf("Enter feet:\n");
    scanf("%d", &f);
    
    printf("Enter inches:\n");
    scanf("%d", &i);
}

void calculation(int f, int i, int &m, int &c)
{
    // calculation here to convert f and i to m and c
    int mm = (12 * f + i) * 25.4;
    m = mm/1000;
    c = (mm - m * 1000)/100;
}

void output(int f, int i, int m, int c)
{
    printf("%d feet and %d inches  converted is %d meters and %d  centimeters \n", f, i, m, c);
    return;
}
Ok - that's a bit of a tangle. Let's try to straighten it out a bit.

One thing there, because there's no indentation, I (and the compiler) have to determine the structure based upon the opening and closing braces.
On that case, it would look like this (unchanged apart from indentation)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <math.h>

void input(int *, int *);
void calculation(int, int, int*, int*);
void output(int, int, int, int);

int main()
{
    {   
        int  feet, inches, meters, centimeters;
        input(&feet, &inches);
        calculation(feet, inches, &meters, &centimeters);
        output(feet, inches, meters, centimeters);
        return 0;
    }
    
    void input(int *f, int *i)
    {
        printf("Enter feet:\n");
        scanf("%d", f);
        printf("Enter inches:\n");
        scanf("%d", i);
    }
    
    void calculation(int f, int i, int *m, int *c)
    {
        // calculation here to convert f and i to m and c
        i = 12*f;
        c = i*2.54;
        m = c/100;
        c - = m*100;
    }
    
    void output(int f, int i, int m, int c)
    {
        printf("%d feet and %d inches  converted is %d meters and %d  centimeters \n", f, i, m, c);
    }
    
    do
    {
        printf("To go again      press Y\n"); // send back to start 
        printf("To exit program  press N\n"); // exits program            
        scanf("%c", &choice);
    } 
    while(choice != 'Y' && choice != 'y' && choice != 'N' && choice != 'n');
    
    while(choice == 'Y' || choice == 'y'); 
    
    return 0;
}



So - now it's clear, the first problem is that the function definitions are all nested inside the main() function. That isn't possible, they need to be moved outside main(), so each function is standing alone.

That gets us to here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <math.h>

void input(int *, int *);
void calculation(int, int, int*, int*);
void output(int, int, int, int);

int main()
{
    {   
        int  feet, inches, meters, centimeters;
        input(&feet, &inches);
        calculation(feet, inches, &meters, &centimeters);
        output(feet, inches, meters, centimeters);
        return 0;
    }
        
    do
    {
        printf("To go again      press Y\n"); // send back to start 
        printf("To exit program  press N\n"); // exits program            
        scanf("%c", &choice);
    } 
    while(choice != 'Y' && choice != 'y' && choice != 'N' && choice != 'n');
    
    while(choice == 'Y' || choice == 'y'); 
    
    return 0;
}


void input(int *f, int *i)
{
    printf("Enter feet:\n");
    scanf("%d", f);
    printf("Enter inches:\n");
    scanf("%d", i);
}

void calculation(int f, int i, int *m, int *c)
{
    // calculation here to convert f and i to m and c
    i = 12*f;
    c = i*2.54;
    m = c/100;
    c - = m*100;
}

void output(int f, int i, int m, int c)
{
    printf("%d feet and %d inches  converted is %d meters and %d  centimeters \n", f, i, m, c);
}


... to be continued.
Last edited on
Now, a suggested approach. Rather than writing the whole program, and then trying to compile/run it, I'd recommend starting small, and gradually building up one small step at a time. Let's begin with main().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>

int main()
{
    char choice = 'Y'; 

    while(choice == 'Y' || choice == 'y')
    {   
        printf("The main body of the code will go here\n");
        //------------------- start ----------------------------
        
        
        
        
        //-------------------- end -----------------------------        
            
        do
        {
            printf("To go again      press Y\n"); // send back to start 
            printf("To exit program  press N\n"); // exits program            
            scanf(" %c", &choice);
        } while (choice != 'Y' && choice != 'y' && choice != 'N' && choice != 'n');
    }   
    
    return 0;
}


That part should look familiar by now - you might even use it as a basis for a lot of different programs. Now what I'd suggest is to take something like that, test it, try every possible input, make sure it behaves properly. Once you're satisfied, it should not need to be changed - except to add whatever else is needed between the two lines I've labelled -- start -- and -- end -- .


Now, let's add the other code, one small step at a time. First the input function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>

void input(int *, int *);

int main()
{
    char choice = 'Y'; 

    while(choice == 'Y' || choice == 'y')
    {   
        printf("The main body of the code will go here\n");
        //------------------- start ----------------------------
        int feet   = 0;
        int inches = 0;
        input(&feet, &inches);
        
        
        
        
        //-------------------- end -----------------------------        
            
        do
        {
            printf("To go again      press Y\n"); // send back to start 
            printf("To exit program  press N\n"); // exits program            
            scanf(" %c", &choice);
        } while (choice != 'Y' && choice != 'y' && choice != 'N' && choice != 'n');
    }   
    
    return 0;
}

void input(int *f, int *i)
{
    printf("Enter feet:\n");
    scanf("%d", f);
    printf("Enter inches:\n");
    scanf("%d", i);
}

Having done that, test it again. Next add the output function. and re-test.
Last of all add the calculation function.

By now, main should look something like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int main()
{
    char choice = 'Y'; 

    while(choice == 'Y' || choice == 'y')
    {   
        printf("The main body of the code will go here\n");
        //------------------- start ----------------------------
        
        int feet        = 0;
        int inches      = 0;
        int meters      = 0;
        int centimeters = 0;
                
        input(&feet, &inches);
                
        calculation(feet, inches, &meters, &centimeters);

        output(feet, inches, meters, centimeters);
        
        //-------------------- end -----------------------------        
            
        do
        {
            printf("To go again      press Y\n"); // send back to start 
            printf("To exit program  press N\n"); // exits program            
            scanf(" %c", &choice);
        } while (choice != 'Y' && choice != 'y' && choice != 'N' && choice != 'n');
    }   
    
    return 0;
}


That just leaves the calculation function, which is a bit broken.
that's brilliant thanks for the help chervil and kemort. its a lot easier to understand when broke down into them steps. ill have a go at writing it again using the steps above thanks
Topic archived. No new replies allowed.