Home > OS >  I want to print a "receipt" in C but can't figure it out
I want to print a "receipt" in C but can't figure it out

Time:01-08

I have been trying to figure out how to print this "receipt", but I don't know if it is possible without just printing everything on their own. This is what I want to print: https://pastebin.com/qxk18ARh (check the raw paste data):

****************************************************************************************************
*                                         PERSONAL DETAILS                                         *
*                                                                                                  *
*           Name:                                                   Tesla                          *
*           Surname:                                                Nikola                         *
*           Address:                                                Androutsou 150, Piraeus, 15232 *
*           Total days of consumption:                              179                            *
*           Sq. Meters:                                             110                            *
*                                                                                                  *
****************************************************************************************************                                    *                                               PRICING                                            *
*                                                                                                  *
*           Initial cost:                                           EUR 19.32                      *            
*           Details:                                                                               *    
*                   Municipal Taxes:                                EUR 7.05                       *                    
*                   Total Charges:                                  EUR 12.27                      *                
*           Final cost (after credit card payment deduction):       EUR 17.39                      *                                    
*                                                                                                  *
****************************************************************************************************                                    *                                          PAYMENT DETAILS                                         *                        
*                                                                                                  *
*           Credit Card Number:                                     1234 5678 9012 3456            *                                    
*                                                                                                  *                                    
****************************************************************************************************

Odd line breaks, trailing white space and overlong lines as found in the pastebin link, but probably not what is really wanted. The raw data contains many tabs, too.

I searched and found only how to print a hollow square with these for loops:

for (int i = 1; i <= 23; i  ) {
    for (int j = 1; j <= 23; j  ) {
        if (i == 1 || i == 23 || j == 1 || j == 23){
            printf("* ");
        }else{
            printf("  ");
        }
    }

    printf("\n");

But I am looking for a rectangle firstly, and I don't know how to put the 2 lines inside in this way. So I am mostly asking if it's possible in a more compact way like with loops and if yes how should I go about doing it. Any kind of help will be great. Thanks in advance.

CodePudding user response:

A nice way to print a form involves using variable widths.

A singular advantage is that it allows updates in a uniform manner.

To print:

*        Initial cost:            EUR 19.32  *
*        Details:                            *
*            Municipal Taxes:     EUR 7.05   *

Use:

int width1a = 8;
int width1b = width1a   4;
int width2a = 25;
int width2b = 25 - 4;
int width3 = 9;

// Notice the common format
printf("*%*s%-*s%-*s  *\n", width1a, "", width2a, "Initial cost:",    width3, "EUR 19.32");
printf("*%*s%-*s%-*s  *\n", width1a, "", width2a, "Details:",         width3, "");
printf("*%*s%-*s%-*s  *\n", width1b, "", width2b, "Municipal Taxes:", width3, "EUR 7.05");

More advance things like centering a title can the be done with computed widths.

CodePudding user response:

I would suggest that you should go with the easy way using plain strings like:

printf("*************************************************");

In general, whenever you are solving any programming problem, go with quick and dirty solution and then try to optimize it later if you have free time.

  •  Tags:  
  • Related