[C++ HELP] I am having trouble with my program. I am trying to output data to fi
ID: 3830634 • Letter: #
Question
[C++ HELP] I am having trouble with my program. I am trying to output data to file "weekly_call_info.txt" from my input file "call_data.txt" but when I run and compile the program it only outputs the last line of data from the input file. (The input file has multiple lines of data that get run through the program then into the output.)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//class declaration
class call_record{
public:
string cell_number;
int relays;
int call_length;
double net_cost;
double tax_rate;
double call_tax;
double total_cost;
};
//Prototypes
void input(ifstream & in, call_record &);
void process(call_record &);
void output(call_record &);
//Input function
void input(ifstream & in, call_record & customer_record){
in >> customer_record.cell_number >> customer_record.relays >> customer_record.call_length;
}
//Process function
void process(call_record & customer_record){
if(customer_record.relays >= 0 && customer_record.relays <=5){
customer_record.tax_rate = 0.01;
} else if(customer_record.relays >= 6 && customer_record.relays <=11){
customer_record.tax_rate = 0.03;
} else if(customer_record.relays >= 12 && customer_record.relays <=20){
customer_record.tax_rate = 0.05;
} else if(customer_record.relays >= 21 && customer_record.relays <=50){
customer_record.tax_rate = 0.12;
}
//net cost of call
customer_record.net_cost = (customer_record.relays/50 * 0.40 * customer_record.call_length);
//cost of tax on call
customer_record.call_tax = customer_record.net_cost * customer_record.tax_rate;
//Total cost of call
customer_record.total_cost = customer_record.net_cost + customer_record.call_tax;
}
void output(call_record & customer_record){
ofstream out("weekly_call_info.txt");
out.setf(ios::showpoint);
out.precision(2);
out.setf(ios::fixed);
out << customer_record.cell_number << " " << customer_record.relays << " " << customer_record.call_length << " " << customer_record.net_cost << " " << customer_record.tax_rate << " " << customer_record.call_tax << " " << customer_record.total_cost << endl;
out.close();
}
int main(){
call_record customer_record;
ifstream in("call_data.txt");
ofstream out("weekly_call_info.txt");
if(in.fail()){
cout << "Your input file does not exist or did not open properly." << endl;
} else {
while (!in.fail()){
input(in, customer_record);
process(customer_record);
output(customer_record);
}
}
in.close();
return 0;
}
Explanation / Answer
Note :
You are getting the only one line in the output file because every time when the program call output() function it is creating a new file with the same name and overwriting the existing the file.So to solve this problem we have to open the output file in append mode.So that it will write the output data in the same file.
ofstream out("D:\weekly_call_info.txt", std::ios::app);
____________________
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//class declaration
class call_record{
public:
string cell_number;
int relays;
int call_length;
double net_cost;
double tax_rate;
double call_tax;
double total_cost;
};
//Prototypes
void input(ifstream & in, call_record &);
void process(call_record &);
void output(call_record &);
//Input function
void input(ifstream & in, call_record & customer_record){
in >> customer_record.cell_number >> customer_record.relays >> customer_record.call_length;
}
//Process function
void process(call_record & customer_record){
if(customer_record.relays >= 0 && customer_record.relays <=5){
customer_record.tax_rate = 0.01;
} else if(customer_record.relays >= 6 && customer_record.relays <=11){
customer_record.tax_rate = 0.03;
} else if(customer_record.relays >= 12 && customer_record.relays <=20){
customer_record.tax_rate = 0.05;
} else if(customer_record.relays >= 21 && customer_record.relays <=50){
customer_record.tax_rate = 0.12;
}
//net cost of call
customer_record.net_cost = (customer_record.relays/50 * 0.40 * customer_record.call_length);
//cost of tax on call
customer_record.call_tax = customer_record.net_cost * customer_record.tax_rate;
//Total cost of call
customer_record.total_cost = customer_record.net_cost + customer_record.call_tax;
}
void output(call_record & customer_record){
ofstream out("D:\weekly_call_info.txt", std::ios::app);
out.setf(ios::showpoint);
out.precision(2);
out.setf(ios::fixed);
out << customer_record.cell_number << " " << customer_record.relays << " " << customer_record.call_length << " " << customer_record.net_cost << " " << customer_record.tax_rate << " " << customer_record.call_tax << " " << customer_record.total_cost << endl;
out.close();
}
int main(){
call_record customer_record;
ifstream in("D:\call_data.txt");
//ofstream out("D:\weekly_call_info.txt");
if(in.fail()){
cout << "Your input file does not exist or did not open properly." << endl;
} else {
while (!in.fail()){
input(in, customer_record);
process(customer_record);
output(customer_record);
}
}
in.close();
//out.close();
return 0;
}
_________________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.