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));...

  • 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] << "...

  • 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...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • c++ programming : everything is done, except when you enter ("a" ) in "F" option ,...

    c++ programming : everything is done, except when you enter ("a" ) in "F" option , it does not work. here is the program. #include <iostream> #include <string> #include <bits/stdc++.h> #include <iomanip> #include <fstream> using namespace std; #define MAX 1000 class Inventory { private: long itemId; string itemName; int numberOfItems; double buyingPrice; double sellingPrice; double storageFees; public: void setItemId(long id) { itemId = id; } long getItemId() { return itemId; } void setItemName(string name) { itemName = name; } string...

  • Please program in C++ and document the code as you go so I can understand what...

    Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...

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
ADVERTISEMENT