函数没有 return 正确的值
Function do not return proper value
函数 converterm(met, bri);
在被调用时没有 return 正确的值。该代码仍然不完整,但它适用于某些选项。只需输入值,每当询问 [=19=]、select 选项 1 的哪个选项并查看结果时。
在这一行 conm.m = conb.ft / 3.2808;
它没有 return 预期值。
//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。
函数 converterm(met, bri);
在被调用时没有 return 正确的值。该代码仍然不完整,但它适用于某些选项。只需输入值,每当询问 [=19=]、select 选项 1 的哪个选项并查看结果时。
在这一行 conm.m = conb.ft / 3.2808;
它没有 return 预期值。
//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。