Home > database >  How do you make a table in C console output?
How do you make a table in C console output?

Time:01-21

This is what my Professor is asking us to do:

Write a program that prints out PI as a type double and a type float EXACTLY as shown below. Your program should have ONE cout statement within a loop which alters the width and precision each time through the loop. Do not write 10 separate cout statements for this problem. Show the output in a table that increases the precision from 1 to 10 as shown below. Use dash ('-') as the fill character. You will need the following manipulators: fixed, setw( W), setprecision( P) and setfill( ).
Use the following two variables for PI.
double PI_D = 3.14159256359;
float PI_F = 3.14159256359; Expected: Expected code from Professor

What I've done so far is not the greatest and I'm fully aware, it's one of my first times working with C :

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main(void)
{
    double PI_D = 3.14159256359;
    float PI_F = 3.14159256359;

    cout << "     DOUBLE" << "          FLOAT" << endl;
    for (int i = 1; i <= 10; i  )
    {
        cout << setfill('-') << setw(i 9) << setprecision(i 1) << PI_D << setw(i 14) <<  
                setprecision(i) << fixed << PI_F << endl;
    }

    system("pause");
    return 0;
}
     DOUBLE          FLOAT
-------3.1------------3.1
------3.142------------3.14
------3.1416------------3.142
------3.14159------------3.1416
------3.141593------------3.14159
------3.1415926------------3.141593
------3.14159256------------3.1415925
------3.141592564------------3.14159250
------3.1415925636------------3.141592503
------3.14159256359------------3.1415925026

My output

CodePudding user response:

You should be able to use std::format for this task. It is really powerful and part of the C standard now.

The format for the first number is {:->{}.{}f} which means:

  1. '-' pad with dashes
  2. '>' right aligned
  3. '{}.{}' specify width and precision
  4. 'f' floating point

The second number is the same, only that the width is hardcoded at 15 characters.

#include <iostream>
#include <format>

int main(void)
{
    double PI_D = 3.14159256359;
    float PI_F = 3.14159256359;
    std::cout << std::format("{:>11s}{:>15s}\n","DOUBLE","FLOAT");
    for (int i = 1; i <= 10; i  )
    {
        std::cout << std::format("{:->{}.{}f}{:->15.{}f}\n",PI_D,9 i,i,PI_F,i);
    }
    return 0;
}

It produces:

Program returned: 0
Program stdout
     DOUBLE          FLOAT
-------3.1------------3.1
-------3.14-----------3.14
-------3.142----------3.142
-------3.1416---------3.1416
-------3.14159--------3.14159
-------3.141593-------3.141593
-------3.1415926------3.1415925
-------3.14159256-----3.14159250
-------3.141592564----3.141592503
-------3.1415925636---3.1415925026

Godbolt: https://godbolt.org/z/5o4xE5d5Y

If you do not have a compiler with full C 20 and std::format you can resort to std::snprintf() as in

#include <iostream>
#include <algorithm>

int main(void)
{
    double PI_D = 3.14159256359;
    float PI_F = 3.14159256359;
    char buf[256];
    std::snprintf( buf, sizeof(buf), "ss\n", "DOUBLE", "FLOAT" );    
    std::cout << buf;
    for (int i = 1; i <= 10; i  )
    {
        size_t len = std::snprintf( buf, sizeof(buf), "%*.*f .*f\n", 9 i, i, PI_D, i, PI_F );
        std::replace( buf, &buf[len], ' ', '-' );
        std::cout << buf;
    }
    return 0;
}

Prints

Program returned: 0
Program stdout
     DOUBLE          FLOAT
-------3.1------------3.1
-------3.14-----------3.14
-------3.142----------3.142
-------3.1416---------3.1416
-------3.14159--------3.14159
-------3.141593-------3.141593
-------3.1415926------3.1415925
-------3.14159256-----3.14159250
-------3.141592564----3.141592503
-------3.1415925636---3.1415925026

Godbolt: https://godbolt.org/z/rsT7YvPYj

  • Related