I have looked around on this forum and none of the past posts really addressed my issue. I created a .txt file with cust no first and last name. I open the file and read in the first line of the file, this part works well. And it displays to the screen correctly. I need to read the next line in the file to get the subject code, title, price and quantityto display on the screen. This part is not working. When I do a break point the subjectcode is 84 T. Is the 84 the equivelant to T? The title only brings in 3 char instead of the whole title.
I don't know if its they way I'm declaring the strings or if I'm not reading the next line of data correctly.
This forum has been a wonderful resource for newbie's like me
Following is the code up till the the subject code and title.
Here is the txt file. Thanks for any help
12345 Mike McGee
T C++ Programming 75.00 6
H History of C++ Program lanuage 45.00 5
R The Bible 100.0 10

#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;
	
    char SubjectCode;
//	char Title;
	double Price;
	int Quantity; 
	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;
	cout << endl;
//	infile >> SubjectCode >> Title >> Price >> Quantity;
	infile >> SubjectCode >> Title;
	cout << SubjectCode << setw(10) << Title << endl; //setw(25) << Price << setw(15) << Quantity << setw(15) << endl;

Recommended Answers

All 6 Replies

When using >> to read something, Only 1 word is read.You'll need to look at other options for reading multiple-word titles.

One idea is to read the entire line as a string and find the Price, Quantity, and Discount and convert them.

Another idea (easiest) is to reorder the file so the Title is at the end of each line. Then you can read the numbers and then the rest of the line as the title.

>>When I do a break point the subjectcode is 84 T. Is the 84 the equivelant to T?

Yes -- look at any ascii chart and see for youself. subjectcode is declared as char, therefore the >> operator will treat is as such. Declare it as an int then test again to see if that fixes the problem. If it doesn't then post a few lines of the data file so we can see what the program is trying to read.

Umm, we haven't reached the point in the class which tells us how to read in a string and convert or reorder data. Can I use getline(infile, title)?
Thank you

Umm, we haven't reached the point in the class which tells us how to read in a string and convert or reorder data.

Reordering the data take an editor. You edit the file so the data is in a different order so you can read it properly using the program.

Can I use getline(infile, title)?
Thank you

Yes.

Since the subjectcode is the first data item and the title is the second data item with more than one word in the title, should I read in the first data item and the next line use getline(infile, Title) and then read in the rest of the data items?

infile >> subjectcode >>
getline(infle, title)
infile >> price >> quantity >> endl;

I am so surprised this is such a nightmare. I guess I don't understand C++ variables very well.

Like one of you told me I had to put the title of the book at the end of the line. Thank you, Thank you.
Now I'm getting the pesky error

error C2143: syntax error : missing ';' before '{'

on the following code

While (infile)
{ this is where is telling me something is missing.
I paired up the brackets.  Any idea where else to look?
"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.