In: Computer Science
Add to the code below so that when complied, it also shows the stalls which made a profit in increasing order of profit (lowest profit to highest profit) in output.txt
#include <iostream>
#include <fstream>// for file streaming
using namespace std;
int main()
{
struct Stall
{
string name;
double income;
double expenses;
double net;
};
double tprofit_loss = 0, most_profit;
Stall tmp;
int n = 0;
Stall Stalls[100];
bool loop = true;
ifstream f; //this is a input file object
f.open("stalls.txt"); //open file with the f object
ofstream of; //this is a output file object
of.open("output.txt"); //open file "output.txt" with the of
object
while (loop)
{
f >> tmp.name; //read from the file
if (tmp.name == "xxxxxx")
{
loop = false;
continue;
}
f>> tmp.income; //read income from the file
f>> tmp.expenses; //read expenses from the file
tmp.net = tmp.income - tmp.expenses;
tprofit_loss += tmp.net;
if (n != 0)
if (Stalls[n-1].net >= tmp.net)
Stalls[n++] = tmp;
else
{
Stall tmp2 = Stalls[n-1];
Stalls[n-1] = tmp;
Stalls[n++] = tmp2;
}
else
{
Stalls[n++] = tmp;
}
}
of<< "Stall name\t" //write to the output file
<< "Net income" << endl;
for (int i = 0; i < n; i++)
of<< Stalls[i].name << "\t\t" << Stalls[i].net << endl;
of<< "Number of stalls in the bazar were: " << n << endl;
if (tprofit_loss < 0)
of << "Total loss of bazar is " << tprofit_loss << endl;
else
of << "Total profit of bazar is " << tprofit_loss << endl;
of << "Stalls with most profit are:" << endl;
most_profit = Stalls[0].net;
for (int i = 0; i < n && Stalls[i].net == most_profit; i++)
of << Stalls[i].name << ", ";
of << endl;
return 0;
}
If you have any doubts, please give me comment...
#include <iostream>
#include <fstream> // for file streaming
using namespace std;
struct Stall {
string name;
double income;
double expenses;
double net;
};
int main() {
double tprofit_loss = 0, most_profit;
Stall tmp;
int n = 0;
Stall Stalls[100];
bool loop = true;
ifstream f; // this is a input file object
f.open("stalls.txt"); // open file with the f object
ofstream of; // this is a output file object
of.open("output.txt"); // open file "output.txt" with the of object
while (loop) {
f >> tmp.name; // read from the file
if (tmp.name == "xxxxxx") {
loop = false;
continue;
}
f >> tmp.income; // read income from the file
f >> tmp.expenses; // read expenses from the file
tmp.net = tmp.income - tmp.expenses;
tprofit_loss += tmp.net;
Stalls[n] = tmp;
n++;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (Stalls[i].net > Stalls[j].net) {
Stall tmp = Stalls[i];
Stalls[i] = Stalls[j];
Stalls[j] = tmp;
}
}
}
of << "Stall name\t" // write to the output file
<< "Net income" << endl;
for (int i = 0; i < n; i++)
of << Stalls[i].name << "\t\t" << Stalls[i].net << endl;
of << "Number of stalls in the bazar were: " << n << endl;
if (tprofit_loss < 0)
of << "Total loss of bazar is " << tprofit_loss << endl;
else
of << "Total profit of bazar is " << tprofit_loss << endl;
of << "Stalls with most profit are:" << endl;
most_profit = Stalls[n-1].net;
for (int i = n-1; i >=0 && Stalls[i].net == most_profit; i--)
of << Stalls[i].name << ", ";
of << endl;
return 0;
}