Thread: A little question about formatted output

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

    A little question about formatted output

    Just wondered if this will put the titles in tabular format:

    printf("%13s%13s%13s\n", "Car", "Hours", "Charge");

    if not, what's wrong with it?
    thanks,
    Extro

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Why don't you tell us if it does what you want or not and if not what does it not do that you want it to. We arn't compilers for you personnel use.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Just wondered if this will put the titles in tabular format
    Why not test it and see? Yes, it will, to a point.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    68
    Quote Originally Posted by orbitz
    Why don't you tell us if it does what you want or not and if not what does it not do that you want it to. We arn't compilers for you personnel use.
    Comments like thisdont help me, note the rules:


    "Keep in mind that being helpful does not give license to be rude."

    I normally wouldn't care but I get this all the time...


    To the people interested in helping here's my code if it helps understand what I'm trying to accomplish:

    Code:
    #include <stdio.h>
    
    float calculateCharges (int hours, float charge);
    int main()
    {
    int car =0;
    int hours;
    float charge;
    
       
    	
    
       while (car <= 3){
    	printf("How many hours was the car parked for?: ");
        scanf("%d", &hours);
    	printf("%s%13s%13s\n", "Car", "Hours", "Charge");
    	++car;
    
            if(hours < 3){
               charge = 2;
    		   printf("%d%13d%13f\n", car, hours, charge);
    }
            if(hours > 3){
               printf("%d%13%13f\n" , car, calculateCharges(hours, charge));
    }
            if(hours >= 24){
            charge = 10;
            printf("%d%13d%13f\n", car, hours, charge);
    }
      
    }
    return 0;
    }
    float calculateCharges (int hours, float charge)
    {
    return charge = 2 + ((float)hours * 1 /2 );
    
    }
    I just ran it again and it print the first statement but not the second, no idea why.
    Any help is appreciated,
    Extro
    Last edited by Extropian; 08-18-2005 at 06:33 PM.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Is this kind of what you are looking for?

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int hours[3];
    
    	//////////
    	// Get user input
    	///////////
    	for(int car = 0; car < 3; car++) 
    	{
    		printf("How many hours was the car[%d] parked for?: ", car);
    		scanf("%d", &hours[car]);
    	}
    
    	/////////
    	// Print out stats
    	//////////
    	printf("%-13s%-13s%-13s\n", "Car", "Hours", "Charge");
    	for(car = 0; car < 3; car++)
    	{
    		if(hours[car] <= 3) 
    			printf("%-13d%-13d%-13f\n", car, hours[car], 2.0);
    		else if(hours[car] > 3) 
    			printf("%-13d%-13d%-13f\n", car, hours[car], 2 + (hours[car] * .5));
    		else if(hours[car] >= 24) 
    			printf("%-13d%-13d%-13f\n", car, hours[car], 10);
    		else printf("\n\n\twtf");
    	}
    	return 0;
    }
    The output:
    Code:
    How many hours was the car[0] parked for?: 20
    How many hours was the car[1] parked for?: 5
    How many hours was the car[2] parked for?: 2
    
    Car          Hours        Charge
    0            20           12.000000
    1            5            4.500000
    2            2            2.000000
    Here's a nifty little format string (not the exploit...) reference: http://www.cplusplus.com/ref/cstdio/printf.html

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Comments like thisdont help me, note the rules:
    Aside from being a tad rude, orbitz does make a good point. If you don't tell us what you expect, we can't tell you how to make the code do it.

    >I normally wouldn't care but I get this all the time...
    This is sign that you're asking the wrong questions, or you're asking questions that can be more easily answered through other means than posting a thread on a forum.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    68
    Thank you very much tonto I'm sure this will help, I'll compare your code to mine and see if I can fix it.
    The only difference is it shoud only output:
    Car Hours Charge
    0 20 12.000000
    1 5 4.500000
    2 2 2.000000

    I'm also supposed to use a user defined function (gotta show we know how to use them)

    i"ll let ya know if I get it.
    Thanks again,
    bryan

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I don't see a difference between the output you expected and the one I posted. Except mine had questions. I mean, you do want the user to enter the number of hours and such right? I just copied the entire programs run history in the console to here, so the questions aren't technically part of the feedback of your program.

    On another note, it is important to ask more specific questions than:
    Quote Originally Posted by You
    Just wondered if this will put the titles in tabular format:

    printf("%13s%13s%13s\n", "Car", "Hours", "Charge");

    if not, what's wrong with it?
    thanks,
    Extro
    This makes the reader think that you made no attempt to try for yourself, in addition to the fact that it is extremely vague and doesn't make much sense. Anyways, uh, good luck on your assignment, and take into consideration what the others have said here. Make your questions clear, concise, thought through, and whatever else makes a good question.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Extropian
    Comments like thisdont help me, note the rules
    You missed the rule that states:

    "Don't be such a lazy ........ing idiot. Try something for yourself once in a while you lazy ass!"

    Quote Originally Posted by Extropian
    I normally wouldn't care but I get this all the time...
    Then maybe you should take a hint and start:
    a) Asking smarter questions (link's already been provided for you).
    b) Stop being so lazy, and try something yourself.

    Why, since you are such a stickler for rules, here's another one, aside from the first one I mentioned, that you should read:Now you may say "well this isn't homework so it doesn't apply". Well it does apply. Obviously it applys a great deal, since you have stated:
    Quote Originally Posted by Extropian
    I normally wouldn't care but I get this all the time...
    So take a hint, grab a clue, and try your call again.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    68
    I'm so close to being done with my code for this one but my function is only retuning 2.000. I really wrapped my brain around this one and can't seem to figure out why it won't work.

    Any thoughts? I'm new to user defined functions (and I'm required to use one here)

    Regards,
    Extro

    Code:
    #include <stdio.h>
    
    float calculateCharges(float charge);
    
    int hours;
    float charge; 
    int main()
    {
    
       int hours[3];
     
    	for(int car = 0; car < 3; car++) 
    	{
    		printf("How many hours was the car[%d] parked for?: ", car);
    		scanf("%d", &hours[car]);
    	}
    
    
    	printf("%-13s%-13s%-13s\n", "Car", "Hours", "Charge");
    	for(car = 0; car < 3; car++)
    	{
    		if(hours[car] <= 3) {
    			charge = 2.0;
    			printf("%-13d%-13d%-13f\n", car, hours[car], charge);
    		}
    
    		if(hours[car] > 3) {
    		printf("%-13d%-13d%-13f\n", car, hours[car], calculateCharges(charge));
    		}
    		if(hours[car] >= 24) {
    			printf("%-13d%-13d%-13f\n", car, hours[car], 10);
    		}
    		 printf("\n\n");
    		
    	}
    	return 0;
    }
    float calculateCharges(float charge)
    {
    	
    		charge = 2 + ((float)hours * 1 / 2);
    		return charge;
    			
    }

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I wouldn't recommend using global variables to help confuse yourself.

    Shouldn't the function calculate a charge based on the number of hours? Perhaps like this?
    Code:
    float calculateCharges(int hours)
    {
       float charge = 2.0F;
       if ( hours > 24 )
       {
          return 10;
       }
       else if ( hours > 3 )
       {
          charge += hours / 2.0F;
       }
       return charge;
    }
    Then your loop could be simplified.
    Code:
       printf("%-13s%-13s%-13s\n", "Car", "Hours", "Charge");
       for ( car = 0; car < 3; car++ )
       {
          printf("%-13d%-13d%-13f\n", car, hours[car], calculateCharges(hours[car]));
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output filename / Char* question.
    By Fidicor in forum C Programming
    Replies: 3
    Last Post: 04-29-2009, 09:56 PM
  2. a output question
    By ok_good in forum C Programming
    Replies: 4
    Last Post: 10-07-2007, 03:45 AM
  3. rcp error output question
    By cristane in forum Linux Programming
    Replies: 1
    Last Post: 07-18-2005, 09:47 PM
  4. Simple question on quoting output
    By LouB in forum C++ Programming
    Replies: 13
    Last Post: 06-16-2002, 02:57 PM
  5. To all who read last post formatted output
    By spliff in forum C Programming
    Replies: 8
    Last Post: 08-21-2001, 03:37 AM