I've been working on this sales problem that has me write a program that takes in a list of salespersons ID number(1-10), item number(7-14), and quantity, example (2,7,4) and prints out sales value to an output file. My problem is that after the first line of data has been processed from the text file, my program no longer reads in values it seems, nor creates new files.
//only file created. corresponds to the first line of data
//file name is "salesOut.txt"
Sales Person: 7
Total Sales: $2415
Not sure if i'm not inputting in the rest of the values correctly after the first line. Also no more text files are created after the first prompt for an output text file.
#include <iostream>
#include <fstream>
#include <iomanip> //for data output manipulators
#include <string>
usingnamespace std;
int main()
{
int idNumber; //stores each sale persons ID number
int itemNumber; //stores product ID
int quantity; //stores number of specific items sold
double itemCost; //stores unit cost
string inFile; //file contains all sale persons information
string outFile;
ifstream inData;
ofstream outData;
cout << "Please enter the name of the data file "
<< "followed by the [Enter] key." << endl;
cin >> inFile;
inData.open(inFile.c_str());
if(!inData) //tests if file opened correctly
{
cout << "Could not open file." << endl;
return 1;
}
inData >> idNumber >> itemNumber >> quantity;
do
{
if(idNumber <= 0 || idNumber > 10) //checks for valid idNumbers
{
cout << "Invalid ID Number." << endl;
return 2;
}
switch(itemNumber) //assigns value to variable itemCost
{ //depending on itemNumber
case 7 : itemCost = 345.00;
break;
case 8 : itemCost = 853.00;
break;
case 9 : itemCost = 471.00;
break;
case 10 : itemCost = 933.00;
break;
case 11 : itemCost = 721.00;
break;
case 12 : itemCost = 663.00;
break;
case 13 : itemCost = 507.00;
break;
case 14 : itemCost = 259.00;
break;
default : cout << "Invalid item number." << endl;
break;
}
cout << "Enter the name of the output file." << endl; //prompt user for first output file
cin >> outFile;
outData.open(outFile.c_str());
outData << "Sales Person: " << idNumber << endl //calculations printed on output file
<< "Total Sales: $" << double(quantity) * itemCost << endl;
outData.close(); //close current outputfile before opening next file
inData >> idNumber >> itemNumber >> quantity;
}
while(inData);
return 0;
}