char转换为float无效,代码不同且效果不佳

时间:2017-10-20 01:54:01

标签: c char atof

我想编写一个程序,它接收一个介于0到10之间的数字,并显示一条消息说它有效。如果没有,它应该继续要求有效的号码。

我可以对所有内容进行编码,对我来说似乎很合适,但它无法正常工作。

我看过许多类似问题的主题,但我无法解决我的问题。我的意思是,我有一些进步,但我的代码还没有工作。

我做了什么: 我创建了一个变量来存储插入的值,使用ctype库确保它只接受数字,将我的char转换为float并检查它是否符合要求。

我尝试了一些不同的代码,我搜索了很多内容,现在我不知道应该做些什么。

代码1:

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

main(){
    char grade;
    float grade2;

    printf("Please, insert a grade between 0 and 10.\n");
    printf("Grade: ");
    scanf("%c",&grade);

    while(isalpha(grade) || ispunct(grade) || isspace(grade) || iscntrl(grade)){
        printf("Please, insert a valid value.\n");
        fflush(stdin);
        printf("Grade: ");
        scanf("%c",&grade);
    }

    if(isdigit(grade)){
        grade2 = atof(grade);
        while(grade2 < 0 || grade2 > 10){
            printf("\nPlease, insert a valid value.\n");
            fflush(stdin);
            printf("grade: ");
            scanf("%c",&grade2);
        }

        printf("Valid value.\n");
    }

    else{
        printf("Restart the program and try again.");
    }

    system("PAUSE");
}

代码1的一大问题是我得到了这个:

[Error] invalid conversion from 'char' to 'const char*' [-fpermissive]

我无法使它工作,但它可以在这里工作(代码在互联网上找到):

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

int main(){
    char a[10] = "3.14";
    float pi = atof(a);
    printf("Value of pi = %f\n", pi);
    return 0;
}

我认为这将是scanf和初始化,但它也可以通过scanf接收一个值。

最后,我认为它可能是数组。我做了:

代码2:

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

main(){
    char grade[50];
    float grade2;

    printf("Please, insert a grade between 0 and 10.\n");
    printf("Grade: ");
    scanf("%c",&grade[50]);

    while(isalpha(grade[50]) || ispunct(grade[50]) || isspace(grade[50]) || iscntrl(grade[50])){
        printf("Please, insert a valid value.\n");
        fflush(stdin);
        printf("Grade: ");
        scanf("%c",&grade[50]);
    }

    if(isdigit(grade[50])){
        grade2 = atof(grade);
        while(grade2 < 0 || grade2 > 10){
            printf("\nPlease, insert a valid value.\n");
            fflush(stdin);
            printf("Grade: ");
            scanf("%c",&grade2);
        }
        printf("The grade is: %f\n", grade2);
        printf("Valid value.\n");
    }

    else{
        printf("Restart the program and try again.");
    }

    system("PAUSE");
}

无论我写的是什么数字,总是打印它是一个有效的值。所以,我更改了代码以查看它所比较的​​值,输出是:

Please, insert a grade between 0 and 10.
Grade: 11
The grade is: 0.000000
Valid value.

我用谷歌搜索了它,如果出现问题,我看到atof可以返回零。

所以,我尝试做的最后一件事就是使用这个模型:(也可在互联网上找到)

double atof(const char *str) 

最后,

代码3:

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

main(){
    char grade;

    printf("Please, insert a grade between 0 and 10.\n");
    printf("Grade: ");
    scanf("%c",&grade);

    while(isalpha(grade) || ispunct(grade) || isspace(grade) || iscntrl(grade)){
        printf("Please, insert a valid value.\n");
        fflush(stdin);
        printf("Grade: ");
        scanf("%c",&grade);
    }

    if(isdigit(grade)){
        float atof(grade *grade);
        while(grade < 0 || grade > 10){
            printf("\nPlease, insert a valid value.\n");
            fflush(stdin);
            printf("Grade: ");
            scanf("%c",&grade);
        }
        printf("The grade is: %f\n", grade);
        printf("Valid value.\n");
    }

    else{
        printf("Restart the program and try again.");
    }

    system("PAUSE");
}

输出结果为:

Please, insert a grade between 0 and 10.
Grade: 11

Please, insert a valid value.
Grade: 6

Please, insert a valid value.
Grade: 10

Please, insert a valid value.
Grade: 5

Please, insert a valid value.
Grade:

我无法摆脱困境。

我调试了它,发现它在第二次被卡住了。

我试图再次打印这个值,然后又得到了0.000 ......

嗯,我刚来这里是因为我在互联网上做了很好的研究,但我仍然无法解决我的问题。

如果我做错了(比如违反规则),我很抱歉。如果是这样,请告诉我,我会尝试解决它。​​

感谢您的关注。

2 个答案:

答案 0 :(得分:0)

修复你的第二种方法

checkHour = function (open, close) {
    return moment().year(1970).month(0).date(1).isBetween(moment(open), moment(close));
};

答案 1 :(得分:0)

请尝试以下代码。这是自我解释的。如果您需要任何具体解释,请告诉我。

{{1}}