扫描、检查、转换、复制值...如何? -- C --

发布于 2024-09-01 16:04:25 字数 1265 浏览 3 评论 0原文

已经有一段时间了,我仍在尝试让某些代码正常工作。我之前问过一些关于不同命令等的问题,但现在我希望这是最后一个问题(将所有问题合并在一个代码中)。

我基本上想要:

*扫描输入(应该是字符?)

*检查它是否是数字

*如果不是,则返回错误

*将该字符转换为浮点数

*将值复制到另一个变量(我在这里称之为imp)

这里这就是我想出的:

编辑代码*

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
main(){  
  int digits;
  float imp=0;
  char* alpha;
  do{

    printf("Enter input\n\n");
    scanf("\n%c",alpha);
    digits=isdigit(alpha);
      if(digits==0){
         printf("error\n\n");
      }
     imp=atof(alpha);
  }while(digits==0);

}

问题是这段代码根本不起作用......它告诉我 atof 必须是一个 const char ,每当我尝试改变它时,它只是不断失败。我很沮丧,被迫在这里问,因为我相信我已经尝试了很多,但我一直失败,但在我让它工作之前我不会松一口气 xD 所以我真的需要你们的帮助。

请告诉我为什么这段代码不起作用,我做错了什么?我仍在学习 C,非常感谢您的帮助:)

编辑 atm 给出的错误是:

'isdigit' 的参数 1 必须是 'const int' 类型,而不是 'char'

编辑 该代码编译良好,但在输入输入时崩溃。

  #include<stdio.h>
  #include<stdlib.h>
  #include<ctype.h>
  main(){  
  int digits;
  float imp=0;
  char* alpha=0;
  do{

   printf("Enter input\n\n");
   scanf("\n%s",alpha);
   digits=(isdigit(alpha[0]));
   imp=atof(alpha);
  }while(digits==0);
 }

Its been a while now and im still trying to get a certain code to work. I asked some question about different commands etc. before, but now I hope this is the final one (combining all questions in one code).

I basically want to :

*Scan an input (should be character ? )

*Check if its a number

*If not, return error

*Convert that character into a float number

*Copy the value to another variable ( I called it imp here)

Here is what I came up with :

EDITED CODE*

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
main(){  
  int digits;
  float imp=0;
  char* alpha;
  do{

    printf("Enter input\n\n");
    scanf("\n%c",alpha);
    digits=isdigit(alpha);
      if(digits==0){
         printf("error\n\n");
      }
     imp=atof(alpha);
  }while(digits==0);

}

The problem is this code does not work at all ... It gives me that atof must be of a const char and whenever I try changing it around, it just keeps failing. I am frustrated and forced to ask here, because I believe I have tried alot and I keep failing, but I wont be relieved until I get it to work xD So I really need your help guys.

Please tell me why isnt this code working, what am I doing wrong ? I am still learning C and really appreciate your help :)

EDIT
Error given atm is :

Argument no 1 of 'isdigit' must be of type 'const int', not '<ptr>char'

EDIT
This code compiles fine, but crashes when an input is entered.

  #include<stdio.h>
  #include<stdlib.h>
  #include<ctype.h>
  main(){  
  int digits;
  float imp=0;
  char* alpha=0;
  do{

   printf("Enter input\n\n");
   scanf("\n%s",alpha);
   digits=(isdigit(alpha[0]));
   imp=atof(alpha);
  }while(digits==0);
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

篱下浅笙歌 2024-09-08 16:04:25

为什么不让 scanf 为您执行 atof 转换呢?

 #include <stdio.h>
 int main()
 {  
    float imp=0;
    while (1)
    {
       printf("Enter input\n\n");
       if (scanf("%f", &imp) < 1) break;
    }
    return 0;
}

您最近的示例失败了,因为 alpha 是 NULL 指针。将其声明为 char alpha[40]; 为其分配空间。您可能需要在格式字符串中使用 %40s 来防止 scanf 溢出 alpha

另外,使用 strtod 而不是 atof,您就会知道转换是否成功(比使用 isdigit 的方法更好,后者会失败)负整数)。

Why not have scanf do the atof conversion for you?

 #include <stdio.h>
 int main()
 {  
    float imp=0;
    while (1)
    {
       printf("Enter input\n\n");
       if (scanf("%f", &imp) < 1) break;
    }
    return 0;
}

Your most recent example is failing because alpha is a NULL pointer. Declare it as char alpha[40]; to allocate space for it. You'll probably want to use %40s in your format string to prevent scanf from overflowing alpha.

Also, use strtod instead of atof and you'll know whether the conversion was successful (better than your method of using isdigit which will fail on a negative integer).

々眼睛长脚气 2024-09-08 16:04:25

您可能需要使用 %s 而不是 %c 并将其放入 char 数组 (char*) 。您还可能会收到一个错误,提示您需要使用 const char* 而不是 const char

您不想只读取一个字符 - 您想读取整个字符串...

编辑

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
main(){  
  int digits,i;
  float imp=0;
  char* alpha = malloc(100); /* 100 is for example */
  do{
    printf("Enter input\n\n");
    scanf("\n%s",&alpha);
    for (i = 0; i != 100; ++i)
    {
      if (alpha[i] == '\0')
        break;
      if (!isdigit(alpha[i]))
      {
        printf("error\n\n");
        return ...;
      }
    }
     imp=atof(alpha);
  }while(true);
}

You probably need to use %s instead of %c and to put it in char array (char*). You also probably get an error that you need to use const char* and not const char.

You don't want to read just one character - you want to read an entire string...

EDIT:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
main(){  
  int digits,i;
  float imp=0;
  char* alpha = malloc(100); /* 100 is for example */
  do{
    printf("Enter input\n\n");
    scanf("\n%s",&alpha);
    for (i = 0; i != 100; ++i)
    {
      if (alpha[i] == '\0')
        break;
      if (!isdigit(alpha[i]))
      {
        printf("error\n\n");
        return ...;
      }
    }
     imp=atof(alpha);
  }while(true);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文