【问题标题】:while loops , int vs char reading from scanf , true and falsewhile 循环,int vs char 从 scanf 读取,true 和 false
【发布时间】:2020-03-08 17:37:49
【问题描述】:
#include <stdio.h>
int main()
{
    int number; // We make 1 int named number
    scanf("%d",&number); // We give to number some number xd
    while (number!=0)// We made a loop while if number isn't 0 do that
    {
        printf("text");// print text
        scanf("%d",&number); // and get again a number. 
        // So everything works well beside inserting some char instead of int.
        // So what is the problem wont scanf return 0 so we exit the program not
        // just printing a text all day?  That's my first question.
    }
    return 0;
}

第二个问题是如何让程序从键盘读取数字,直到我为 ex '.' 输入一些特殊符号。是的,我们用循环来做,对吗?但是,如果我输入除数字以外的所有内容,scanf("%d",&amp;something) 它如何给我回0

【问题讨论】:

  • ASCII Table and Description -- 如果您扫描为字符而不是 int,则哪个字符构成 number == 0? (提示:无)?始终,始终验证用于获取用户输入的任何函数的返回(尤其是 scanf)。
  • 它不返回 0 吗?公元前是假的?
  • 在决定是否终止循环时,不要只看number的值,还(或只)看scanf本身的返回值 .
  • ASCII 字符'0' 具有十进制值48。十进制值为零的字符是 nul-character (您不能输入)。 while (scanf("%d",&amp;number) == 1) puts ("text"); 呢?
  • 奖励:如果您输入的不是数字,stdin 未读 中还剩下什么? scanf 会导致什么类型的故障? (matching 还是 input?)如果您需要在循环退出后获取更多输入怎么办?与man 3 scanf 一起度过 45 分钟,然后为自己节省十倍的时间……

标签: c loops char int scanf


【解决方案1】:

将它从 scanf int 更改为 char

假设:您一次读取一个字符

#include <stdio.h>

int main()
{
    char c = "0";
    int n = 0;

    while (n != 46)// we made a loop while if char isn't '.' ASCII - 46 do that
    {
        scanf(" %c", &c);
        n = (int)c;
        printf("text %d", n);
        //Add if statement to confirm it is a number ASCII 48 - 57 and use it.
    }
    return 0;
}

编辑: 关于如何使用数字的更多信息: 逐个输入数字或仅输入一个整数,例如 123,并带有一些结尾特殊字符,例如 ';'或者换行

scanf(" %c", &c);

到:

scanf("%c", &c);

这样它会将 '\n' 注册为 ASCII 10

将它与 atoi 一起使用以获取实际的 int 值并使用它。

编辑 2:

@World,您不能期望只读取数字和“。” 使用您自己的代码执行此操作的一种方法是:

#include <stdio.h>

int main()
{
    char c = "0";
    int n = 0;
    int number = 0;

    while (n != 46)// we made a loop while if char isn't '.' ASCII - 46 do that
    {
        scanf("%c", &c);
        n = (int)c;

        if (n>=48 && n<=57) {
            number *= 10;
            number += (n-48);
        }
        if (n==10) {
             printf("text %d\n", number);
             //Use number for something over here
             number = 0;
        }
    }
    return 0;
}

编辑 3: 这背后的逻辑:

假设您在控制台中输入 341 线

scanf("%c", &c);

将一一读取,因此分别读取 '3'、'4'、'1' 和 '\n' while循环

while (n != 46)

将因此运行 4 次,其中:

第一次

c = '3';
n = 51;
if (n>=48 && n 数字 = 0 * 10;
数 = 数 + n - 48 = 0 + 51 - 48 = 3

第二次

c = '4';
n = 52;
if (n>=48 && n 数字 = 3 * 10 = 30;
数 = 数 + n - 48 = 30 + 52 - 48 = 34

第三次

c = '1';
n = 49;
if (n>=48 && n 数字 = 34 * 10 = 340;
数 = 数 + n - 48 = 340 + 49 - 48 = 341

第四次

c = '\n';
n = 10;
if (n>=48 && n 如果 (n==10) 为真;
它打印“text 341”并将数字设置为 0

当 c = '.' 时,while 循环退出n = 46

【讨论】:

  • 如何输入341
  • '3', '4', '1', '\n', 附加到字符串, atoi()
  • 可以输入341,scanf会逐个字符读取,因此打印出:text 51text 52 text 49
  • 不是我要问的问题是如何从 scanf 获取 int 然后像 n++ 一样使用它们或使用 if statemans 并且当我插入 ' 。并且只有 ' 。 '不是其他字符而已'。 ' 从 while 中中断。
  • 好的,第二个代码运行良好,但它是如何工作的,为什么我们得到数字 *10,如果不是 n-48,我不明白。
【解决方案2】:

如何让程序从键盘读取数字,直到我为 ex '.' 输入一些特殊符号为止

scanf("%d",&amp;number) 返回 0 时,有一些非数字输入。使用备用代码读取该非数字输入。

#include <stdio.h>

int main() {
  while (1) {
    int number;
    int scan_count = scanf("%d", &number);
    if (scan_count == EOF) break;  // End-of-file or rare input error.
    if (scan_count != 1) {
      int ch = fgetc(stdin);
      if (ch == '.') break;  // Expected non-numeric input to break the loop.
      printf("Unexpected input character '%c'\n", ch);
    } else {
      printf("Expected numeric input: %d\n", ch);
    }
  } // endwhile
  printf("Done\n");
}

更好的方法是放弃 scanf() 并使用 fgets()` 来读取用户输入。然后处理输入的字符串。

【讨论】:

    猜你喜欢
    • 2015-11-05
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 2023-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多