Solved

C++ Outputting to a file

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now