Question

Fix the following program so that input is from a file “c:\comp\asg1\stalls.txt” and output is from...

Fix the following program so that input is from a file “c:\comp\asg1\stalls.txt” and output is from a file “c:\comp\asg1\stalls_output.txt”

#include <iostream>

#include <fstream>

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;
f.open("stalls.txt");

ofstream of;
of.open("output.txt");

while (loop) {
f >> tmp.name;

if (tmp.name == "XXXXXX" || 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"
<< "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 << "Stall with most profit is:" << endl;
most_profit = Stalls[n - 1].net;

for (int i = n - 1; i >= 0 && Stalls[i].net == most_profit; i--)
of << Stalls[i].name << endl;
  
of << "Stalls with profits: "<<endl;
for (int i = 0; i < n; i++)
{
if ((Stalls[i].net) > 0)
of << Stalls[i].name << endl;
}
of << endl;

of << "Order:"<<endl;
  
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++){
if (Stalls[i].net < 0)
{
for (int j = i + 1; j < n; ++j){
Stalls[j - 1] = Stalls[j];
               }
               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;
}
}
}
for (int i = 0; i < n; i++)
of << Stalls[i].name << endl;

of << endl;
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include <iostream>

#include <fstream>

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;

    //reading input from c:\comp\asg1\stalls.txt

    //note that, all \ characters have been replaced with \\

    //because \ alone is an escape character (used in \n, \t etc),

                // whereas \\ represents \ character

    f.open("c:\\comp\\asg1\\stalls.txt");

               

                //storing output in c:\comp\asg1\stalls_output.txt, same rules applicable

    ofstream of;

    of.open("c:\\comp\\asg1\\stalls_output.txt");

               

                //before entering loop, I suggest that you check if the file is opened correctly

                //this can be done by calling if(!f){ file not opened. do something or quit }else { file

                //opened }

    while (loop) {

        f >> tmp.name;

        if (tmp.name == "XXXXXX" || 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"

       << "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 << "Stall with most profit is:" << endl;

    most_profit = Stalls[n - 1].net;

    for (int i = n - 1; i >= 0 && Stalls[i].net == most_profit; i--)

        of << Stalls[i].name << endl;

    of << "Stalls with profits: " << endl;

    for (int i = 0; i < n; i++) {

        if ((Stalls[i].net) > 0)

            of << Stalls[i].name << endl;

    }

    of << endl;

    of << "Order:" << endl;

    for (int k = 0; k < n; k++)

        for (int i = 0; i < n; i++) {

            if (Stalls[i].net < 0) {

                for (int j = i + 1; j < n; ++j) {

                    Stalls[j - 1] = Stalls[j];

                }

                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;

            }

        }

    }

    for (int i = 0; i < n; i++)

        of << Stalls[i].name << endl;

    of << endl;

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
Fix the following program so that input is from a file “c:\comp\asg1\stalls.txt” and output is from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Replace "#include <algorithm>" and "sort(stalls, stalls + count, comparator2);" with simple and ba...

    Replace "#include <algorithm>" and "sort(stalls, stalls + count, comparator2);" with simple and basic C++ #include <iostream> #include <string> #include <fstream> #include <algorithm> using namespace std; #define MAX 100 struct Stall { string stallName; float income; float expense; }; // to sort in decreasung net income bool comparator(Stall x, Stall y) { return (x.income - x.expense) > (y.income - y.expense); } // to sort in increasing profit bool comparator2(Stall x, Stall y) { return ((x.income - x.expense) < (y.income - y.expense));...

  • This program is in C++ which reserves flight seats. It uses a file imported to get...

    This program is in C++ which reserves flight seats. It uses a file imported to get the seat chart called "chartIn.txt" which looks like this 1 A B C D E F 2 A B C D E F It continues until row 10. Sorry unable to provide the chartIn.txt file. I am having issues with function displaySeats, it displays then but when it comes to 10 the row looks like this 1 0 A B C D E ,...

  • My code doesn't output correctly using a .txt file. How do I clean this up so...

    My code doesn't output correctly using a .txt file. How do I clean this up so it posts correctly? ----------------------------------------------- CODE ---------------------------------------------- #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <fstream> using namespace std; struct Customer {    int accountNumber;    string customerFullName;    string customerEmail;    double accountBalance; }; void sortDesc(Customer* customerArray, int size); void print(Customer customerArray[], int size); void print(Customer customerArray[], int size) {    cout << fixed << setprecision(2);    for (int i = 0; i...

  • can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...

    can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block {    std::string word;    int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) {    string filename="input.txt";    //declare array of struct word_block    word_block arr[SIZE];    int count = 0;    if (argc < 2)    {        cout << "Usage: " << argv[0] << "...

  • I am trying to modify this program so that it stores the employee's name in a...

    I am trying to modify this program so that it stores the employee's name in a c-string and can handle up to 100 employees (so it would mostly be a partially filled array), but I cannot get it to work. It works with two files that it is suppose to read input from. Then it prints output to an output file. Employee Info File (first file read, M/F should be ignored): 5    Christine Kim 30.00   2       1    F 15    Ray...

  • CODE ERROR Please help to fix the error. I don't know why I can not output...

    CODE ERROR Please help to fix the error. I don't know why I can not output to file. when I open the output file there is an error. here is the input data: Princeton University NJ Princeton 41820    8014   0.0740   0.98   0.97 Harvard University MA Cambridge 43838    19882   0.0580   0.97   0.97 Yale University CT New Haven 45800    12109   0.0690   0.99   0.98 Columbia University NY New York           51008    23606   0.0690   0.99   0.96 Stanford University CA...

  • Use the file processing example program shown at the bottom. Modify the program to enter the...

    Use the file processing example program shown at the bottom. Modify the program to enter the grades, and count number of grades. Also get total and average grade. Use the following data for grades: 79, 99, 85, 97, 88, 95, 100, 87, 94 EXAMPLE OUTPUT Total: 9 grades Total grade sum: 824 Average grade: 92 Letter grade: A / Using Break, and Continue #include "stdafx.h" #include using namespace std; int main() { int i = 0; for (int x =...

  • Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1,...

    Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1, int j=2,int k=3, double product =1.0) { i+=2; j*=2; k/=2; product=i*j*k; } void Magic(int& i, int& j, double& product) { i+=2; j=j*2+2; product=i*j; } void Magic(int* i,int* j) { double product; *i+=2; *j=*j*2+2; product=*i * *j; } int main() { double product; int i=0,j=0,k=0; product=i*j*k;    Magic(); cout<<"i, j, k and product in main () after 1st round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;    Magic(2,4); cout<<"i, j, k and...

  • Can some one fix my code so that the output result same as below? BAR PLOT...

    Can some one fix my code so that the output result same as below? BAR PLOT OF CYLINDER PRESSURE -VS- TIME pressure is on horizontal axis - units are psi time is on vertical axis - units are msec    0.0         20.0        40.0          60.0        80.0       100.0      120.0       +---------+---------+---------+---------+---------+---------+ 0.0|************************* 1.5|********************************* 3.0|***************************************** 4.4|**************************************************      05.9|********************************************* 7.4|******************************** 8.9|*********************** 10.4|***************** 11.9|************** 13.3|************* 14.8|************ 16.3|********* 17.8|******* 19.3|****** 20.7|****** 22.2|******* 23.7|******* .......... ===================== her is my code //#include"stdafx.h" #include #include #include #include #include #include using...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT