Counting loops

This is a discussion on Counting loops within the C Programming forums, part of the General Programming Boards category; I'm busy with a code (in the learning phases) and everything is working just fine, but I wanted to know ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    33

    Counting loops

    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.

  2. #2
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Add a variable inside the loop previously initialized to 0 and increment it each time loop gets executed.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I have my "blind to what is in front of my face" moments too, so here's a clue that will hopefully ring those bells:

    It is extremely similar to the number of the times the loop iterates:
    Code:
    int loop_count = 0;
    	do
    	{
    		printf("Enter score (3-10):");
    		user_input++;
                    loop_count++;  // this happened once each time
    		scanf("%d", &user_input);
    	}
    	while(user_input > -1);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Code:
    int times = 0;
    
    do
    	{
    		printf("Enter score (3-10):");
    		user_input++;
    		scanf("%d", &user_input);
                    times++;
    	}
    ...

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    brilliant!! I had tried loop_count++ before and it gave me a ridiculous (large) number and I now realize (thanks to you two), I had forgotten to declare: int loop_count = 0;

    Thank you all!

    The final code checks correct and looks like this:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int user_input = 1, loop_count = 0;
    	float loop_count_average; //still working on this
    	
    	printf("The program calculates the average of scores you enter.\n");
    	printf("End with a negative integer.\n");
    	
    	do
    	{
    		printf("Enter score (3-10):");
    		user_input++;
    		loop_count++;
    		scanf("%d", &user_input);
    	}
    	while(user_input > -1);
    	
    	loop_count = loop_count--;//filters the negative integer
    	
    	printf("You entered %d scores.\n", loop_count);
            printf("Average score: %.2f\n");//still working on this
    	
    	return 0;
    }
    Just out of curiosity, what does the void in int main(void) mean?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Even / Odd Counting with Loops
    By chris515nuke in forum C Programming
    Replies: 7
    Last Post: 11-09-2008, 04:24 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  3. Counting with loops up, down, up, ... ets
    By at0m1sk in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2007, 08:01 PM
  4. Counting in loops?
    By chuy in forum C Programming
    Replies: 4
    Last Post: 10-17-2005, 02:56 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21