#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????
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 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
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;
}
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
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;
}
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?
#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;
}
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).
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.