enter array element until pressing enter

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
int main(){

int list[50];
int i = 0;
int input;
while (i < 50){
scanf("%d",&input);
  if(input == '\n') break;
  list[i] = input;
  i++;
}
int a;
for (a=0;a<i;a++)
printf("%d ",list[a]);}


for the above code,I want to make a program that people enter the array element , then when he press enter,the program stop and then print out what he has just entered just now...
but my program cant work,anyone help me????
Last edited on
This kinda makes no sense to me. You must press enter to enter an array element right?

I mean, you type in the element, say the number 6, then you press enter. And thats how your program is built. So I dont understand how you would want it to stop once pressing enter?
i mean between each digit, use blank
start
8 9 1 4<<then enter
I have to use C program but not C++ program...
so I have to use something like
1
2
3
 char x; 
while (getchar(x)!='\n')
....

I don't quite sure,anyone help me to adjust my original code???thanks and it is urgent!!!!
So only C, not C++? I suggest looking at

gets
sscanf
strtok
i think it is a very difficult question,at least no one solve it as i have searched it in internet....
if using those gets,sscanf,strtok,we have only read those string,number...but not really can be calculated.....
maybe anyone can write the code so that I will realise
nah, that's quite an easy one ^^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(void)
{
    int list[50];
    int i = 0;
    int input;

// 10 is a LineFeed which is sent when pressing enter
    while((input = getchar()) != 10 && i < 50) 
    {
   	list[i++] = input - '0';
    }

    for (int j =0; j<i; j++)
        printf("%d ",list[j]);

    system("pause");

    return EXIT_SUCCESS;
}
Last edited on
Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
        enum { MAXSZ = 50 };
        int list[MAXSZ] ;
        int sz = 0 ;

        puts( "enter numbers one by one separated by white space; enter a new line to terminate input: " ) ;

        while( sz < MAXSZ )
        {
            int ch ;
            do
            {
                ch = fgetc( stdin );
                if( ch == '\n' || ch == EOF ) goto done ;

            } while( isspace(ch) ) ;

            ungetc( ch, stdin ) ;

            int number ;
            if( scanf( "%d", &number ) != 1 ) break ; // *** exit on non-integer input

            list[sz] = number ;
            ++sz ;
        }

    done:
        for( int i = 0 ; i < sz ; ++i ) printf( "%d ", list[i] ) ;
        puts( "" ) ;
}

http://coliru.stacked-crooked.com/a/765b2bea0bc52455
not this one ar.....
I mean reader enter the several number ,then after they press enter, the sum/mean will be printed out...
the difficulty is that the computer never know how many number the reader will put into until they pressed enter
Do you also want to be able to have two-digit numbers as input?

then just modify the code to your needs...
I'll do the sum for you! :b

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main(void)
{
    int list[50];
    int i = 0;
    int input;

// 10 is a LineFeed which is sent when pressing enter
    while((input = getchar()) != 10 && i < 50) 
    {
   	list[i++] = input - '0';
    }

    int sum = 0;
    for (int j =0; j<i; j++)
    {
        sum += list[j];
        printf("%d ",list[j]);
    }
    printf("\nsum: %d", sum);

    

    system("pause");

    return EXIT_SUCCESS;
}
Last edited on
wrong..you can compile before posting it...
when doing simple calculate sum of 1 to 8,sum is -76,which is wrong
and also,the computer don't know two-digit or 1-digit the reader enter,maybe there is 3-digit....
is there any code possible?
Last edited on
@Gamer2015
You need to ignore the whitespaces

@sirrunrun
what you can do is scanf a string ("%s") and use strtok to split the string. Then use atoi or sscanf to get the values.

http://www.cplusplus.com/reference/cstring/strtok/?kw=strtok
http://www.cplusplus.com/reference/cstdlib/atoi/?kw=atoi
http://www.cplusplus.com/reference/cstdio/sscanf/?kw=sscanf
oh,anyway I have solved the problem myself,thanks anyone!!!
Could you post your code here so if anyone looks through this post in the future they'll be able to see it?
In the interest of future people with the same question, my suggestion was basically the same as coder777's:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <string.h>

int main(void)
{
    char str[100];
    char *pch; //pointer for use with strtok
    int sum = 0, count = 0;
    
    printf("Enter some integers: ");
    fgets(str, 100, stdin); //like getline in C++
    pch = strtok(str, " ");
    while (pch != NULL)
    {
        int temp;
        sscanf(pch, "%d", &temp);
        sum += temp;
        ++count;
        pch = strtok(NULL, " ");
    }
    
    printf("The sum of the integers is %d.\n", sum);
    printf("The average of the integers is %.2f.", 1.0 * sum / count);
    
    return 0;
}
Last edited on
> gets(str); //like getline in C++

gets() is no longer part of standard C (deprecated earlier and removed by C11).
std::gets() is no longer part of standard C++ (deprecated earlier and removed by C++14).
Didn't know it was deprecated... What about gets_s then?
The gets() function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear on stdin). For this reason, the function has been deprecated in the third corrigendum to the C99 standard and removed altogether in the C11 standard. fgets() and gets_s() are the recommended replacements.

Never use gets().


http://en.cppreference.com/w/c/io/gets
I edited the code to use fgets now.
Pages: 12