Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Introducing Spaces into printf statements (C language)?


  • Please log in to reply
11 replies to this topic

#1 altair05

altair05

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:12:55 PM

Posted 06 February 2011 - 02:08 AM

I would like to put in spaces in my print statements so that there are spaces when it runs. It's hard to explain so I'll try my best.


printf("%3c", 'g');

This would print:   g

With three spaces.

How can I do that with multiple characters, for example replace 'g' with QTY.

I could do the following as an alternative:

printf("   %c", 'g');


But I don't want to lose points for this, so is there a way to include with width specifications with multiple characters.

Edited by altair05, 06 February 2011 - 02:50 AM.


BC AdBot (Login to Remove)

 


#2 JosiahK

JosiahK

  • Members
  • 269 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:55 PM

Posted 06 February 2011 - 09:05 AM

Yes, there is a way.
As it's homework, I can't tell you the answer. But as a quick tip, what does the c mean in your statement? But what is "QTY" which 'P' isn't?
Try changing it to another letter.
Quod non mortiferum, fortiorem me facit.
I don't read minds. Please help everyone by answering any questions and reporting on the results of any instructions. Query any concerns and explain problems or complications.

#3 altair05

altair05
  • Topic Starter

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:12:55 PM

Posted 07 February 2011 - 03:19 AM

So instead of %c I would use %s, correct?

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:11:55 AM

Posted 07 February 2011 - 08:11 AM

The easiest thing to do would be to try it, right? :)

#5 strolln

strolln

  • Members
  • 400 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:N. Calif.
  • Local time:09:55 AM

Posted 07 February 2011 - 08:29 AM

You mean you never did the first program given in just about all programming books, printing "Hello World"? LOL
To Err is human; to really foul things up requires a Bleeping Computer!

#6 JosiahK

JosiahK

  • Members
  • 269 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:55 PM

Posted 07 February 2011 - 11:39 AM

strolln, I fail to see how printing "Hello World" would tell people how to pad the string with spaces.
Quod non mortiferum, fortiorem me facit.
I don't read minds. Please help everyone by answering any questions and reporting on the results of any instructions. Query any concerns and explain problems or complications.

#7 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:11:55 AM

Posted 07 February 2011 - 12:02 PM

No, but it would have told you which flag to use. :wink:

#8 JosiahK

JosiahK

  • Members
  • 269 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:55 PM

Posted 07 February 2011 - 12:12 PM

Ah, my mistake.
Quod non mortiferum, fortiorem me facit.
I don't read minds. Please help everyone by answering any questions and reporting on the results of any instructions. Query any concerns and explain problems or complications.

#9 altair05

altair05
  • Topic Starter

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:12:55 PM

Posted 07 February 2011 - 07:16 PM

Ok, I don't think I have solved it yet, but I'm working on it. This is what my code looks like so far.

