-This program total ups how much money someone makes before taxes.
I just need it to read the values from a file and save them in a file
This assignment is due tonight and i'm not sure how to do it, any help would be great
I know I need to use <fstream> but I don't really know how.
How does the program know which part of the file is the hours worked and which part is the paid rate?
Here is my code:
#include <iostream>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::setprecision;
int main()
{
double hours = 40.0;
double rate = 10.0;
double gross = 0.0;
double overtime = 1.5;
cout << "Enter the ammount of hours worked: ";
cin >> hours;
cout << "Enter the ammount paid per hour: ";
cin >> rate;
if (hours<0 || rate<0)
cout << "ERROR pick a different number" << endl;
else
if(hours > 40){ //calculate gross pay
overtime = (hours-40) * rate * 1.5;
gross = 40 * rate + overtime;
}
else
{
gross = rate * hours;
}
cout << "Overtime: " << overtime << endl;
cout << "Gross pay: " << gross << endl;
system ("pause");
return 0;
}the correct program for the above description is-
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
ifstream inf("c:\\input.dat");
ofstream outf("c:\\out.dat");
if(!inf)
{
cout<<"File inf.dat couldnt be opened";
_getch();
exit(1);
}
if(!outf)
{
cout<<"File out.dat couldnt be opened";
_getch();
exit(1);
}
double hours ;
double rate ;
double gross ;
double overtime=0;
cout<<"\n\nReading values of hours and rate from input.dat....\n";
inf>> hours;
inf >> rate;
cout<<"\nValues are :\nhours : "<<hours<<"\trate : "<<rate;
if(hours > 40)
{ //calculate gross pay
overtime = (hours-40) * rate * 1.5;
gross = 40 * rate + overtime;
}
else
{
gross = rate * hours;
}
cout<<"\n\nvalues calculated : \n";
cout<<"overtime : "<<overtime<<"\tgross : "<<gross;
cout<<"\nwriting values of overtime and gross in out.dat....\n";
outf<<overtime<<endl<<gross;
return 0;
}
in order to run this program, first make a file "input.dat" in the c drive.
put values in this file like that-
30
10
or like-
50
5
first value indicates the number of hours and the second value indicates rate.
u need not create out.dat. it will be created automatically.
now run the program and see ur desired output.Daryl...you have posted this same question with the same code 3 times in the last 2 hours.
First time, Blackcompe gave you an answer that should have at least gotten you started. It should have solved at least half of the problem, and gotten you to think about the other half.
But you haven't updated your code to reflect his answer, or anything else you might have learned in the last couple hours. Are you working on this problem, or just posting questions and hoping someone else is bored this Friday night?
Take a stab at doing it yourself, post YOUR code if you get stuck, maybe someone will help. Most of us don't want to do homework on Friday nights ;-)
没有评论:
发表评论