inFile txt saving

Hi guys, I have to write a program that asks for the month year and total income for a month and it will find the sales rate with the equation s=7/1.06 i dont know how to save the input into a txt file. i dont know how to use the inFile command that well. The answre should turn out like this
bash-2.04$ a.out
Please enter the month for this report: February
Please enter the year for this report: 2005
Please enter the total income for this month: 15000.25
Sales Tax Report Saved to file: SalesTaxData.txt

bash-2.04$ cat SalesTaxData.txt

Month: February 2005
-----------------------
Total Collected: $ 15000.25
Sales: $ 14151.18
County Sales Tax: $ 283.02
State Sales Tax: $ 566.05
Total Sales Tax: $ 849.07
-----------------------


so far my code is like this but it does not work..

#include <iostream>
#include <fstream>
using namespace std;

int main ()

{
ifstream inFile;
ofstream outFile;
double sales , total , year;
char month[10];

inFile.open("sales.txt");

cout << "Please enter the month for this report:";
cin>> month;
cout << "Please enter the year for this report:";
cin >> year;
cout << "Please enter the total income for this month:";
cin >> total;
cin >> total;
sales = total / 1.06;
cin >> sales;
cout << "The sales amount is "<< sales << endl;

>> month; inFile << month;
cout << "Month: ";
inFile >> total;
cout << "Total Collected: ";
inFile >> sales;
cout << "Sales: ";
inFile.close();

return 0;
ifstream shoud have
ifstream variable_name ("Name of file.extention all in quotes", open_mode);
ofstream should have
ofstream variable_name ("Name of file.extention all in quotes", open_mode);

This I find easier for myself to use\read then the .open stuff.

>> month; inFile << month; Whaaa? What are we trying to do?

The file input should all be done in a loop. So tear down and redo let's start freesh.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12

#include <iostream>
#include <fstream>
using namespace std;

int main ()

{
  ifstream inFile ("sales.txt", ios_base::in);
  ofstream outFile ("total.txt", ios_base::trunc);
//...
//... 


To output data to your total.txt file use
outFile << data;

To Input data from your sales.txt file use
1
2
3
4
5
while(inFile)
{inFile >> variable;
//...
//...
}


I remeber I had a lot of trouble with this when I was younger.
Topic archived. No new replies allowed.