c - 函数没有返回正确的值

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

函数 converterterm(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);

}

最佳答案

问题出在您尝试打印值的部分。如果出现

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 .

关于c - 函数没有返回正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34760696/

相关文章:

c++ - 为什么在不相关的 #define 语句上包含 <windows.h> 会导致 "error: expected ' )' before numeric constant"

c - C 中的全局变量链表队列? (初始化元素不是常量)

c - int16_t 是否保证被签名?

c++ - 使用 fprintf 获取访问冲突写入位置 0x00000014

c++ - sprintf函数的问题,last参数写错了

c++ - 使用 %。在 printf 中

android - onClick() 的函数在下次点击时返回数据,但不会在同一次点击时返回

c++ - 钩。 va_list 。可能吗?

C#:为方法嵌套返回 'this'?

java - 从方法返回 "void"或 "null"