In: Computer Science
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
Note for Xcode users
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
#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.