CODE:
#include <stdio.h>
main() {
int nums[100], i;
char answer;
int count = 0;
double avg;
for (i = 0; i < 100; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &nums[i]);
printf("Another? ");
scanf("%c", &answer);
count += nums[i];
}
}
RUN:
~> a.out
Enter number 1: 1
Another? Enter number 2: 2
Another? Enter number 3: 3
Another? Enter number 4: 4
Another? Enter number 5: 5
Another? Enter number 6: 6
Another? Enter number 7: 7
Another? Enter number 8: 8
Another? Enter number 9:
It’s supposed to ask me if I want to enter another number, but for some reason the scanf is not working. Also, I need to make it so that the user can enter 100 numbers, or any number under that, being prompted with a question of “do you want to enter another number”. If the answer is no, it terminates, if it is yes, it carries on.
Your first scanf leaves a newline in the buffer. It’s because
%dignores trailing blanks while%cdoesn’t. Use this cheap trick to make the secondscanfeat the blanks:The issue is common enough, you can read more about it in the C FAQ.