Question

In: Computer Science

C++ Program -------------------- Project 5-1: Monthly Sales Create a program that reads the sales for 12...

C++ Program

--------------------

Project 5-1: Monthly Sales

Create a program that reads the sales for 12 months from a file and calculates the total yearly sales as well as the average monthly sales.

Console

Monthly Sales

COMMAND MENU

m - View monthly sales

y - View yearly summary

x - Exit program

Command: m

MONTHLY SALES

Jan        14317.41

Feb         3903.32

Mar         1073.01

Apr         3463.28

May         2429.52

Jun         4324.70

Jul         9762.31

Aug        25578.39

Sep         2437.95

Oct         6735.63

Nov          288.11

Dec         2497.49

Command: y

YEARLY SUMMARY

Yearly total:           76811.12

Monthly average:         6400.93

Command: x

Bye!

Specifications

  • Your instructor should provide a tab-delimited file named monthly_sales.txt that contains the month and sales data shown above.
  • Round the results of the monthly average to a maximum of 2 decimal digits and make sure that a minimum of 2 decimal digits are displayed on the console.
  • Right-align the columns that display the monthly sales, the yearly total, and the monthly average.

Note for Xcode users

  • To get this to work correctly on your system, you can set a full path to the file in your code, or you can change the working directory for the Xcode project to the directory that contains the text file.
  • To change the working directory for the Xcode project, open the project and select the ProductàSchemeàEdit Scheme item. Then, select the Run category, check the Use Custom Working Directory box, and specify the working directory.

Text file

-----------------------

Jan   14317.41
Feb   3903.32
Mar   1073.01
Apr   3463.28
May   2429.52
Jun   4324.70
Jul   9762.31
Aug   25578.39
Sep   2437.95
Oct   6735.63
Nov   288.11
Dec   2497.49

Solutions

Expert Solution

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main() {
    ifstream infile("C:\\monthly_sales.txt");
    double sales[12];
    string months[12];
    int i = 0;
    double sale;
    string month;
    while (infile >> month >> sale)
    {
        sales[i] = sale;
        months[i] = month;
        i++;
    }
    char input;
    cout << "Monthly Sales\n";
    cout << "COMMAND MENU\n";
    cout << "m - View monthly sales\n";
    cout << "y - View yearly summary\n";
    cout << "x - Exit program\n";
    do {
        cout << "Command: ";
        cin >> input;
        switch (input) {
            case 'm':
                cout << "MONTHLY SALES\n";
                for(int i = 0; i < 12; i++){
                    cout << months[i] << "\t" << setw(10) << right << setprecision(2) << fixed << sales[i] << "\n";
                }
                break;
            case 'y':
                double sum = 0;
                for(int i = 0; i < 12; i++){
                    sum += sales[i];
                }
                double average = sum / 12;
                cout << "YEARLY SUMMARY\n";
                cout << "Yearly total:\t" << setw(10) << right << setprecision(2) << fixed << sum << "\n";
                cout << "Monthly average:\t" << setw(10) << right << setprecision(2) << fixed << average << "\n";
                break;
        }
    } while (input != 'x');
    return 0;
}

sample input:

Jan   2.456
Feb   5.56
Mar   66.123
Apr   32.0
May   23.01
Jun   88.003
Jul   99.666
Aug   87.567
Sep   1.123456
Oct   3.00
Nov   2.0003
Dec   55.21

sample output:

Monthly Sales
COMMAND MENU
m - View monthly sales
y - View yearly summary
x - Exit program
Command: y
YEARLY SUMMARY
Yearly total:   465.72
Monthly average:   38.81
Command: m
MONTHLY SALES
Jan   2.46
Feb   5.56
Mar   66.12
Apr   32.00
May   23.01
Jun   88.00
Jul   99.67
Aug   87.57
Sep   1.12
Oct   3.00
Nov   2.00
Dec   55.21
Command: x

Process finished with exit code 0

Please provide feedback for this answer.


Related Solutions

Project 6-1: Email Creator C++ code Create a program that reads a file and creates a...
Project 6-1: Email Creator C++ code Create a program that reads a file and creates a series of emails. Console Email Creator ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi James, We've got some great deals for you. Check our website! ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi Josephine, We've got some great deals for you. Check our website! ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi Art, We've got some great deals for you. Check our website! Specifications...
This is C++ Create a program that reads an HTML file and converts it to plain...
This is C++ Create a program that reads an HTML file and converts it to plain text. Console: HTML Converter Grocery List * Eggs * Milk * Butter Specifications: The HTML file named groceries.html contains these HTML tags: <h1>Grocery List</h1> <ul> <li>Eggs</li> <li>Milk</li> <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add asterisks (*) before the list items, and display the...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
Write a c++ program for the Sales Department to keep track of the monthly sales of...
Write a c++ program for the Sales Department to keep track of the monthly sales of its salespersons. The program shall perform the following tasks: Create a base class “Employees” with a protected variable “phone_no” Create a derived class “Sales” from the base class “Employees” with two public variables “emp_no” and “emp_name” Create a second level of derived class “Salesperson” from the derived class “Sales” with two public variables “location” and “monthly_sales” Create a function “employee_details” under the class “Salesperson”...
How to do in C++ HTML Converter Create a program that reads an HTML file and...
How to do in C++ HTML Converter Create a program that reads an HTML file and converts it to plain text. Console HTML Converter Grocery List * Eggs * Milk * Butter Specifications Your instructor should provide an HTML file named groceries.html that contains these HTML tags: <h1>Grocery List</h1> <ul>     <li>Eggs</li>     <li>Milk</li>     <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add...
Create a C Program that reads a file and replaces every other letter with an asterisk....
Create a C Program that reads a file and replaces every other letter with an asterisk. The first integer 'num' is the number of lines. Include an "Error" Message and exit the program if:    The wrong name for the input/output file is given    The input/output file can not be opened input.txt 7 Never Have We Ever Played A Game output.txt 7 N*v*r *a*e W* *v*r *l*y*d * G*m*
How do I create this program? Using C++ language! Write a program that reads data from...
How do I create this program? Using C++ language! Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL...
Create a project called rise. Add a class called GCD. Complete a program that reads in...
Create a project called rise. Add a class called GCD. Complete a program that reads in a numerator (as an int) and a denominator (again, as an int), computes and outputs the GCD, and then outputs the fraction in lowest terms. Write your program so that your output looks as close to the following sample output as possible. In this sample, the user entered 42 and 56, shown in green. Enter the numerator: 42 Enter the denominator: 56 The GCD...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT