If you look at your original code:
Code:
int sum_function()
{
char std_array[20];
int total;
int z;
int sum;
for (z=0; z<20;Z++){
scanf("%s",std_array);
total=atoi(std_array;
sum=sum+total;
}
printf("The sum of thes numbers is %i\n",sum);
return 0;
}
You are not using an array to store 20 numbers. You store one number, convert it, then store a new number where the old number was. Either use Prelude's original solution, or:
You could use the function isdigit() to check the string:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int sum_function();
int main(void)
{
sum_function();
return 0;
}
int sum_function()
{
char std_array[20];
int total;
int i;
int sum = 0;
bool quit = false;
do {
scanf("%s",std_array);
for (i=0; i<strlen(std_array); i++)
{
if (!isdigit(std_array[i]))
{
quit = true;
break;
}
}
if (!quit)
{
total = atoi(std_array);
sum += total;
}
} while (!quit);
printf("The sum of these numbers is %i\n",sum);
return 0;
}