C程序舍入输入值

时间:2017-10-27 14:57:18

标签: c

我需要帮助,

我有这段代码来验证输入:

#include <stdio.h>
#include<stdio.h>

int checkInput1(void);

int main(){

    float a = checkInput1();

    printf("The value of a is:%f\n" , a);
} 

  int checkInput1(void){
    float option1,check1;
    char c;

    do{
        printf("Enter the first side of the triangle");

        if(scanf("%f%c",&option1,&c) == 0 || c != '\n'){
            while((check1 = getchar()) != 0 && check1 != '\n' && check1 != EOF);
            printf("\t[ERR] Invalid number for the triplet.\n");
        }else{
            break;
        }
    }while(1);
    printf("returning the value of option1, which is %f\n", option1);
    return option1;



}

我的问题是,如果我输入例如78.8

输出结果为:

Enter the first side of the triangle78.8                                                                 
returning the value of option1, which is 78.800003                                                       
The value of a is:78.000000

我想说以下内容:

Enter the first side of the triangle78.8                                                                 
returning the value of option1, which is 78.800003                                                       
The value of a is:78.800000

我做错了什么?

提前致谢

1 个答案:

答案 0 :(得分:0)

你从函数返回int,这是不包含小数类型的整数类型。看看你的声明:int checkInput1(void)。函数名称(int)之前的checkInput1表示返回的类型。考虑在那里使用其他类型。