I am confused how to have a user enter up to 20 numbers into an array and then end the input with any word. any suggestions?
I am confused how to have a user enter up to 20 numbers into an array and then end the input with any word. any suggestions?
What have you tried? Post your code (in code tags).
Can you write a program to read exactly 20 numbers into an array and print them back out?
This is what i have so far, but nothing ive tried has worked. Like, the program will exit once i enter a work or letter but i cant count how many numbers the user entered before the word
Code:#include <stdio.h> #include <stdlib.h> int main() { double numArray[25]; char wordArray[10]; int x = 0; int y = 0; setvbuf(stdout, NULL, _IONBF, 0); printf("Enter up to 25 positive reals (non-numeric to stop).\n"); scanf(" %c", &wordArray[y]); for (x = 0; x < 25; x++) { scanf("%lf", &(numArray[x])); if (numArray[x] == wordArray[y]){ break; } } printf("%d values were entered\n", x);
I don't see any reason for the setvbuf call or the wordArray array.
I think the insight you're looking for here is that scanf returns the number of values it was able to scan, which will be 1 in your case if it was able to read a number. So something like the following might work for you:
Code:if (scanf("%lf", &numArray[x]) != 1) break;