Input File Question
Mar 24, 2008 at 5:53pm UTC
I'm trying to allow a user to enter a file name and if they do not have one default to one. However, I'm having an issue determining if it should be a do-while or if-else statement and the syntax. Below is my source code.
Thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
#include <iostream>
#include <stdlib.h>
#include <string>
#include <math.h>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
string customer_id;
char ticket_type;
string event_num;
string ticket_type_long;
float ticket_price;
int num_of_tickets_purchased;
float fee;
float total;
int num_records = 0;
ifstream inData;
ofstream outData;
float calc_total (int num_of_tickets_purchased, float ticket_price, float fee);
float calc_fee (int num_of_tickets_purchased);
string user_file;
bool good_user_file;
cout << fixed << showpoint;
cout << setprecision(2);
inData.open("c:\\tickets.txt" );
inData.open (user_file.c_str());
cout<<"Please enter the name of the file you wish to use including the extension." << endl;
cin >> user_file;
if (user_file = inData.open(user_file.c_str())
else
user_file = inData.open("c:\\tickets.txt"
//outData.open("c:\\ticketsales.txt");
while (inData.eof() == false )
{
num_records++;
inData >> customer_id >> ticket_type >> event_num >> ticket_price >> num_of_tickets_purchased;
if (ticket_type == 'F' )
ticket_type_long = "Football" ;
if (ticket_type == 'H' )
ticket_type_long = "Hockey" ;
if (ticket_type == 'B' )
ticket_type_long = "Basketball" ;
fee = calc_fee (num_of_tickets_purchased);
total = calc_total (num_of_tickets_purchased, ticket_price, fee);
cout <<setw(8)<<"Customer" <<setw(8)<<"Type" <<setw(20)<<"Event ID" <<setw(8)<<"Total" <<endl;
outData <<setw(8)<<"Customer" <<setw(8)<<"Type" <<setw(20)<<"Event ID" <<setw(8)<<"Total" <<endl;
cout <<setw(5)<<customer_id<<setw(14)<<ticket_type_long<<setw(12)<<event_num<<setw(6)<<"$" <<total<<endl;
outData << setw(5)<<customer_id<<setw(14)<<ticket_type_long<<setw(12)<<event_num<<setw(6)<<"$" <<total<<endl;
cout << endl;
}
cout << "Record count:" << num_records << endl;
inData.close();
outData.close();
system("pause" );
return 0;
}
float calc_fee (int num_of_tickets_purchased)//function to calculate fee
{
float fee;
if (num_of_tickets_purchased == 1)
{
fee = 0;
}
else if (num_of_tickets_purchased == 2)
{
fee = 3 * num_of_tickets_purchased;
}
else if ((num_of_tickets_purchased >= 3)&&(num_of_tickets_purchased <=6))
{
fee = 3.5 * num_of_tickets_purchased;
}
else if (num_of_tickets_purchased >= 7)
{
fee = 5.5 * num_of_tickets_purchased;
}
return fee;
}
float calc_total (int num_of_tickets_purchased, float ticket_price, float fee)//function to calculate total
{
float total;
if (num_of_tickets_purchased == 1)
{
total = ticket_price * num_of_tickets_purchased;
}
else if (num_of_tickets_purchased == 2)
{
total = fee + (ticket_price * num_of_tickets_purchased);
}
else if ((num_of_tickets_purchased >= 3)&&(num_of_tickets_purchased <=6))
{
total = fee + (ticket_price * num_of_tickets_purchased);
}
else if (num_of_tickets_purchased >= 7)
{
total = fee + (ticket_price * num_of_tickets_purchased);
}
return total;
}
Last edited on Mar 24, 2008 at 5:54pm UTC
Topic archived. No new replies allowed.