函数不返回正确的值

时间:2016-01-13 07:26:11

标签: c printf return-value return-type format-specifiers

调用函数converterm(met, bri);时没有返回正确的值。代码仍然不完整,但它适用于某些选项。只需键入值,只要询问选择哪个选项,选择选项1并查看结果。

在此行conm.m = conb.ft / 3.2808;,它不会返回预期值。

//METRIC_BRITISH - BUILD 1.0

#include<stdio.h>
#include<stdlib.h>
#include<process.h>

//GLOBAL-STRUCTURES DECLARATION
struct metric
{
float m;
float cm;
};

struct british
{
float ft;
float in;
};

//GLOBAL-STRUCTURE-VARIABLE DECLARATION
struct metric met = { 0,0 };
struct british bri = { 0,0 };
int q = 0;
int w = 0;

//USER-DEFINED FUNCTION
struct metric converterm(struct metric conm, struct british conb);
struct british converterb(struct british conb, struct metric conm);
void header();

void header()
{
printf("*-*-*-*-*METRIC_BRITISH*-*-*-*-*");
printf("\n\n");
}

//PROGRAM STARTS HERE
main()
{
//VARIABLE-DECLARATION
int n = 0, c = 0, b = 0, v = 0, i = 0;

//FUNCTION CALL-OUT
header();

printf("Format : Metric \n\n");
printf("Enter the Value for Metres : ");
scanf_s("%f", &met.m);
printf("\n");
printf("Enter the Value for Centimetres : ");
scanf_s("%f", &met.cm);
printf("\n");

printf("*--------------------------------------------*\n\n");

printf("Format : British \n\n");
printf("Enter the Value for Feet : ");
scanf_s("%f", &bri.ft);
printf("\n");
printf("Enter the Value for Inches : ");
scanf_s("%f", &bri.in);
printf("\n\n");

printf("In which Format would you like to add other value? \n");
printf("1. Metric \n");
printf("2. British \n");
printf("Enter any Option : ");

while (i == 0)
{
    printf("\n");
    scanf_s("%d", &n);
    switch (n)
    {
    case 1:

        printf("In which Unit you want to add value? \n");
        printf("1. Metres \n");
        printf("2. Centimetres \n");
        printf("Enter any Option : ");
        scanf_s("%d", &c);
        q = c;
        met = converterm(met, bri);
        i = 1;

        break;

    case 2:

        printf("In which Unit you want to add value? \n");
        printf("1. Feet \n");
        printf("2. Inch \n");
        printf("Enter any Option : ");
        scanf_s("%d", &c);
        q = c;
        bri = converterb(bri, met);
        i = 1;

        break;

    default:

        printf("INVALID OPTION. \n");
        printf("Please Enter Correct Option.");
        i = 0;

        break;
    }

}

printf("Values for Metric : \n");
printf("Metres = %d \n", met.m);
printf("Centimetre = %d \n", met.cm);

printf("\n*--------------------------------------------*\n\n");

printf("Values for British : \n");
printf("Feet = %d \n", bri.ft);
printf("Inch = %d \n", bri.in);

//TERMINAL-PAUSE
system("pause");
}

struct metric converterm(struct metric conm, struct british conb)
{
int i = 0;

switch (q)
{
case 1:

    printf("\n");
    printf("Would you like to Add? \n");
    printf("1. Add Feet to Metre \n");
    printf("2. Add Inch to Metre \n");
    printf("Enter any Option : ");
    scanf_s("%d", &i);
    break;

case 2:

    printf("\n");
    printf("Would you like to Add? \n");
    printf("1. Add Feet to Centimetre \n");
    printf("2. Add Inch to Centimetre \n");
    printf("Enter any Option : ");
    scanf_s("%d", &i);
    break;

}

if (i == 1)
{
    conm.m = conb.ft / 3.2808;
    //conm.m = conb.in / 39.370;
}

else
{
    conm.cm = conb.ft / 0.032808;
    //conm.cm = conb.in / 0.39370;
}

return(conm);
}

struct british converterb(struct british conb, struct metric conm)
{
int i = 0;

switch (w)
{
case 1:

    printf("\n");
    printf("Would you like to Add? \n");
    printf("1. Add Metre to Feet \n");
    printf("2. Add Centimetre to Feet \n");
    printf("Enter any Option : ");
    scanf_s("%d", &i);
    break;

case 2:

    printf("\n");
    printf("Would you like to Add? \n");
    printf("1. Add Metre to Inch \n");
    printf("2. Add Centimetre to Inch \n");
    printf("Enter any Option : ");
    scanf_s("%d", &i);
    break;

}

if (i == 1)
{
    conb.ft = conm.m*3.2808;
    //conb.ft = conm.cm*0.032808;
}

else
{
    conb.in = conm.m*39.370;
    //conb.in = conm.cm*0.39370;
}

return(conb);

}

1 个答案:

答案 0 :(得分:4)

问题在于您尝试打​​印值的部分。在

的情况下
printf("Metres = %d \n", met.m);
printf("Centimetre = %d \n", met.cm);

printf("Feet = %d \n", bri.ft);
printf("Inch = %d \n", bri.in);

您正在使用%d格式说明符来打印float类型变量的值。您应该使用%f代替。

FWIW,对格式说明符使用不适当的参数类型会调用undefined behavior