2016-01-13 124 views
-1

函數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); 

} 
+0

請修改您的帖子並修復縮進。 – Lundin

回答

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

+0

謝謝,我一直在尋找解決方案從天xD – Eddy

+0

@Eddy歡迎。 :) –