Solved

C++ Outputting to a file

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ array loading from DB inheriting previous data when empty 5 59
Find Visual Studio Tools 2 92
Add values of each row in an array 3 50
c++, dynamic object by json 1 25
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…
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 …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 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

21 Experts available now in Live!

Get 1:1 Help Now