Reputation: 5
I am attempting to use this code to ensure if a negative value is entered, it'll be recognised as an invalid input and require the user to retype the scores.
I am unsure why this isn't being found, though I do believe it may have to do with my array indexing. Here's my code:
#include <stdio.h>
#include <string.h>
double quiz_1(){
int test_scores[3];
double result;
printf("Please Enter The Students Test Score (e.g. 85 65 75): ");
for (int i = 0; i < (sizeof test_scores / sizeof test_scores[0]); i++) {
if(scanf("%d", &test_scores[i]) < 0){
printf("\nInvalid Input\nPlease Enter The Students Test Score (e.g. 85 65 75): ");
i = 0;
}
}
result = (test_scores[0] * 0.2) + (test_scores[1] * 0.3) + (test_scores[2] * 0.5);
printf("\nThe Student's Final Grade is %.2f", result);
return 0;
}
int main() {
quiz_1();
return 0;
}
Upvotes: 0
Views: 109
Reputation: 15386
scanf
returns the number of items of the argument list successfully filled. I think you're meaning to test if (test_scores[i] < 0)
.
Upvotes: 1