Originally Posted by
nadroj
If you want further help, post your complete (100% complete) code, as well as what your input and output is. Describe any other details we must know in order to help. If you leave any one of these out, it will only be hurting you because the help will be delayed until we get all of that information.
Well...The below code works just fine, but when I implement the advised code, that would just loop until I break it off...
The code that works:
Code:
#include <stdio.h>
int main()
{
int user_input = 1;
int loop_count = -1;//starts with -1 because the exit input (final input) is ignored in the count
float sum = -1;
printf("\nThe program calculates the average of scores you enter.\n");
printf("End with a negative integer.\n");
printf("\n");
do
{
printf("Enter score (3-10): ");
sum += user_input;//used to determine the sum for the average
loop_count++;
scanf("%d", &user_input);
}while(user_input >= 0);
printf("\nYou entered %d scores.\n", loop_count);
printf("Average score: %.2f\n", sum/loop_count);
return 0;
}
When I put the scanf and while statement together, as advised, it doesn't work:
Code:
}while(scanf("%d", &user_input)!<=-1);
or
Code:
}while(scanf("%d", &user_input)>=0);
Oh and the output will just keep printing this line (no matter the user input - positive or negative):
Code:
printf("Enter score (3-10): ");
The code should exit the loop when a negative integer is entered and it would print:
Code:
printf("\nYou entered %d scores.\n", loop_count);
printf("Average score: %.2f\n", sum/loop_count);