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;

It looks OK to me. Have you tried the easiest debugging of all? Use cout to tell you what's happening as you progress through the program. After the switch output a message. After a read, output the data. Stuff like that. Put them where you think the trouble spots are.

delete the while statement on line 64. It doesn't seem to have a useful purpose.

It looks OK to me. Have you tried the easiest debugging of all? Use cout to tell you what's happening as you progress through the program. After the switch output a message. After a read, output the data. Stuff like that. Put them where you think the trouble spots are.

change the statement (SubjectCode != 'EC') to (SubjectCode != 'E').

Why do you have two nested while statements? Are there more lines in the file after EC?

Yes, there are 3 different customers in the file. In my text file I put an E at the end of each customer.
12345 Mike McGee
T 75.00 6 C++ Programming
S 45.00 1 History of C++
H 100.00 1 The Bible
E
12346 Ronald Regan
H 50.00 12 Politics in the 80's
R 45.00 5 Religion in Politics
E
12347 Joe Bonamassa
L 35.00 10 Music of 90's
L 60.00 5 Back to the Blues
M 90.00 7 Math in Music
H 65.00 15 History of Blues
E
When it hits the E I have all the totals printed out and then I want to read in the next record which would be the next customer and that isn't happening. I have been at this almost non stop for 2 days, I can't spot what I'm doing wrong. I have the last lines commented out b/c I don't know what to do with them. Thanks for any help.

while (infile)
	{
	 while (SubjectCode != 'E')
		{
		  switch (SubjectCode)
		  {
			case 'T':	
			   if (Quantity > 5)
			   {
				   TAmtofDisc = (Quantity * Price) * TECH_DISC;
				   AccumTotal += Quantity * Price;
				   cout  << setw(4) << SubjectCode << setw(24) << Title << setw(10) 
					     << "$ " << Price << setw(11) << Quantity << setw(14) 
						 << "$ " << TAmtofDisc << endl;
			   }
			   else
     		   {
			    	TAmtofDisc = 0;
					AccumTotal += Quantity * Price;
					cout  << setw(4) << SubjectCode << setw(24) << Title << setw(10) 
					     << "$ " << Price << setw(11) << Quantity << setw(14) 
						 << "$ " << TAmtofDisc << endl;
			   }
			break;
			case 'S':
				if (Quantity > 2)
				{
				SAmtofDisc = (Quantity * Price) * SCIENCE_DISC;
				AccumTotal += Quantity * Price;
				cout  << setw(4) << SubjectCode << setw(24) << Title << setw(10) 
				  << "$ " << Price << setw(11) << Quantity << setw(14) 
				  << "$ " << SAmtofDisc << endl;
				}
				else 
				{
				SAmtofDisc = (Quantity * Price) * SCIENCE_DISC;
				AccumTotal += Quantity * Price;
				cout  << setw(4) << SubjectCode << setw(24) << Title << setw(10) 
					  << "$ " << Price << setw(11) << Quantity << setw(14) 
					  << "$ " << SAmtofDisc << endl;
			  }
			cout << endl;
			break;
			case 'H':
				   HAmtofDisc = (Quantity * Price) * HISTORY_DISC;
				   AccumTotal += Quantity * Price;
				   cout  << setw(4) << SubjectCode << setw(19) << Title << setw(15) 
					     << "$ " << Price << setw(10) << Quantity << setw(14) 
						 << "$ " << HAmtofDisc << endl;
				   cout << endl;
			break;
			case 'M':	
			   if (Quantity > 5)
			   {
				   MAmtofDisc = 0;
				   over5 = Quantity - 5;
				   Quantity = Quantity - over5;
				   AccumTotal += Quantity * Price;
				   cout  << setw(4) << SubjectCode << setw(24) << Title << setw(10) 
					     << "$ " << Price << setw(11) << Quantity << setw(14) 
						 << "$ " << MAmtofDisc << endl;
			   }
			   else
     		   {
			    	MAmtofDisc = 0;
					AccumTotal += Quantity * Price;
					cout  << setw(4) << SubjectCode << setw(24) << Title << setw(10) 
					     << "$ " << Price << setw(11) << Quantity << setw(14) 
						 << "$ " << MAmtofDisc << endl;
			   }
			break;
	       	} // end switch  

		  infile >> SubjectCode >> Price >> Quantity;
		  getline (infile,Title);

	    } // end 2nd while
		    SalesTaxAmt = AccumTotal * STATE_TAX;
			GrandTotal = AccumTotal + SalesTaxAmt;
		cout << "-------------------------------------------------------------------" << endl;
		cout << "Sub-Total: $ " << AccumTotal << endl;
		cout << "State Tax: $ " << SalesTaxAmt << endl;
		cout << "Total: $ " << GrandTotal << endl;
		infile >> SubjectCode >> Price >> Quantity;
				 getline (infile,Title);
		 if (SubjectCode = 'E')
		 {
		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;
		 } // end if
 } //end 1st while
