Getting the loop control form the user

This is a discussion on Getting the loop control form the user within the C Programming forums, part of the General Programming Boards category; A quick question: Normally I've seen that the programmer sets the loops control variable for a for statement. How does ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    68

    Getting the loop control form the user

    A quick question:

    Normally I've seen that the programmer sets the loops control variable for a for statement. How does one get the user to set it?
    Thanks,
    Extro

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    Code:
    int numtimes, i;
    printf("How many times do you want the loop to run: ");
    scanf("%d",&numtimes);
    for( i = 0; i < numtimes; ++i )
    {
        // Do stuff
    }
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    68
    I got it to work but any feedback is appreciated -can I make it better?(constructive criticism welcome).
    Regards,
    Extro

    Code:
    	int		num1, num2;		
    	int		integers;		/* How many numbers the user wants analyzed */
    	int		counter = 0;	/* Allows more than two numbers to be analyzed */
    	int		total;
    	int		smallest;		
    	int		largest;
    	float	average;
    	int total2;
    
    	printf("Enter number of digits to be analyzed: ");	/* Ask for how many digits are to be analyzed */
    	scanf("%d" , &integers);		
    
    	printf("Enter the first number: ");	
    	scanf("%d" , &num1);
    
    	for(counter = 1; counter < integers; counter++){	/* Allows the user to have several numbers analyzed */
    
    		printf("Enter the next number: ");
    		scanf("%d" , &num2);
    
    		
    		if(num1 > 2){			/* Sets the smallest and largest numbers */
    
    			largest = num1;
    			smallest = num2;
    					}
    		if(num2 > num1){
    
    			largest = num2;
    			smallest = num1;
    			}
    	
    					
    		if(integers < counter){
    			
    		
    		}
    			total = num1 + num2;				/* Totals the numbers */
    			
    			
    	}
    	total2 = total + num2;
    	average = (float)total2/counter;		/* Averages the numbers */
    	printf("\n");	/* Prints a blank line */
    	printf("The smallest number is %d\n " , smallest);	/* Prints the smallest number */
    	printf("The largest number is %d \n" , largest);		/* Prints the largest number */
    	printf("The total is %d\n" , total2);					/* Prints the total */
    	printf("The average is %f\n " , average);				/*	Prints the aeverage */
    
    	return 0;											/*Ends program */
    }
    Last edited by Extropian; 08-10-2005 at 08:35 PM. Reason: Managed to fix it

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,520
    Gnu indent would be a start.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    68
    Gnu indent?
    lol, ya lost me...

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    hey ,

    Y don't you make use of array to hold the numbers. ? you will be able to
    hold the list of number the user has entered.

    you can initialise small & large to say 0 and then compare the number you
    have read to with small and large and make the change to small and large..

    like

    Code:
     if( num <small)
           small=num;
        else
           {
              if(num>large)
                large=num;
           }
    you can also do it in online. with conditional operator..

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,150
    Code:
    you can initialise small & large to say 0
    That's a bad idea. What if the smallest number the user enters is 3. Since small was initialized to 0 then it's going to report false information. A better idea would be to initialize small to INT_MAX and large to INT_MIN.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a "transparent" User Control Background.
    By indigo0086 in forum C# Programming
    Replies: 2
    Last Post: 01-01-2008, 10:50 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  3. Instant messenger app help
    By AusTex in forum C Programming
    Replies: 2
    Last Post: 05-01-2005, 12:41 AM
  4. user input loop
    By asudave2302 in forum C++ Programming
    Replies: 11
    Last Post: 12-21-2004, 08:45 PM
  5. Replies: 11
    Last Post: 03-22-2003, 12:50 PM

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