Solved

C++ Outputting to a file

Posted on 2004-04-17
7
197 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:krleonia
  • 2
7 Comments
 

Accepted Solution

by:
Rackboy earned 50 total points
ID: 10851562
# include <fstream>

ofstream fileOut
fileOut.open(filename)
fileOut << "what you want to output to the file";
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10852775
Don't forget to close your ofstream again once you are done;
fileOut.close();

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10853376
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
 
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11080523
tinchos,

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

Regards, Alex
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question