Home > OS >  How to center numbers and add list
How to center numbers and add list

Time:11-11

i made a program for converting Celcius to Fahrenheit and have some troubles with modifying it. I need to all numeration for each line and center numbers in lines. rn my program outputs all to left. Can you please help me with this?

#include <stdio.h>
#include <iostream>

int main(){
// Table header
setlocale(LC_ALL, "");
system("color F0");
printf("\tTable Output\t");
printf("\n----------------------------------\n");
printf("| Celcius \t | Fahrenheit \t |\n");
printf("----------------------------------\n");

// Table body
for (double i = 15; i <= 30; i  )
{
    printf("| %.f\t\t | %.1f\t\t |\n", i, 1.8 * i   32);
}

printf("----------------------------------\n");
printf("\n\tList output\n");
using namespace std;
int main();
{
    for (double i = 15; i <= 30;   i)
    {
        cout << "Celcius: " << i << " --> Fahrenheit: " << 1.8 * i   32 << endl;
    }
}
return 0;

}

CodePudding user response:

As I already stated in the comments, 'padded output' may be able to help you. There's some finetuning to be done here, maybe use sprintf to get the number of characters and prepend/append spaces accordingly, but here you go with a C-Style answer.

setlocale(LC_ALL, "");
system("color F0");
printf("\tTable Output\t");
printf("\n----------------------------------\n");
printf("|   Celcius  | Fahrenheit |\n");
printf("----------------------------------\n");

// Table body
for (int i = 15; i <= 30; i  )
{
    printf("| % 6d     |   % 6.1f   |\n", i, 1.8 * i   32);
}
  •  Tags:  
  • c
  • Related