Question

In: Computer Science

Add to the code below so that when complied, it also shows the stalls which made...

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;

}

Solutions

Expert Solution

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;

}


Related Solutions

How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that the following test cases work. Note: I have removed all the unnecessary inherited List implementations. I have them to: throw new UnsupportedException(); For compilation, you could also add //TODO. Test (Main) List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size...
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
The code below is giving an arithmetic overflow. Correct the below given the below code, so...
The code below is giving an arithmetic overflow. Correct the below given the below code, so that the value of s0 is printed before calling the function fun1, inside the function Fun1 and after returning from Fun1 in main. Upload the corrected .asm or .s file in the drop box. .text main: addi $s0,$zero,2 jal Disp jal Fun1 jal Disp j Exit Fun1: addi $sp,$sp,-4 sw $s0,0($sp) addi $s0,$s0,15 jal Disp lw $s0,0($sp) addi $sp,$sp,4 jr $ra Disp: li $v0,1...
QUESTION: Add code so that if no parameter values are specified for method “main”, then the...
QUESTION: Add code so that if no parameter values are specified for method “main”, then the error message “file name expected” is printed out instead of the "Opening file " message. Hint: If no parameter values are specified for method “main”, then the length of the array “args” will be zero. If that error message “file name expected” is printed out, the program should stop. You can make any Java program stop using this line … System.exit(0); skeleton code: /**...
HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT...
HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT BY THE USER AS SPECIFIED IN THEH FIRST PART OF THE QUESTION? Ask the user to enter a number and display the number, followed by ***, followed by the number squared, followed by &&&, and followed by the number cubed. Allow the user to enter as many numbers as he/she wishes. So, use a reasonable sentinel value to end the loop (for example, -999)....
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
Below is my code in C#, When I run it, the output shows System.32[], Can you...
Below is my code in C#, When I run it, the output shows System.32[], Can you please check and let me know what is the problem in the code. class Program { static void Main(string[] args) { int number=12; Console.WriteLine(FizzArray(number)); } public static int[] FizzArray(int number) { int[] array = new int[number]; for (int i = 1; i < number; i++) array[i] = i; return array; }
A. Add code (see below for details) to the methods "set" and "get" in the following...
A. Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index]...
A. Add code (see below for details) to the methods "set" and "get" in the following...
A. Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT