Thanks for clearing that up makes perfect sense now... I cant believe it still worked even with them. It works great now and I added continue in the IF loop so that it will keep going. I took out the else statement, because it was too much trouble to get it to work correctly and if a user put in a 10 it would only count the 1. Was running into that problem so here is the working code, btw what would be the fix if they entered a 10, I tried to use a
Code:
if (number < 0 || number > 9 || number == (double)number);
but it sadly did not work. Anyways here is my code it works now thought I might post it for others that might have trouble in the future
Code:
#include <stdio.h>
#define MAXLEN 5 /* sets the length of the array */
void getArray (int s[], int maxlen);
void sumArray (int s[], int maxlen);
main()
{
int array[MAXLEN]; /* places the integeres in the array */
int array2[MAXLEN];
getArray (array, MAXLEN); /* fills the array with the function getArray */
getArray (array2, MAXLEN);
sumArray (array, MAXLEN); /* sums and prints with the function sumArray */
sumArray (array2, MAXLEN);
}
void getArray ( int s[], int maxlen )
{
int i, number;
for (i=0; i < MAXLEN && i!= EOF; i++)
{
printf("Enter numbers between 0-9 \n"); /* Asks the users for a number 0-9 */
number = getchar() - '0'; /* sets the string to a int */
if (number < 0 || number > 9) /* looks to check to see if the number is 0 - 9 */
{
--i;
continue;
}
getchar();
s[i] = number; /* puts the number in the array */
}
}
void sumArray ( int s[], int maxlen )
{
int i, sum;
for (i=0; i<MAXLEN && i!= EOF; i++)
{ sum += s[i]; /* will count the sum and store in the array */
printf("%d ", s[i]); /* prints out the numbers til it gets to count 5 */
}
printf("= %d\n", sum); /* will print the sum */
}
/* expected output:
Enter numbers between 0-9
1
Enter numbers between 0-9
2
Enter numbers between 0-9
3
Enter numbers between 0-9
4
Enter numbers between 0-9
5
Enter numbers between 0-9
6
Enter numbers between 0-9
7
Enter numbers between 0-9
8
Enter numbers between 0-9
9
Enter numbers between 0-9
0
1 2 3 4 5 = 15
6 7 8 9 0 = 45 */