I missed a day of programming class and didn't get to learn properly how to input and output file with fstream. Well I looked at a website and I think I have the gist of it, I just don't know where the information is stored.
Here's a code I'm writting for class work. The syntax is write, but I guess somethings wrong with the semantics. What I'm doing is finding the Net Pay after tax deductions, and outputting the results in a seperate .txt file, as well as displaying the amount each tax has deducted from the gross amount. I'm also asking the user to input the gross pay and their name. Here's the code, please help if you can, it's due tomorow and my version of Visual C++ doesn't show the errors properly:
// This program will deduct gross pay, format the data and place it in a file
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
const float FED=.15;
const float STATE=.035;
const float SOCIAL=.0575;
const float MED=.0275;
const float PEN=.05;
const int HEALTH=75;
int main()
{
double gross, federal, state, social, medicare, pension, health, net;//Declaring variables
string name;
ofstream netpayOut;
netpayOut.open("netpay.txt");
//I'm having the compiler output results in netpay.txt
cout<<"Enter your name:";
//Beginning by having user input name and gross pay
getline(cin, name);
cout<<"Now enter your gross pay:";
cin>>gross>>endl;
netpayOut<<name"\n";
netpayOut<<fixed<<showpiont<<setprecision(2);
//setting manipulators and outputting gross amount
netpayOut<<"Gross Amount:";
netpayOut<<setw(12)<<"$"<<gross<<endl;
netpayOut<<"Federal Tax:";
//outputting federal tax
federal=FED*gross;
//storing the amount removed from tax in federal
net=gross-federal; //finding the netpay by subtracting gross and federal
gross-=net
//finding the left over gross pay by subtracting from net
netpayOut<<setw(13)<<"$"<<federal<<endl;
netpayOut<<"State Tax:";
//outputting state tax
state=STATE*gross;
net=gross-state;
gross-=net;
netpayOut<<setw(15)<<"$"<<state<<endl;
netpayOut<<"Social Security Tax:";
//outputting social security tax
social=SOCIAL*gross;
net=gross-social;
gross-=net;
netpayOut<<setw(5)<<"$"<<social<<endl;
netpayOut<<"Medicare/Medicaid Tax:";
//outputting medicare tax
medicare=MED*gross;
net=gross-medicare;
gross-=net;
netpayOut<<setw(3)<<"$"<<medicare<<endl;
netpayOut<<"Pension Plan:";
//outputting pension plan tax
pension=PEN*gross;
net=gross-pension;
gross-=net;
netpayOut<<setw(12)<<"$"<<pension;
netpayOut<<"Health Insurance:";
//outputting health insurance tax
health=gross-HEALTH;
net=gross-health;
netpayOut<<setw(8)<<"$"<<health<<endl;
netpayOut<<"Net Pay:";
//outputting the netpay
netpayOut<<setw(17)<<"$"<<net<<endl;
cout<<"Your netpay will be found in the file 'netpay.txt'."<<endl;
netpayOut.close();
//disasociating the file (closing the file)
cin.get(); cin.get(); //freezing screen for user to input information
return 0;
}
Here's a code I'm writting for class work. The syntax is write, but I guess somethings wrong with the semantics. What I'm doing is finding the Net Pay after tax deductions, and outputting the results in a seperate .txt file, as well as displaying the amount each tax has deducted from the gross amount. I'm also asking the user to input the gross pay and their name. Here's the code, please help if you can, it's due tomorow and my version of Visual C++ doesn't show the errors properly:
// This program will deduct gross pay, format the data and place it in a file
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
const float FED=.15;
const float STATE=.035;
const float SOCIAL=.0575;
const float MED=.0275;
const float PEN=.05;
const int HEALTH=75;
int main()
{
double gross, federal, state, social, medicare, pension, health, net;//Declaring variables
string name;
ofstream netpayOut;
netpayOut.open("netpay.txt");
//I'm having the compiler output results in netpay.txt
cout<<"Enter your name:";
//Beginning by having user input name and gross pay
getline(cin, name);
cout<<"Now enter your gross pay:";
cin>>gross>>endl;
netpayOut<<name"\n";
netpayOut<<fixed<<showpiont<<setprecision(2);
//setting manipulators and outputting gross amount
netpayOut<<"Gross Amount:";
netpayOut<<setw(12)<<"$"<<gross<<endl;
netpayOut<<"Federal Tax:";
//outputting federal tax
federal=FED*gross;
//storing the amount removed from tax in federal
net=gross-federal; //finding the netpay by subtracting gross and federal
gross-=net
//finding the left over gross pay by subtracting from net
netpayOut<<setw(13)<<"$"<<federal<<endl;
netpayOut<<"State Tax:";
//outputting state tax
state=STATE*gross;
net=gross-state;
gross-=net;
netpayOut<<setw(15)<<"$"<<state<<endl;
netpayOut<<"Social Security Tax:";
//outputting social security tax
social=SOCIAL*gross;
net=gross-social;
gross-=net;
netpayOut<<setw(5)<<"$"<<social<<endl;
netpayOut<<"Medicare/Medicaid Tax:";
//outputting medicare tax
medicare=MED*gross;
net=gross-medicare;
gross-=net;
netpayOut<<setw(3)<<"$"<<medicare<<endl;
netpayOut<<"Pension Plan:";
//outputting pension plan tax
pension=PEN*gross;
net=gross-pension;
gross-=net;
netpayOut<<setw(12)<<"$"<<pension;
netpayOut<<"Health Insurance:";
//outputting health insurance tax
health=gross-HEALTH;
net=gross-health;
netpayOut<<setw(8)<<"$"<<health<<endl;
netpayOut<<"Net Pay:";
//outputting the netpay
netpayOut<<setw(17)<<"$"<<net<<endl;
cout<<"Your netpay will be found in the file 'netpay.txt'."<<endl;
netpayOut.close();
//disasociating the file (closing the file)
cin.get(); cin.get(); //freezing screen for user to input information
return 0;
}
Last edited: