65 721
Assignments Done
99,2%
Successfully Done
In October 2018

# Answer to Question #77991 in C++ for Crystal

Question #77991
At a school’s bazaar, activities were divided into ‘stalls’. At the close of the bazaar,
the manager of each stall submitted information to the Principal consisting of the
name of the stall, the income earned and its expenses. Some sample data were:

Games 2300.00 1000.00
Sweets 900.00 1000.00

Using a structure to hold a stall’s data, write a program to read the data and print a
report consisting of stall name and net income (income - expenses), in order of
increasing net income using selection sort. Assume that a line containing xxxxxx only ends the data.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
/*
Games 2300.00 1000.00
Sweets 900.00 1000.00
*/

struct STALL
{
string name;
double earned;
double expensed;
};

int main(int argc, char* argv[])
{
const char* filename = "input.txt";

vector<STALL> stalls;
ifstream infile(filename);
if (infile)
{
string line;
while(getline(infile, line))
{
if (line!="xxxxxx")
{
stringstream ss(line);
STALL stall;
if (ss >> stall.name >> stall.earned >> stall.expensed)
{
stalls.push_back(stall);
}
}
}
// Sort by net income
for (int i=0; i < (int)stalls.size(); i++)
{
int pos_min = i;
for (int j = i + 1; j < (int)stalls.size(); j++)
{
if ((stalls[j].earned - stalls[j].expensed) < (stalls[pos_min].earned - stalls[pos_min].expensed))
pos_min = j;
}

if (pos_min != i)
{
STALL temp;
temp = stalls[i];
stalls[i] = stalls[pos_min];
stalls[pos_min] = temp;
}

cout << stalls[i].name << " ";
cout << (stalls[i].earned - stalls[i].expensed) << endl;
}
}
else
cout << "Cannot open input file" << endl;

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!