从用户采取输入并将其存储在一个阵列 [英] Taking Input from user and storing it in an array

查看:119
本文介绍了从用户采取输入并将其存储在一个阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很新的C所以这将是巨大的,如果有人能告诉我怎么可以这样做:
我尝试使用与fgets来获取用户输入。因此,如果用户输入了以下的整数:
1 2 3 4 5

我如何扫描,然后把它们放在int数组= [1,2,3,4,5]

下面是我想现在做的事:我不断地循环,并要求用户输入一个数字,直到用户不把任何东西。在每次迭代中,我保存的用户的输入到阵列

  INT myArray的[200];
烧焦input_str [1024];fprintf中(标准输出,输入数字:);如果(与fgets(input_str,sizeof的(input_str),标准输入)== 0){
    fprintf中(标准错误,错误从与fgets()返回... \\ n);
    出口(1);
}sscanf的(input_str,%D,&安培; N);
myArray的[I] = N;// while循环就会一直继续担任该字符串不为空
而(input_str [0]!='\\ 0'){    如果(与fgets(input_str,sizeof的(input_str),标准输入)== 0){
        打破;
    }
    对于(i = 1; I< 201;我++){
        sscanf的(input_str,%D,&安培; N);
        myArray的[I] = N;
    }
}


解决方案

 的#include<&stdio.h中GT;诠释主要(无效){
    INT myArray的[200];
    烧焦input_str [1024];
    INT I,N = 0;
    为size_t全= sizeof的(myarray的)/的sizeof(* myArray的);    fprintf中(标准输出,输入数字:);    而(与fgets(input_str,sizeof的(input_str),标准输入)!= NULL){
        的char * p = input_str;
        INT V,len个;        如果(* input_str =='\\ n'|| n ==可满)
            打破; //输入端,如果空行        而(1 ==的sscanf(P,%D%N,&安培; V,和放大器; LEN)){
            myarray的[N +] = V;
            P + = LEN;
            如果(N ==满)
                打破;
        }
    }
    //检查打印
    对于(i = 0; I< N;我++)
        的printf(%d个,myArray的[I]);
    返回0;
}

I'm really new to C so it would be great if someone could tell me how I can do this: I am trying to get user input using fgets. So if the user enters the following integers: 1 2 3 4 5

How do i scan them and put them in an array of ints= [1,2,3,4,5]

Here is what I am trying to do right now: I am continuously looping and asking the user to input a number until the user does not put anything. In each iteration, I am saving the input of the user to the array

int myArray[200];
char input_str[1024];

fprintf(stdout, "Enter numbers: ");

if (fgets(input_str, sizeof(input_str), stdin) == 0){
    fprintf(stderr, "Error returned from fgets()...\n");
    exit(1);
}

sscanf(input_str, "%d", &n);
myArray[i]=n;

//The while loop will continue as long as the string is not null
while( input_str[0]!='\0'){

    if (fgets(input_str, sizeof(input_str), stdin) == 0){
        break;
    }
    for (i=1;i<201;i++){
        sscanf(input_str, "%d", &n);
        myArray[i]=n;
    }
}

解决方案

#include <stdio.h>

int main(void){
    int myArray[200];
    char input_str[1024];
    int i, n = 0;
    size_t full = sizeof(myArray)/sizeof(*myArray);

    fprintf(stdout, "Enter numbers: ");

    while(fgets(input_str, sizeof(input_str), stdin) != NULL){
        char *p = input_str;
        int v, len;

        if(*input_str == '\n' || n == full)
            break;//input end if blank line

        while(1==sscanf(p, "%d%n", &v, &len)){
            myArray[n++] = v;
            p += len;
            if(n == full)
                break;
        }
    }
    //check print
    for (i=0;i<n;i++)
        printf("%d ", myArray[i]);
    return 0;
}

这篇关于从用户采取输入并将其存储在一个阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