Link to home
Start Free TrialLog in
Avatar of krleonia
krleonia

asked on

C++ Outputting to a file

I've written a program to calculate employee salary, bonus and vacation time. But I'd like to output everything that appears on the DOS screen to a file. I've done input/output from files, but never directly outputted into a file. Heres my code as is and compiles with no errors, just need to figure out how to output everything on the dos screen to a file.



#include <iostream>
#include <iomanip>
using namespace std;

#define outFile "output.dat"

//function prototypes
void welcomeMessage();
void displayMenuGetEntry(short &);
double calcCommission();
double calcBonus();
double calcVacationTime();
void farewellMessage();

int main()
{
      //declare variables
      short menuEntry;
      double commission, bonus, vacationTime;

      //ensure no scientific notation in output
      cout << setiosflags(ios::fixed|ios::showpoint);

      //display welcome message
      welcomeMessage();

      //get users menu entry
      displayMenuGetEntry(menuEntry);

      //loop to process user menu entries
      while (menuEntry > 0)
      {
            //process user input
            if (menuEntry == 1)
            {
                  commission = calcCommission();
                  cout << setprecision(2);
                  cout << "Your commission is: $" << commission << endl;
            }
            else if (menuEntry == 2)
            {
                  bonus = calcBonus();
                  cout << setprecision(2);
                  cout << "Your bonus is: $" << bonus << endl;
            }
            else if (menuEntry == 3)
            {
                  vacationTime = calcVacationTime();
                  cout << setprecision(1);
                  cout << "You get " << vacationTime << " weeks vacation" << endl;
            }
            else if (menuEntry == 4)
            {
                                 // when 4 is entered, print all results so far to an output file
                                }

            else
                  cout << "+++ invalid entry +++" << endl;
            //end else if

            //get users next menu entry
            displayMenuGetEntry(menuEntry);
      }//end while

      //display good-bye message
      farewellMessage();

      return 0;
}//end main

void welcomeMessage()
{
      cout << "Greetings - welcome to the IC CIT2510 salesforce calculator" << endl << endl;
}//end welcomeMessage

void displayMenuGetEntry(short &choice)
{
      do
      {
            cout << "*************************" << endl;
            cout << "0 - quit" << endl;
            cout << "1 - compute commission" << endl;
            cout << "2 - compute bonus" << endl;
            cout << "3 - compute vacation time" << endl;
            cout << "4 - Print Results to output file" << endl;
            cout << "*************************" << endl;
            cout << "Enter choice: ";
            cin >> choice;
            if ((choice < 0) || (choice > 3))
                  cout << "--- invalid entry ---" << endl;
            //end if
      }while ((choice <0) || (choice > 3));
}//end displayMenuGetEntry

double calcCommission()
{
      //declare local variables
      double salesAmount, commission;
      short commissionCategory;

      //get sales amount
      cout << "What is your current sales amount? ";
      cin >> salesAmount;

      //get commission category code
      do
      {
            cout << "What is your commission category (1..5)? ";
            cin >> commissionCategory;
            if ((commissionCategory < 1) || (commissionCategory > 5))
                  cout << "--- invalid commission category ---" << endl;
            //end if
      }while ((commissionCategory < 1) || (commissionCategory > 5));

      //calculate commission
      if (commissionCategory == 1)
            commission = salesAmount * 0.01;
      else if (commissionCategory == 2)
            commission = salesAmount * 0.03;
      else if (commissionCategory == 3)
            commission = salesAmount * .05;
      else if (commissionCategory == 4)
            commission = salesAmount * 0.075;
      else
            commission = salesAmount * 0.1;
      //end else if

      return commission;
}//end calcCommission

double calcBonus()
{
      //declare local variables
      double salesAmount, bonus;
      short bonusCategory;

      //get sales amount
      cout << "What is your current sales amount? ";
      cin >> salesAmount;

      //get bonus category code
      do
      {
            cout << "What is your bonus category (1..3)? ";
            cin >> bonusCategory;
            if ((bonusCategory < 1) || (bonusCategory > 3))
                  cout << "--- invalid bonus category ---" << endl;
            //end if
      }while ((bonusCategory < 1) || (bonusCategory > 3));

      //calculate bonus
      bonus = salesAmount * bonusCategory * 0.05;

      return bonus;
}//end calcBonus

double calcVacationTime()
{
      //declare local variables
      double vacationTime, years;
      char employmentCode;

      //get sales amount
      cout << "How many years have you been with the company? ";
      cin >> years;

      //get employment category code
      do
      {
            cout << "What is your employment category code (a..d)? ";
            cin >> employmentCode;
            if ((employmentCode < 'a') || (employmentCode > 'd'))
                  cout << "--- invalid employment category code ---" << endl;
            //end if
      }while ((employmentCode < 'a') || (employmentCode > 'd'));

      //calculate vacationTime
      if (employmentCode == 'a')
            vacationTime = 1.0;
      else if (employmentCode == 'b')
            vacationTime = 1.0 + (years / 7.0);
      else if (employmentCode == 'c')
            vacationTime = years / 3.5;
      else if (employmentCode == 'd')
            vacationTime = 6.0;
      //end else if

      return vacationTime;
}//end calcVacationTime

void farewellMessage()
{
      cout << "So long..." << endl;
      cout << "Thanks for using the IC CIT2510 salesforce calculator!!!" << endl << endl;
}//end farewellMessage
ASKER CERTIFIED SOLUTION
Avatar of Rackboy
Rackboy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Don't forget to close your ofstream again once you are done;
fileOut.close();

Actually cout is a stream as fileOut is. The only difference is opening and closing.

Instead of

  ofstream fileOut;
  fileOut.open(filename);   // filename is a const char* like "output.txt"

you may do also

 ofstream fileOut("output.txt");

After that just replace cout by fileOut.

Regards, Alex
 
tinchos,

give the points to Rackboy as he was first and his answer is right ... beside of the missing semicolons.

Regards, Alex