I'm very new to C++, my midterm is requiring me to read from a txt file I created line by line. Each line has different subject code that requires some calculations. My understanding was that you open your file and read in the first line in the file, thats the priming read. I have Inside the while stmt is another while and a swtich stmts. I thought that the break stmt after the case stmt throws you out of the switch structure and puts at the end of the code. This is were I thought the next line of data is to be read in. Do have that much correct? I don't seem to be reading in the next line in the doc. Following is the first part of txt doc and the prgm so far.
12345 Mike McGee
T 75.00 6 C++ Programming
S 45.00 5 History of C++ Program lanuage
R 100.0 10 The Bible
EC
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
// declare constants
double TECH_DISC = .05;
double SCIENCE_DISC = .04;
double HISTORY_DISC = .03;
double lIT_DIC = .05;
double RELIGION_DISC = .05;
double STATE_TAX = .06;
int main()
{
//Declare and initialize variables
ifstream infile;
int CustomerNumber;
double AccumTotal = 0;
char SubjectCode;
// char Title;
double Price;
int Quantity = 0;
double SubTotalT;
double SubTotalS;
double SubTotalM;
double SubTotalH;
double SubTotalL;
double SubTotalR;
double TotalofInvoice;
double AmtofDisc;
string FirstName;
string LastName;
string Title;
infile.open("mid-term.txt");
if (!infile)
{
cout << "Cannot open the input file." << endl;
cout << "Program terminates!!!" << endl;
return 1;
}
cout << fixed << showpoint << setprecision(2);
infile >> CustomerNumber >> FirstName >> LastName;
cout << "-------------------------------------------------------------------" << endl;
cout << "Customer Number: " << CustomerNumber << setw(30)
<< "Customer Name: " << FirstName << " " << LastName << endl;
cout << "-------------------------------------------------------------------" << endl;
cout << endl;
cout << "Category" << setw(10) << "Title" << setw(25) << "Price" << setw(15) << "Quantity" << setw(15) << "Discount" << endl;
infile >> SubjectCode >> Price >> Quantity;
getline (infile,Title);
cout << endl;
// cout << setw(4) << SubjectCode << setw(24) << Title << setw(15) << Price << setw(12) << Quantity << endl;
while (infile)
{
while (SubjectCode != 'EC')
{
switch (SubjectCode)
{
case 'T':
if (Quantity > 5)
{
AmtofDisc = (Quantity * Price) * TECH_DISC;
AccumTotal += Quantity * Price;
cout << setw(4) << SubjectCode << setw(24) << Title << setw(10)
<< "$ " << Price << setw(11) << Quantity << setw(14)
<< "$ " << AmtofDisc << endl;
}
else
{
AmtofDisc = 0;
AccumTotal += Quantity * Price;
cout << setw(4) << SubjectCode << setw(24) << Title << setw(10)
<< "$ " << Price << setw(11) << Quantity << setw(14)
<< "$ " << AmtofDisc << endl;
}
break;
case 'S':
if (Quantity > 2)
{
AmtofDisc = (Quantity * Price) * SCIENCE_DISC;
AccumTotal += Quantity * Price;
cout << setw(4) << SubjectCode << setw(24) << Title << setw(10)
<< "$ " << Price << setw(11) << Quantity << setw(14)
<< "$ " << AmtofDisc << endl;
}
else
{
AmtofDisc = (Quantity * Price) * SCIENCE_DISC;
AccumTotal += Quantity * Price;
cout << setw(4) << SubjectCode << setw(24) << Title << setw(10)
<< "$ " << Price << setw(11) << Quantity << setw(14)
<< "$ " << AmtofDisc << endl;
}
cout << endl;
break;
} // end switch
infile >> SubjectCode >> Price >> Quantity;
getline (infile,Title);
cout << endl;
} // end 2nd while
} //end 1st while
system ("pause");
return 0;