I'm busy with a code (in the learning phases) and everything is working just fine, but I wanted to know how you can print the number of times a program had to loop (loop_count).
The program:
The point of the program is that the user will be prompted to enter a score(s). The program will stop prompting for the score or end the loop when the user enters a negative integer.
Once the loop has ended, the number of scores will be counted and printed in the following text.
Lets say the user entered 8 > 7 > 5 > -4 // the negative is ignore and not added to the
//total scores counted.
You entered 3 scores.
Example:
Code:
#include <stdio.h>
int main()
{
int user_input = 1;
int loop_count;
printf("The program calculates the average of scores you enter.\n");
printf("End with a negative integer.\n"); //note that the loop ends when the user
//enters a negative integer
do
{
printf("Enter score (3-10):");
user_input++;
scanf("%d", &user_input);
}
while(user_input > -1);
loop_count = ??? ; //I'm not sure how to construct this. The loop_count should not
//include the negative integer, used to exit the loop
printf("You entered %d scores.\n", loop_count);
return 0;
}
The final product should look like this:
Code:
The program calculates the average of scores you enter.
End with a negative integer.
Enter score (3-10):5
Enter score (3-10):8
Enter score (3-10):8
Enter score (3-10):6
Enter score (3-10):5
Enter score (3-10):7
Enter score (3-10):5
Enter score (3-10):-1
You entered 7 scores.