//	if (SubjectCode = 'E')
//		 {
//		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;
//		 } // end if    
 system ("pause");
return 0;
}

I take it my suggestion didn't help at all.

Please note how hard it is to read your posted code. If you'd use 4 SPACEs instead of a TAB for your indentation, it would be much easier to read -- and it's more industry standard. I've never seen a company that indents a full TAB.

Visual Studio does most of the tabbing. Is there a way to set the tab stops? You are correct I didn't get the results I wanted with your suggestion. But that is not to say I don't appreciate all your help. I thank you for taking the time. I will clean up the code and resend if that will help you.
"T"

vc++ 2008 has an Option to replace tabs with spaces. I haven't figured out how to do the same with vc++ 2010 yet.

The code changes I sent you on allow the program to read the entire file correctly. I did nothing to fix any of the calculations.

Hi,
In the while loop, there is no option to handle the records which is starting with customer number like "12346".Because of this, the wile loop is not processing 2nd customer.

My suggestion:
1. use 'N' to indicate next records.if Subject code is 'N' read customerNumber,First name and LastName.
2. use 'E' to indicate end of file/records and stop reading data from file.
3.use structure to read the records from file based on the subject code.

Unfortunately the command to read the next customer was commented out. I understand what you are instructing me to do.
What happens in the file there is a seperate line for the end of cust indicator. So the programs reads in this one character and it doesn't stop trying to read the record so it drops down and picks up the next cust# and so when I ask it to read the cust# and the first and last name it has already moved onto the second line in the next coustomer which has the invoice info. example
while

Well, re-read the program I sent you via PM. You apparently just ignore it altogether or didn't understand it because I fixed that problem for you.

Sorry hit the wrong key. example
While
while not N
swicth
case T
calculations
case H
calulations
case R
calulations
read next record (at the end of the customer it puts N in the subjectcode field)

The while stmt picks up the N as end of customer drops down and calculates totals and displays for that customer and then I ask for the next record with cust# and name and the pointer in the file is in the wrong place. It is picking up the N and droping down to try and pick up the rest of the fields I've asked for in the next record.
I don't believe its a logic error, but I don't know C++ that well yet. I just keep thinking of COBOL and the control breaks are usually done on the changing of like the dept number and the record has data in all the fields. I think I'm going to change the data file so that the end of customer record has data in the fields all zeros or something. I know this isn't what the instructor wants, but thats the only way I know how to get this midterm completed. I'll let you know if it works. Thank you again for all your help.
"T"

**sigh**

I did go back and reread your post. I was so tired last night I mis read the instructions. I did what you suggested and yes it works. At the end of the file it is displaying the headings only. I don't know how to gt around that and right now I'm so tired I don't care.
I do have a question about displaying to the screen. When you use set() doesn't that line the output right justified? The lines that have zero disc are off by one column and I don't know why.
Thank you again for all your wonderful help. I'm quiting for the night.
"T"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.