23 #include<stdio.h>
 24 
 25 
 26 int main()
 27 {
 28   int tv;             //The number of Televisions bought by the consumer
 29   int bluRay;         //The number of Blu-Ray Players bought by the consumer
 30   int remote;         //The number of Remote Controls bought by the consumer
 31   int mp3;            //The number of MP3 Players bought by the consumer
 32   int laptop;         //The number of Laptop Computers bought by the consumer
 33 
 34   float tvTotal;      //The total price of Televisions bought
 35   float bluRayTotal;  //The total price of Blu-Ray Players bought
 36   float remoteTotal;  //The total price of Remote Controls bought
 37   float mp3Total;     //The total price of MP3 Players bought
 38   float laptopTotal;  //The total price of Laptop Computers bought 
 39 
 40   float subtotal;     //The total price of all purchases without tax
 41   float subtotalTax;  //The toal price of all purchases including tax
 42   float total;        //The total price includiding tax
 43 
 44   printf("\n");
 45   printf("Enter quantity of televisions: ");
 46   scanf("%d", &tv);         //Takes the number from the user and saves it in variable tv
 47 
 48   printf("Enter the quantity of Blu-ray players: ");
 49   scanf("%d", &bluRay);     //Takes the number from the user and saves it in variable bluRay
 50 
 51   printf("Enter quanitity of remote controls: ");
 52   scanf("%d", &remote);     //Takes the number from the user and saves it in variable remote 
 53 
 54   printf("Enter quantity of MP3 players: ");
 55   scanf("%d", &mp3);        //Takes the number from the user and saves it in variable mp3
 56 
 57   printf("Enter quantity of laptops: ");
 58   scanf("%d", &laptop);     //Takes the number from the user and saves it in variable laptop
 59 
 60 
 61   tvTotal = tv *  599.99;         //Calculates the total price of television using the unit price
 62   bluRayTotal = bluRay * 199.99;  //Calculates the total price of bluRay Players using the unit price
 63   remoteTotal =  remote * 29.99;  //Calculates the total price of remote controls using the unit price
 64   mp3Total =  mp3 * 44.99;        //Calculates the total price of MP3 Players using the unit price
 65   laptopTotal = laptop * 349.99;  //Calculates the total price of laptops using the unit price
 66 
 67   subtotal = tvTotal + bluRayTotal + remoteTotal + mp3Total + laptopTotal;    //Calculates the subtotal using the total prices     of all objects bought
 68   subtotalTax = subtotal * .07;   //Calculates the sales tax of 7% of the total merchandise 
 69   total = subtotal + subtotalTax;  //Adds the tax to the subtotal to arrive with the total price the consumer owes the seller
 70 
 71   printf("\nQTY  DESCRIPTION      UNIT PRICE  TOTAL PRICE");
 72   printf("\n---  -----------      ----------  -----------");
 73   printf("\n%3d  Televisions      $   599.99  $%10.2f", tv, tvTotal);         //Prints the quantity of electronics bought and t    he total price of those electronics(next four lines including current) 
 74   printf("\n%3d  Blu-ray Players  $   199.99  $%10.2f", bluRay, bluRayTotal);
 75   printf("\n%3d  Remote Controls  $    29.99  $%10.2f", remote, remoteTotal);
 76   printf("\n%3d  MP3 Players      $    44.99  $%10.2f", mp3, mp3Total);
 77   printf("\n%3d  Laptop Computers $   349.99  $%10.2f", laptop, laptopTotal);
 78   printf("\n%35c----------", '-');
 79   printf("\n%25cubtotal  $%10.2f", 'S', subtotal);
 80   printf("\n%30cax  $%10.2f", 'T', subtotalTax);
 81   printf("\n%28cotal  $%10.2f", 'T', total);
 82   printf("\n\n");
 83 
 84   return(0);
 85 }



Edited by altair05, 07 February 2011 - 09:29 PM.


#10 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:12:55 PM

Posted 08 February 2011 - 06:41 AM

Well, I have to praise your coding style. The variables are named according to camel notation. The comments, space and tabs are nicely done. There is consistency in your coding style throughout the program. Beautiful and easy to follow.
I would give you 50% marks just for this if I were your teacher, even if your program does not do what it was supposed to do.

Looking at your code, it appears that you have finally done what you wanted to do.

May I ask why your program starts with line #24 and not line #1 ?

Edited by Romeo29, 08 February 2011 - 06:41 AM.


#11 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:11:55 AM

Posted 08 February 2011 - 08:23 AM

My bet is that it is because the top of the program is also properly commented. +1 on your coding style also.

#12 altair05

altair05
  • Topic Starter

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:12:55 PM

Posted 21 February 2011 - 02:55 PM

It starts at line #24 because everything above is part of my schools academic integrity statement. We have to digitally sign in so we can be held liable if we copy another student. It also has things like our email addresses, which is how we submit our work and class section and division numbers so that our submitted files go to the right directory, and things like that.

And thanks for the compliments, I feel as if I worked hard and it payed off :)

Edited by altair05, 21 February 2011 - 02:56 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users