Here is what I am doing. I have posted this a few times over the last few days and you guys have been great with help but I am just getting so frustrated.
I am building an array that will take up to 10 numbers. If a user enters a zero I want to exit and stop taking numbers. If a user enters a letter I want to ignore it and ask for the next number. After the user enters all the numbers I want to give them the sum. I just can't make this work. I have made so many changes and tried so many things and yet I am getting more frustrated.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 10 /* size of array */
int verify (char* value) /* this function will verify the input is a number*/
{
int iChars = strlen (value);
int i;
for (i=0; i<iChars; i++) {
if (! isdigit (value [i])) {
return 0; /*Not a number*/
}
return 1; /*Is a number*/
}
}
int main()
{
int i, arr, sum, average;
char temp[50];
/* get the numbers */
printf("Enter up to 10 numbers, to stop entering numbers enter a zero. \n");
for(i=0; i<SIZE; )
{
if(fgets(temp, sizeof(temp), stdin) == NULL) /*make sure there has been input*/
{
printf("Please enter a valid number\n");
return 1;
}
if(!verify(temp))
{
printf("Invalid input, please try again\n");
}
else
{
arr[i] = atoi(temp); /*convert char to int */
if (!arr[i]) {
break;
}
++i; /* next number */
}
}
return 0;
}