I need to format my BMR output so it has a nice neat appearance with the Headings over each column. In one column it should say BMR and the other Daily Calorie Intake. This is what I have so far:

/* Program: Harris Benedict Equation
 * 
 */
 
 # include <fstream>
 # include <iostream>
 # include <iomanip>
 
 using namespace std;
 
 //prototypes
 void Get_Data (ifstream&, int&, int&, int&, char&, int&); // Reads in data from file 
 int Basal_Meta_Rate (int, int, int, char); // Weight, Height, Age, and Sex
 int Caloric_Needs (int, int); // BMR and Activity Level
  
 
 int main ()
 {
     ifstream data;
     ofstream out;
     int wgt, hgt, age, act_lvl;
     char sex;
     int bmr, calories;
     
     data.open ("bmr_inputs.txt"); //file for input
     if (!data)
        {
               cout << "Error!!! Failure to Open bmr_inputs.txt" << endl;
               system ("pause");
               return 1;
        }
     out.open ("out.txt"); //file for output
     if (!out)
        {
              cout << "Error!!! Failure to Open out.txt" << endl;
              system ("pause");
              return 1;
        }
        
      Get_Data (data,wgt, hgt, age, sex, act_lvl);
     
     while (data)
        {
                 
                 bmr = Basal_Meta_Rate (wgt, hgt, age, sex);
                 calories = Caloric_Needs (bmr, act_lvl);
                 out<< "bmr " << bmr << "  calories " << calories <<endl;
                 Get_Data (data,wgt, hgt, age, sex, act_lvl);
                 
        }
       
        
              cout << "The End..." <<endl;
              data.close ();
              out.close ();
              system ("pause");
              return 0;                      
 } //main   
    
 void Get_Data (ifstream& data, int& wgt, int& hgt, int& age, char& sex, int& act_lvl)
 {
     data>> wgt;
     data>> hgt >> age >> sex >> act_lvl;
     
 }
 int Basal_Meta_Rate (int wgt, int hgt, int age, char sex) 
     {
         if (sex == 'M')
                 {
                 return (66 + (6.23 * wgt) + (12.7 * hgt) - (6.76 * age));
                 }
                 else 
                 {
                 return (655 + (4.35 * wgt) + (4.7 * hgt) - (4.7 * age));
                 }
         
     }
     
 int Caloric_Needs (int bmr, int act_lvl)
     {
                    int calories;
                    
                    if (act_lvl == 1)
                    {
                       calories = (bmr * 1.2);
                    }
                    else if (act_lvl == 2)    
                    {                
                       calories = (bmr * 1.375);
                    }
                    else if (act_lvl == 3)
                    {
                       calories = (bmr * 1.55);
                    }
                    else if (act_lvl == 4)
                    {
                       calories = (bmr * 1.725);
                    }
                    else if (act_lvl == 5)
                    {
                       calories = (bmr * 1.9);
                    }
                    return (calories);
     }

I have attched my input file as well. Thanks so much!!!

Recommended Answers

All 4 Replies

This might be what you're looking for; http://www.cplusplus.com/reference/iostream/ios_base/width/

You can set the width of an output field with it. In the example on that page, note that after setting the wdith to 10, the output ("100" in the example) then takes up the 8th, 9th and 10th positions of the output. You can use 'left', 'right', 'internal' and other such to alter the positioning in a field, and of course you can pad it yourself with blank space.

you can use the /t and /n in your output e.g
cout << "WORD/t" << "OPPOSITE/n";
cout <<"go/t" <<"come/n";
cout <<"boy/t" << "girl/n";
The output will be :
WORD OPPOSITE
go come
boy girl

Ok, thanks! I'll check that out and see if I can make some nice headings. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.