Im trying to write a c program using visual studio The progr
I\'m trying to write a c++ program using visual studio. The program is supposed to take a sales.dat file of the format: name unit quantity and out put a file for each salesperson with their total sales at the end of the file. My program does everything except print out the totals. No matter what I do the total is always 0. Here\'s the original instructions:
Write a program that generates sales-report files for a set of traveling salespeople. The salespeople call in their sales to a fulfillment desk, where the sales are all entered into a file. Each sale is recorded as one line on the file sales.dat as a salesperson last name (use enumeration type and make up the names), an item number, and a quantity, with all three items separated by blanks. There are 10 salespeople.
The company sells eight different products, with IDs numbered 7 through 14 (some older products have been discontinued). The unit prices of the products are given here:
Product Number
Unit Price
7
345.00
8
853.00
9
471.00
10
933.00
11
721.00
12
663.00
13
507.00
14
507.00
The program reads in the sales file, and generates a separate file for each salesperson containing just his or her sales. Each line from the sales file is copied to the appropriate salesperson’s file, with the salesperson’s name omitted. The file names should be the name of the salesperson with .dat appended (you may have to adjust names that don’t work as file names on your computer, such as hyphenated names or names with apostrophes). The total for the sale (quantity times unit price) is then appended to the record. At the end of processing, the total sales for each salesperson should be output with informative labels to cout. Use functional decomposition to design the program. Make sure that the program handles invalid salesperson’s names. If a salesperson’s name is invalid, write out an error message to cout. If a product number is invalid, write the error message to the salesperson’s file and don’t compute a total for the sale.
My code thus far:
#include<iostream>
#include<fstream>
#include<string>
#include<map>
#include<vector>
using namespace std;
enum salesperson {Carter, Hamilton, Higgs, MackyLynn, Martin, Morrison, ONeil, Pierce, Stien, Thornton};
vector<string> salesnames{ \"Carter\", \"Hamilton\", \"Higgs\", \"Macky-Lynn\", \"Martin\", \"Morrison\", \"O\'Neil\", \"Pierce\", \"Stien\", \"Thornton\" };
int main()
{
ifstream reader;
ofstream writer;
reader.open(\"sales.dat\");
map<string, salesperson> map1;
map<string, salesperson>::iterator *it;
map1[\"Carter\"] = Carter;
map1[\"Hamilton\"] = Hamilton;
map1[\"Higgs\"] = Higgs;
map1[\"Macky-Lynn\"] = MackyLynn;
map1[\"Martin\"] = Martin;
map1[\"Morrison\"] = Morrison;
map1[\"O\'Neil\"] = ONeil;
map1[\"Pierce\"] = Pierce;
map1[\"Stien\"] = Stien;
map1[\"Thornton\"] = Thornton;
float unitprice[] = { 345.00, 853.00, 471.00, 933.00, 721.00, 663.00, 507.00, 507.00 };
int id;
int quantity;
writer.open(\"Carter.dat\");
writer << \"Item Number\" << \" \" << \"Quantity Sold\" << endl;
writer.open(\"Hamilton.dat\");
writer << \"Item Number\" << \" \" << \"Quantity Sold\" << endl;
writer.open(\"Higgs.dat\");
writer << \"Item Number\" << \" \" << \"Quantity Sold\" << endl;
writer.open(\"MackyLynn.dat\");
writer << \"Item Number\" << \" \" << \"Quantity Sold\" << endl;
writer.open(\"Martin.dat\");
writer << \"Item Number\" << \" \" << \"Quantity Sold\" << endl;
writer.open(\"Morrison.dat\");
writer << \"Item Number\" << \" \" << \"Quantity Sold\" << endl;
writer.open(\"ONeil.dat\");
writer << \"Item Number\" << \" \" << \"Quantity Sold\" << endl;
writer.open(\"Pierce.dat\");
writer << \"Item Number\" << \" \" << \"Quantity Sold\" << endl;
writer.open(\"Stien.dat\");
writer << \"Item Number\" << \" \" << \"Quantity Sold\" << endl;
writer.open(\"Thornton.dat\");
writer << \"Item Number\" << \" \" << \"Quantity Sold\" << endl;
while (!reader.eof()){
string str;
reader >> str;
reader >> id;
reader >> quantity;
if (map1.find(str) == map1.end()){
cout << \"\ ----Name: \" << str << \" not found------\";
cin.get();
}
if (id<7 || id>14){
cout << \"\ -----\" << id << \" is Invalid product id------\";
cin.get();
}
string temp = str;
str += \'.\';
str += \'d\';
str += \'a\';
str += \'t\';
writer.open(str.c_str(), std::ios::app);
writer << id << \" \" << quantity << endl;
writer.close();
}
for (int isales = 0; isales <= Thornton; isales++) {
string name = salesnames.at(isales);
string name2 = name;
name2 += \'.\';
name2 += \'d\';
name2 += \'a\';
name2 += \'t\';
reader.open(name2);
string str2;
reader >> str2;
double total = 0;
int id2;
int quantity2;
while (!reader.eof()) {
reader >> id2;
reader >> quantity2;
double partial = quantity2 * unitprice[id2 - 7];
cout << partial << endl;
total += partial;
}
writer.open(name2.c_str(), std::ios::app);
writer << \"Total Sales: \" << total << endl;
cout << name << \" Total Sales: \" << total << endl;
}
cin.get();
return 0;
}
and the sales.dat I\'m currently using:
O\'Neil 7 5
Macky-Lynn 8 2
Thornton 9 2
Hamilton 10 10
Carter 11 5
Pierce 12 7
Stien 13 3
Higgs 14 9
Martin 8 2
Morrison 7 4
O\'Neil 9 5
Macky-Lynn 10 2
Thornton 11 2
Hamilton 13 10
Carter 9 5
Pierce 14 7
Stien 12 3
Higgs 7 9
Martin 10 2
Morrison 12 4
Pierce 13 7
Stien 14 3
Higgs 8 9
Martin 9 2
Morrison 12 4
O\'Neil 13 5
Macky-Lynn 7 2
Pierce 11 7
Stien 12 3
Higgs 12 9
Martin 9 2
Morrison 10 4
Pierce 7 7
Stien 12 3
| Product Number | Unit Price |
| 7 | 345.00 |
| 8 | 853.00 |
| 9 | 471.00 |
| 10 | 933.00 |
| 11 | 721.00 |
| 12 | 663.00 |
| 13 | 507.00 |
| 14 | 507.00 |
Solution
c++ code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string line;
ifstream myfile (\"sales.dat\");
std::map<string, int> totalsales;
totalsales[\"Carter\"] = 0; totalsales[\"Hamilton\"] = 0;
totalsales[\"Higgs\"] = 0;
totalsales[\"Macky-Lynn\"] = 0;
totalsales[\"Martin\"] = 0;
totalsales[\"Morrison\"] = 0;
totalsales[\"O\'Neil\"] = 0;
totalsales[\"Pierce\"]= 0; totalsales[\"Stien\"] = 0; totalsales[\"Thornton\"] = 0;
std::map<int, float> rate;
rate[7] = 345.00; rate[8] = 853.00; rate[9] =471.00 ; rate[14] = 507.00 ;
rate[10] = 933.00; rate[11] = 721.00 ; rate[12] = 663.00; rate[13] = 507.00;
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
string buf; // Have a buffer string
stringstream ss(line); // Insert the string into a stream
vector<string> tokens; // Create vector to hold our words
while (ss >> buf)
tokens.push_back(buf);
string name = tokens[0].c_str();
int code = atoi(tokens[1].c_str() ) ;
int quant = atoi(tokens[2].c_str() ) ;
if(totalsales.find(name) == totalsales.end())
{
cout << \"Error, \" << name << \" is invalid\" << endl;
}
else
{
if(rate.find(code) == rate.end())
{
cout << \"Error, product number is invalid\" << endl;
}
else
{
totalsales[name] = totalsales[name] + (rate[code])*quant;
}
}
}
myfile.close();
ofstream myfile;
for (std::map<string,int>::iterator it=totalsales.begin(); it!=totalsales.end(); ++it)
{
string fname = it->first;
fname.append(\".dat\");
myfile.open(fname.c_str());
myfile << it->second << \'\ \';
myfile.close();
}
}
else
{
cout << \"Unable to open file\" << endl;
exit(1);
}
return 0;
}




