Hi all. I am trying to read items from a text file whose name is input by the user and also output the receipt to a text file whose name is input by user.
I got stock for about 3 hours. Could you guys help? Thank You.
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
string formatDollar(double money);
int main ()
{
double price_ticket;
double base_cost;
double total_cost;
double tax_rate;
double tax;
string visiting_team;
// get four required data items from user
cout << "Enter the name of the visiting team: ";
getline(cin, visiting_team, '\n');
cout << "Enter the number of tickets: ";
cin >> num_tickets;
cout << "Enter the price per ticket: ";
cin >> price_ticket;
cout << "Enter the sales tax rate (e.g., 7.0): ";
// Generate receipt for the ticket purchase in a separate file.
ofstream myfile ("file.txt");
if (myfile.is_open())
{
myfile << " Nets vs. " << visiting_team << " at \"The Rock\"" << endl;
myfile << "-----------------------------------------" << endl;
myfile << "Number of tickets:" << setw(10) << num_tickets << endl;
myfile << "Price per ticket:" << setw(11) <<
formatDollar(price_ticket) << endl;
myfile << "Total ticket price:" << setw(9) <<
formatDollar(base_cost) << endl;
myfile << "Sales tax:" << setw(18) << formatDollar(tax) << endl;
myfile << "-----------------------------------------" << endl;
myfile << "Total amount due:" << setw(11) <<
formatDollar(total_cost) << endl;
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
{
// Convert the given dollar amount into
// a string with a leading "$"
ostringstream convert;
convert << setiosflags(ios::fixed);
convert << setprecision(2);
convert << "$" << money;
return convert.str();
} // end formatDollar