简体   繁体   English

C 逗号在 scanf 中破坏了 integer

[英]C comma is breaking integer in scanf

The code below asks the user to enter a number followed by an operator.下面的代码要求用户输入一个数字,后跟一个运算符。 It works as long as no commas are used.只要不使用逗号,它就可以工作。 For example,例如,

Happy path快乐之路

6000+2000 will output 6000.000 + 2000.000 

Unexpected output意外 output

6,000+2,000 will output 6.000 , 0.000

Can scanf somehow treat the value as float when using a comma? scanf可以在使用逗号时以某种方式将值视为浮点数吗?

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

int main(int argc, char *argv[]) 
{
    char operatorFound;
    float num1,num2, calcValue;
    printf("Please enter a simple expression (ie: 2+2):" );
    scanf("%f%c%f", &num1, &operatorFound, &num2);
    printf("%f\n%c\n%f", num1, operatorFound, num2); 
   return 0;
}

It may be possible to adjust scanf under Linux by setting the locale accordingly.可以通过相应地设置语言环境来调整 Linux 下的 scanf。 See scanf Linux documentation请参阅scanf Linux 文档

For decimal conversions, an optional quote character (').对于十进制转换,可选的引号字符 (')。 This specifies that the input number may include thousands' separators as defined by the LC_NUMERIC category of the current locale.这指定输入数字可能包含千位分隔符,由当前语言环境的 LC_NUMERIC 类别定义。 (See setlocale(3).) The quote character may precede or follow the '*' assignment- suppression character. (参见 setlocale(3)。)引号字符可以在 '*' 赋值抑制字符之前或之后。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <locale.h>

int main(int argc, char *argv[]) 
{
    char operatorFound;
    float num1, num2, calcValue;
    // Use en_US.UTF-8 to separate groups of thousands, many other countries use a period instead, and some countries separate thousands groups with a space
    setlocale(LC_NUMERIC, "en_US.UTF-8");
    puts("Please enter a simple expression (ie: 2+2):" );
    
    // Notice the use of a single quote
    if (scanf("%'f %c%'f", &num1, &operatorFound, &num2) == 3) { 
        printf("%f\n%c\n%f", num1, operatorFound, num2); 
    } else {
        puts("Unable to process input");
    }
   return 0;
}

While you are scanning the input C takes commas as char.在扫描输入时 C 将逗号作为字符。

If you enter only 6,00 it will still print 6.000, 0.000 because it takes comma as a second input.如果您只输入 6,00,它仍然会打印 6.000, 0.000,因为它需要逗号作为第二个输入。

Therefore you should use dots '.'因此,您应该使用点“。” for floats用于花车

So after testing.所以经过测试。 I found out my compiler is flawed.我发现我的编译器有缺陷。 When I test on Linux GCC all the answers worked.当我在 Linux GCC 上测试时,所有答案都有效。

If I enter如果我输入

6,000 + 2,000 it will output 8000 6,000 + 2,000 它将 output 8000

6000 + 2000 it will output 8000 6000 + 2000 它将 output 8000

6,000 + 2000 it will output 8000 6,000 + 2000 它将 output 8000

6000 + 2,000 it will output 8000 6000 + 2,000 它将 output 8000

6000+2000 it will output 8000 6000+2000 它将 output 8000

etc etc等等等等

Thank you all for looking into this and helping me out感谢大家对此进行调查并帮助我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM