Question

In: Computer Science

Write a program that does the following in C++ 1 ) Write the following store data...

Write a program that does the following in C++

1 ) Write the following store data to a file (should be in main)

DC Tourism

Expenses 100.20

Revenue 200.50

Maryland Tourism

Expenses 150.33

Revenue 210.33

Virginia Tourism

Expenses 140.00

Revenue 230.00

2 ) Print the following heading: (should be in heading function)

Store name | Profit

[Note: use setw to make sure all your columns line up properly]

3 ) Read the store data for one store (should be in main)

4 ) Calculate the profit for the store (should be in getProfit function)

5 ) Print the store information in the following format (should be in printMessage function)

<Store name> | <Profit>

[Note: use setw to make sure all your columns line up properly]

6 ) Repeat steps 3-5 in an end of file loop [should be in main]

Your answer should look like this:

Store name Profit                                                                            

DC Tourism 100.30                                                                            

Maryland Tourism 60.00                                                                            

Virginia Tourism               90.00    

Functions you need

Function name | Parameters           | Return Type

heading | N/A                       | void

getProfit      | expenses,revenue | float

PrintMessage    | storeName1, profit | void

Solutions

Expert Solution

#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include<vector>
#include<sstream>
#include<string>
using namespace std;

class Record
{
private:
   string name;
   double expenses, revenue;
public:
   Record()
   {
       this->name = "";
       this->expenses = this->revenue = 0.0;
   }
   Record(string name, double expenses, double revenue)
   {
       this->name = name;
       this->expenses = expenses;
       this->revenue = revenue;
   }
   string getName() { return name; }
   double getExpenses() { return expenses; }
   double getRevenue() { return revenue; }
   double getProfit() { return(revenue - expenses); }
};

// FUNCTION PROTOTYPES
void heading();
void PrintMessage(string storeName, double profit);

int main()
{
   string filename = "data.txt";
   string line;
   vector<Record> records;
   ifstream inFile(filename.c_str());
   if (!inFile.is_open())
   {
       cout << "Error in opening file " << filename << "! Exiting..\n";
       exit(EXIT_FAILURE);
   }
   // read input file and populate array of records
   while (getline(inFile, line))
   {
       string name = line;
       // expenses
       getline(inFile, line);
       stringstream ss1(line);
       vector<string> tokens1;
       string token1;
       double expense = 0.0;
       while (getline(ss1, token1, ' '))
       {
           tokens1.push_back(token1);
       }
       string title = tokens1[0];
       expense = stod(tokens1[1]);

       // revenue
       getline(inFile, line);
       stringstream ss2(line);
       vector<string> tokens2;
       string token2;
       double revenue = 0.0;
       while (getline(ss2, token2, ' '))
       {
           tokens2.push_back(token2);
       }
       title = tokens2[0];
       revenue = stod(tokens2[1]);
       records.push_back(Record(name, expense, revenue));
   }
   inFile.close();

   // display the heading
   heading();
   // print the rows
   for (Record r : records)
       PrintMessage(r.getName(), r.getProfit());
   cout << endl;
   return 0;
}
void heading()
{
   cout << setw(20) << left << "STORENAME" << setw(1) << left << "PROFIT" << endl;
}
void PrintMessage(string storeName, double profit)
{
   cout << setw(20) << left << storeName << setw(1) << left << "$" << setprecision(2) << fixed << profit << endl;
}

******************************************************** SCREENSHOT ********************************************************

INPUT FILE (data.txt) - This file needs to be created before running the code and this file should be created within the same project directory.

CODE SCREENSHOTS :

CONSOLE OUTPUT :


Related Solutions

Write a C program that will read different data types from the following file and store...
Write a C program that will read different data types from the following file and store it in the array of structures. Given file: (This file have more than 1000 lines of similar data): time latitude longitude depth mag magType nst gap dmin 2020-10-19T23:28:33.400Z 61.342 -147.3997 12.3 1.6 ml 12 84 0.00021 2020-10-19T23:26:49.460Z 38.838501 -122.82684 1.54 0.57 md 11 81 0.006757 2020-10-19T23:17:28.720Z 35.0501667 -117.6545 0.29 1.51 ml 17 77 0.1205 2020-10-19T22:47:44.770Z 38.187 -117.7385 10.8 1.5 ml 15 100.22 0.049 2020-10-19T22:42:26.224Z...
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file...
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file Electricity.txt and read each column into an array (8 arrays total). 2.   Also create 2 arrays for the following: Total Fossil Fuel Energy (sum of all fossil fuels) Total Renewable Energy (sum of all renewable sources) Electricity.txt: Net generation United States all sectors monthly https://www.eia.gov/electricity/data/browser/ Source: U.S. Energy Information Administration All values in thousands of megawatthours Year   all fuels   coal       natural gas   nuclear  ...
Write a program in C that does the following: 1. Declares an array called numbers_ary of...
Write a program in C that does the following: 1. Declares an array called numbers_ary of 6 integer numbers. 2. Declares an array called numbers_ary_sq of 6 integer numbers. 3. Reads and sets the values of numbers_ary from the keyboard using a loop. 4. Sets the values of numbers_ary_sq to the square of the values in numbers_ary using a loop. 5. Displays the values of numbers_ary and the values of numbers_ary_sq beside each other using a loop. Example Output Assume...
Write a program that uses a structure to store the following weather data for a particular...
Write a program that uses a structure to store the following weather data for a particular month: Total Rainfall High Temperature Low Temperature Average Temperature. The program should have an array of 12 structures to hold weather data for an entire year. When the program runs, it should ask the user to enter data for each month. (The average temperature should be calculated.) Once the data are entered for all the months, the program should calculate and display the average...
Write a C program that does the following In this part, you will write more complicated...
Write a C program that does the following In this part, you will write more complicated functions. They will require parameters and return values. The purpose is to give you experience with these components, and to show you how functions can be used to break your code down into smaller parts. You will also get some more experience with iterating through arrays.Open repl project Lab: User-Defined Functions 2. Write a program that does the following: 1.(20 pts.) Allows the user...
Write a program that uses a structure to store the following data: (Remember to use proper...
Write a program that uses a structure to store the following data: (Remember to use proper formatting and code documentation) Member Name Description name student name idnum Student ID number Scores [NUM_TESTS] an array of test scores Average Average test score Grade Course grade Declare a global const directly above the struct declaration const int NUM_TESTS = 4; //a global constant The program should ask the user how many students there are and should then dynamically allocate an array of...
Write a program that uses a structure to store the following data about a customer account:...
Write a program that uses a structure to store the following data about a customer account: Name Address City, State and Zip Telephone number Account balance Date of last payment The program should use an array of at least 5 structures. It should let the user enter data into the array, change the contents of any element and display all the data stored in the array. The program should have a menu-driven interface and use functions as appropriate. Input validation:...
write a c++ program using micro soft visual studio 2010 to write a program and store...
write a c++ program using micro soft visual studio 2010 to write a program and store 36 in variable x and 8 in variable y. add them and store the result in the variable sum. then display the sum on screen with descriptive text. calculate the square root of integer 36 in x. store the result in a variable. calculate the cube root of integer 8 in y. store result in a variable. display the results of square root and...
Write a java program that does the following: a) The user will enter data such as...
Write a java program that does the following: a) The user will enter data such as client name and client balance. The user can stop inputting data by entering "stop". The program should store the user inputs as a data member of an array. The type of this array is a class named ClientData. Below the output should be displayed by the program. The parts in bold are inputs from the user and not hard coded in the program Client...
You are to write a C++ program which does the following: Reads in the size of...
You are to write a C++ program which does the following: Reads in the size of a list of characters. Reads in the list of characters. Prints the list of characters in the opposite order read in. Prints the list of characters in the order read in. Sorts the list. Prints the sorted list. You may assume there will be no more than 1000 characters in the list. (You should use a constant to make this limit easily changeable.) You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT