Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-字符实现的错误检查输入 intmain(){ printf(“输入一个数字[在1.0-10.0之间]:”; 双低频; 对于(;;){ printf(“number=”); 扫描频率(“%lf”、&lf); if(lf10){ printf(“错误:编号必须为=1.000000\n”); } 否则,如果(lf>10){ printf(“Error:number必须_C_Scanf - Fatal编程技术网

C-字符实现的错误检查输入 intmain(){ printf(“输入一个数字[在1.0-10.0之间]:”; 双低频; 对于(;;){ printf(“number=”); 扫描频率(“%lf”、&lf); if(lf10){ printf(“错误:编号必须为=1.000000\n”); } 否则,如果(lf>10){ printf(“Error:number必须

C-字符实现的错误检查输入 intmain(){ printf(“输入一个数字[在1.0-10.0之间]:”; 双低频; 对于(;;){ printf(“number=”); 扫描频率(“%lf”、&lf); if(lf10){ printf(“错误:编号必须为=1.000000\n”); } 否则,如果(lf>10){ printf(“Error:number必须,c,scanf,C,Scanf,您可以使用字符串库,更具体地说,可以使用一个名为atof()的函数,该函数将数字作为参数。如果提供的字符不是数字,它将返回0。因此,将数字作为字符读取,然后如果得到0,您可以退出程序。因此,它看起来是这样的: int main() { printf("\nEnter a number [between 1.0 - 10.0]: "); double lf; for (;;) { printf("number = "); scanf("%lf", &lf); if

您可以使用字符串库,更具体地说,可以使用一个名为atof()的函数,该函数将数字作为参数。如果提供的字符不是数字,它将返回0。因此,将数字作为字符读取,然后如果得到0,您可以退出程序。因此,它看起来是这样的:

int main() {
printf("\nEnter a number [between 1.0 - 10.0]: ");
double lf;
for (;;) {
    printf("number = ");
    scanf("%lf", &lf);
    if (scanf("%lf", &lf) != 1) {
        printf("Error: enter a number\n");
        int ch;
        while ((ch = getchar()) != EOF && ch != '\n');
        continue;
    }
    else if (lf < 1) {
        printf("Error: number must be >= 1.000000\n");
    }
    else if (lf > 10) {
        printf("Error: number must be <= 10.000000\n");
    }
    else {
        break;
    }
}
printf("Enter another number: ");
}

这将使用getline(3),它将malloc和/或realloc一个缓冲区来保存输入行。这样就不用担心截断或空终止

scanf("%s",  temp);
int lf = atof(temp);
if( !lf) {
    printf("No strings allowed");
    exit(1);
为getline(3)定义源代码700// #包括 #包括 内部主(空){ char*ln=NULL; 尺寸lnsz=0; 双低频; printf(“输入一个数字[介于1.0-10.0之间]:number=”); 而(getline(&ln,&lnsz,stdin)>0){ 字符c=0; 如果(sscanf(ln,%lf%c,&lf,&c)!=2 | | c!='\n') printf(“错误:必须是一个数字\n”); else if(lf<1) printf(“错误:编号必须大于等于1.000000\n”); 否则,如果(lf>10) printf(“错误:编号必须是
intmain(){
printf(“\n输入一个数字[在1.0-10.0之间]:”;
双低频;
对于(;;){
printf(“number=”);
如果(scanf(“%lf”,&lf)!=1){//这是这里的修复
printf(“错误:输入一个数字\n”);
int-ch;
而((ch=getchar())!=EOF&&ch!='\n');
继续;
}
else if(lf<1){
printf(“错误:编号必须大于等于1.000000\n”);
}
否则,如果(lf>10){

printf(“错误:数字必须是检查
scanf()
的返回值吗?如果你能看一下,我已经编辑了我的代码!
scanf("%s",  temp);
int lf = atof(temp);
if( !lf) {
    printf("No strings allowed");
    exit(1);
#define _XOPEN_SOURCE 700 // for getline(3)
#include <stdio.h>
#include <stdlib.h>

int main(void) {
        char *ln = NULL;
        size_t lnsz = 0;
        double lf;

        printf("Enter a number [between 1.0 - 10.0]: number = ");
        while (getline(&ln, &lnsz, stdin) > 0) {
                char c = 0;
                if (sscanf(ln, "%lf%c", &lf, &c) != 2 || c != '\n')
                        printf("Error: must be a number\n");
                else if (lf < 1)
                        printf("Error: number must be >= 1.000000\n");
                else if (lf > 10)
                        printf("Error: number must be <= 10.000000\n");
                else
                        break;
                printf("number = ");
        }
        printf("lf = %f\n", lf);
        free(ln);
        return 0;
}
int main() {
printf("\nEnter a number [between 1.0 - 10.0]: ");
double lf;
for (;;) {
printf("number = ");
if (scanf("%lf", &lf) != 1) {                   //THIS IS THE FIX HERE
    printf("Error: enter a number\n");
    int ch;
    while ((ch = getchar()) != EOF && ch != '\n');
    continue;
}
else if (lf < 1) {
    printf("Error: number must be >= 1.000000\n");
}
else if (lf > 10) {
    printf("Error: number must be <= 10.000000\n");
}
else {
    break;
}
}
printf("Enter another number: ");