Reading in from a file

Started by
1 comment, last by TheUnbeliever 15 years, 10 months ago
I'm working on a school assignment where we have to read in a large set of data and manipulate customer orders. So we have files that have the customers, credit card #'s, orders, etc. The problem I am having is on a very trivial aspect, and it's frustrating me so badly so I was hoping someone could help me out. Something is wrong with this section of my code I think

for ( int x=0; x< numCards ;x++)
		{
			int cardNum=0, cardMonth=0,cardYear=0,cardCode = 0;
			string cardType;

			fileReader >> cardNum;
			fileReader >> cardMonth;
			fileReader >> cardYear;
			fileReader >> cardCode;
			getline (fileReader,cardType);

			
			custToInsert->AddCreditCard(new CreditCard (cardNum,cardMonth,cardYear,cardCode,cardType));


		}
This section of the code is supposed to be reading in credit card information. Right before the loop it reads in the number of credit cards to load, each credit cards information is stored on a single line with tabs in between the data. The problem is this code works only once for some reason. IE if you had numcards = 2 then it will read in the first line of credit card information correctly, but it won't read the second line in correctly (it will just not put anything into the variables). fileReader is a ifstream by the way. So does anyone know why this would read in the variables of one line and then not work for the second line on? Edit: FIXED After wasting a total of 2 hours on this stupid problem, it turned out i should have made the variables of type long long, it was having overflow problems. [Edited by - Mr_Threepwood on June 7, 2008 7:14:52 PM]
Advertisement
So from the looks of things the layout of your file looks something like this?:

numberOfCardscardNumber      cardMonth      cardYear      cardCode      "card type"cardNumber      cardMonth      cardYear      cardCode      "card type"cardNumber      cardMonth      cardYear      cardCode      "card type"..........


Are you sure the number of cards is being given the correct value?
Quote:Original post by Mr_Threepwood
Edit: FIXED
After wasting a total of 2 hours on this stupid problem, it turned out i should have made the variables of type long long, it was having overflow problems.


[TheUnbeliever]

This topic is closed to new replies.

Advertisement