Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.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中的值,直到找到新行char,'\n';_C_Arrays_Algorithm_Scanf_Getchar - Fatal编程技术网

扫描C中的值,直到找到新行char,'\n';

扫描C中的值,直到找到新行char,'\n';,c,arrays,algorithm,scanf,getchar,C,Arrays,Algorithm,Scanf,Getchar,我如何才能scanf()将输入的整数值输入到数组中,直到我按enter键为止 我相信我可以使用getchar()!='\n' 但是我怎么才能在这条线上循环呢 假设我的输入是2021122。我想要一个包含所有这些输入的数组。 我可以使用什么给定的函数来扫描它们 您正在尝试将整数作为字符读取,因此一旦读取,您需要将其转换为整数 使用fgets()将行读取到缓冲区,然后解析输入缓冲区以获取整数 将整数存储到数组中 代码看起来像 char buf[300]; int a[5],i=0; fgets(bu

我如何才能
scanf()
将输入的整数值输入到数组中,直到我按enter键为止

我相信我可以使用
getchar()!='\n'

但是我怎么才能在这条线上循环呢

假设我的输入是
2021122
。我想要一个包含所有这些输入的数组。 我可以使用什么给定的函数来扫描它们

  • 您正在尝试将整数作为字符读取,因此一旦读取,您需要将其转换为整数
  • 使用
    fgets()
    将行读取到缓冲区,然后解析输入缓冲区以获取整数
  • 将整数存储到数组中
  • 代码看起来像

    char buf[300];
    int a[5],i=0;
    fgets(buf,sizeof(buf),stdin);
    char *p = strtok(buf," ");
    while(p != NULL)
    {
      char *endptr;
      a[i] = strtol(p,&endptr,10);
      if ((*endptr != '\0') && (isspace(*endptr) == 0))
          printf("warning: invalid value detected\n");
      else
          i++;
      p = strtok(NULL," ");
    }
    
    您可以使用可选的
    strtol()
    而不是
    atoi()
    将字符串转换为整数

    注:您的
    buf
    应该足够大,可以容纳整行
    fgets()
    读取换行符

    如果使用
    getchar()
    可以逐个获取数字,因此需要 首先将它们存储在缓冲区中,当出现空白时, 将这些数字转换为数字,并将其存储到数组中

    下面是我为您编写的代码的解释

    • 第一条if语句:如果获得的字符是数字,则将其存储在
      buf
    • 第二条if语句:如果获取的字符是空白或下线,且至少有1位数字存储在
      buf
      中,则将数字转换为数字并存储在数组
      a
    • 第三条if语句:如果获取的字符不是数字、空格或EOL,则警告用户
    • 第四条if语句:如果获得的字符是EOL,则结束循环
    下面的代码工作正常

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(){
    #define BUFSIZE 50
    #define ARRAYSIZE 5
        int i,k,a[ARRAYSIZE];
        char c,buf[BUFSIZE];
    
        for(i=0,k=0;(i<BUFSIZE)&&(k<ARRAYSIZE);){
            c=getchar();
            if(isdigit(c)){
                buf[i++] = c;
            }else if((i>0) && (c==' ' || c=='\n')){
                buf[i] = '\0';
                a[k++] = atoi(buf);
                i=0;
            }else if(!(c==' ' || c=='\n')){
                printf("warning : invalid value %c is detected\n",c);
                i=0;
            }
            if(c=='\n'){
                break;
            }
        }
        printf("input :");
        for(i=0;i<ARRAYSIZE;i++){
            printf("%d, ",a[i]);
        }
        printf("\n");
    }
    
    #包括
    #包括
    #包括
    int main(){
    #定义BUFSIZE 50
    #定义数组化5
    inti,k,a[ARRAYSIZE];
    字符c,buf[BUFSIZE];
    
    对于(i=0,k=0),(i最好在
    strtok()
    中使用
    isspace()
    的各种空白,以便与
    isspace(()
    一致,而不仅仅是
    )。