This stated below is my c++ program. My questions are how could get the console
ID: 3547988 • Letter: T
Question
This stated below is my c++ program. My questions are how could get the console to display multiple outputs and then send those outputs to one text file. As of right now as soon as the user enters his inputs the output would display and then clear after the user ends entering his second round of repeated inputs.
So:
-Want console to display multiple output
-Send all those outputs to one text file.
******************************************************************************************
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <cstdlib>
#include <utility>
using namespace std;
//************************ main ********************************
int main()
{
int class_id;
double employeeid,
hrs_worked,
ot_hours,
total_pay,
ot_pay,
reg_rate,
reg_pay;
char name[30],
cContinue;
//input section
do {
cout << "The Employee's Name: ";
cin >> ws;
cin.getline(name,(sizeof(name)-1));
cout << "The Employee's Id. Number: ";
cin >> employeeid;
cout << "The Employee's Job Classification Number: ";
cin >> class_id;
switch (class_id)
{
case 1: reg_rate = 5.00;
break;
case 2: reg_rate = 6.00;
break;
case 3: reg_rate = 9.00;
break;
case 4: reg_rate = 12.00;
break;
default: reg_rate = 5.00;
break;
}
cout << "The Number of Hours Worked. ";
cin >> hrs_worked;
//process section
if (hrs_worked == 40)
{ reg_pay = hrs_worked * reg_rate;
ot_pay = 0;}
else if (hrs_worked >= 40)
{reg_pay = hrs_worked * reg_rate;
ot_pay = (hrs_worked - 40) * 1.5 * reg_rate;}
else if (hrs_worked <= 40)
{reg_pay = hrs_worked * reg_rate;;
ot_pay = 0;}
ot_hours = hrs_worked - 40;
if (hrs_worked <= 40)
{ot_hours = 0;}
total_pay = ot_pay + reg_pay;
//output section
system("cls");
cout << setw(48) << "WorkHard Corporation" << endl;
cout << setw(48) << "====================" << endl;
cout << setw(10) << "Employee Name: " << setw(5) << name << setw(20) << "ID. Number: " << setw(5)
<< employeeid << endl;
cout << setw(10) << "Job Classification: " << setw(5) << class_id << setw(20) << "Hourly Rate: " << setw(5)
<< reg_rate << endl;
cout << setw(10) << "Total Hours Worked: " << setw(5) << hrs_worked << setw(20) << "Overtime Hours: " <<
setw(5) << ot_hours << endl;
cout << setw(10) << "Regular Pay: " << setw(12) << reg_pay << setw(20) << "Overtime Pay: " << setw(5) <<
ot_pay << endl;
cout << setw(48) << "Total Earnings ......... " << setw(5) << total_pay << endl;
cout << "***********************************************************" << endl;
cout << "Continue(y/n)?" << std::endl;
cin >> cContinue;
} while (cContinue == 'y' || 'Y');
system("pause");
}
Explanation / Answer
Your program currently already accepts multiple inputs, except its doing it infinitely and never ends because of your while statement.
while (cContinue == 'y' || 'Y'); is always true since the ascii numbering for Y is not 0. It should be while (cContinue == 'y' || cContinue == 'Y'); it needs to be checked twice.
To achieve the printing, you should put all that information into a file instead of showing it after input. close that file after everything is read in, then open the file you just made and print everything out.
Another way of doing it is making arrays but there is no indication of how many sets of data you want to hold. So just printing it all into a file is probably better.
It'll look something like this.
ofstream outFile;
outFile.open("data.txt");
//put in the title bar
outFile << setw(48) << "WorkHard Corporation" << endl;
outFile << setw(48) << "====================" << endl;
//read in 1 set of your data as you've already done then start writing it to file
outFile << setw(10) << "Employee Name: " << setw(5) << name << setw(20) << "ID. Number: " << setw(5)
<< employeeid << endl;
outFile<< setw(10) << "Job Classification: " << setw(5) << class_id << setw(20) << "Hourly Rate: " << setw(5)
<< reg_rate << endl;
outFile << setw(10) << "Total Hours Worked: " << setw(5) << hrs_worked << setw(20) << "Overtime Hours: " <<
setw(5) << ot_hours << endl;
outFile << setw(10) << "Regular Pay: " << setw(12) << reg_pay << setw(20) << "Overtime Pay: " << setw(5) <<
ot_pay << endl;
outFile<< setw(48) << "Total Earnings ......... " << setw(5) << total_pay << endl;
outFile<< "***********************************************************" << endl;
cout << "Continue(y/n)?" << endl;
cin >> cContinue;
//after all data has been collected close the file
outFile.close();
//now create a file to read from
ifstream inFile;
inFile.open("data.txt");
//then just print whatever is in the file until end of file
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.