Thread: for loop problem. (again)

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    25

    for loop problem. (again)

    I need help figuring out how to make this for loop print 25 times no matter what. The problem is to: generate a table of conversions from Celsius to Rankin, allow the user to enter starting temp and increment. Print 25 lines. Use a for loop in your solution.

    Code:
    #include<stdio.h>
    
    
    int main()
    {
    	//Variable declarations
    	double ra;
    	double c,inc;
    	
    	//getting the values from the user
    	printf ("Enter the starting temperature in degrees Celsius: ");
    	scanf("%lf", &c);
    	printf("Enter the increment: ");
    	scanf("%lf", &inc);
    
    	printf ("\nCelsius\t\t Rankin\n");  //printing the label for the table
    	printf ("-----------------------------\n");
    	
    	//Print celsius an rakin and calculate rankin in a loop
    	for(c=c;c<=25*inc;c+=inc)
    	{
    		ra = (1.8*c)+32+459.67;  //converting celsius to rankin
    		printf ("%7.3f\t\t %7.3f\n",c,ra);  //printing 1 row of values
    	}
    
    
    	//Exit program
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Now what's the output that you're getting?
    for(c=c; // init is optional since c is already init'ed

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    well it depends on the inputted numbers, for bigger numbers I was getting 25 rows, but with the test value I must use for my homework (c=15 and inc=2.5) I was only getting 20.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't expect there to be 25 lines every time if you have it incrementing by a random increment every time. If you start at 0 and increment by 5 every time, you can't stop at 25 because that will only give you 5 lines. You need to just control your loop like so:
    Code:
    for( x = 0; x < 25; x++ )
        ...then use 'inc' here for your calculation, not for your loop control
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    Thanks quzah, I was in the middle of trying that when you posted. Got it down.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM