Home > Enterprise >  How do I setup the width?
How do I setup the width?

Time:11-22

We are given an assignment wherein we are supposed to output a given txt file and solve for the BMI. However, I am having problems with how to output the data as it is not following my desired alignment. Any help would be appreciated! I apologize if my question is very simple as I am not that good at programming.

Here is the txt file:

homework.txt

Here is my code:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <string>

using namespace std;

int main()
{
    string line = "";
    ifstream inFile;
    
    inFile.open("homework.txt");
    
    cout << setw (3) << "NO " <<
            setw (10) << "FIRSTN " <<
            setw (10) << "LASTN " <<
            setw (7) << "WEIGHT " <<
            setw (7) << "HEIGHT " <<
            setw (4) << "BMI " << endl ;
        
    if (inFile.is_open())
    {
        while (getline(inFile, line, '#'))
        {
            cout  << line << left << setw(10);
        }
    }
    
    inFile.close();
    return 0;
}

Here is the result:

Result

*I have not yet worked on with how the BMI would be computed and displayed.

CodePudding user response:

One possible way would be :

#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <string>

using namespace std;

int main()
{
    string no, firstin, lastin, weight, height;
    ifstream inFile;
    
    inFile.open("homework.txt");
    
    
    std::cout << std::setw(13) << std::setfill(' ') << "NO" 
    << std::setw(13) << std::setfill(' ') << "FIRSTIN" 
    << std::setw(13) << std::setfill(' ') <<"LASTIN" 
    << std::setw(13) << std::setfill(' ') << "WEIGHT" 
    << std::setw(13) << std::setfill(' ') << "HEIGHT" 
    << std::setw(13) << std::setfill(' ') <<"BMI" <<std::endl;   
        
    if (inFile.is_open())
    {
        //correct this for reading upto '\n'
        while (getline(inFile, no, '#'),
        getline(inFile, firstin, '#'),
        getline(inFile, lastin, '#'),
        getline(inFile, weight, '#'),
        getline(inFile, height, '\n') //read upto '\n' instead of '#'
        )
        {
                std::cout << std::setw(13) << std::setfill(' ') << no 
                << std::setw(13) << std::setfill(' ') << firstin 
                << std::setw(13) << std::setfill(' ') <<lastin
                << std::setw(13) << std::setfill(' ') << weight
                << std::setw(13) << std::setfill(' ') << height
                << std::setw(13) << std::setfill(' ') << "MBI" //write whatever bmi value here
                <<std::endl; 
            
        }
    }
    
    inFile.close();
    return 0;
}


The output of the above program is:

           NO      FIRSTIN       LASTIN       WEIGHT       HEIGHT          BMI
            1       Hannah         Cruz          130          5'4          BMI
            2     Franches       Ramire          137          5'6          BMI

which can be seen here.

Also note since your question specifically asked for alignment and not for how to calculate bmi, i have not calculated it in my above program. You can calculate it and then use that value in the last column instead of the hard coded string literal bmi.

  •  Tags:  
  • c
  • Related