Solved

C++ Outputting to a file

Posted on 2004-04-17
7
200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

713 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