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

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 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...
Write a program in c++ that reads x[4]. Then create the array y[6||6], such that the...
Write a program in c++ that reads x[4]. Then create the array y[6||6], such that the first quarter of y consists of the value of the first element of x. The second quarter of y consists of the value of the second element of x. The third quarter of y consists of the value of the third element of x. The fourth quarter of y consists of the value of the fourth element of x?
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
*C++* /* This program reads a movie rating from 1 to 10 from the user and...
*C++* /* This program reads a movie rating from 1 to 10 from the user and prints a corresponding word for the rating. Programmer: Your name here Date:       Current date here */ #include #include using namespace std; int main() {       int rating; // user entered rating       cout << "Enter your movie rating (an integer from 1 to 10): ";       cin >> rating;       cout << "\nThe movie was ";       // Select the appropriate word for the...
C or C++ program Create a list of 5 things-to-do when you are bored (in the...
C or C++ program Create a list of 5 things-to-do when you are bored (in the text file things.txt, one line each), where each thing is described as a string of characters of length in between 10 and 50. Design a C program to read these things in (from stdin or by input redirection) and store them in the least memory possible (i.e., only the last byte in the storage for each string can be the null character). After reading...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT