0

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;

}

tstanisl
  • 13,520
  • 2
  • 25
  • 40
Dopaminos
  • 1
  • 2
  • Centering is kinda hard for me.. – Dopaminos Nov 10 '21 at 12:10
  • Well, assuming you know the number of spaces in your table, you could use something like 'padded output' (refer to `printf` for that, most notably the `% 6.02f` notation) to achieve your centering. (The above would (likely) pad (up to) 6 spaces to the left and stop at 2 digits after the decimal dot). – Refugnic Eternium Nov 10 '21 at 12:14
  • @Dopaminos Check out this below question. One of the answer shows here how you can centrally align your data. https://stackoverflow.com/questions/14765155/how-can-i-easily-format-my-data-table-in-c – Toothless Nov 10 '21 at 12:30

1 Answers1

0

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);
}
Refugnic Eternium
  • 4,089
  • 1
  • 15
  • 24