trouble getting information using ifstream

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream infile;
ifstream infile2;
ofstream thisweek;
ofstream sales;
ofstream orders;

int id;
int itemsInStock;
int itemsOnOrder;
int month;
int day;
int year;
int itemNumbers;
int NumTransactions;


char skip;
char transactionType;

float price;
float sellingPrice;


string itemName;



infile.open("lastweek.txt");
infile2.open("transaction.txt");
thisweek.open("thisweek.txt");
sales.open("sales.txt");
orders.open("orders.txt");


while(infile >> ws && !infile.eof())
{
infile >> id >> itemName >> itemsInStock >> itemsOnOrder >> price >> sellingPrice;
cout << id << " " << itemName << " " << itemsInStock << " " << itemsOnOrder << " " << price << " " << sellingPrice << endl;
}

cout << endl;


while(infile2 >> ws && !infile2.eof())
{
infile2 >> month >> skip >> day >> skip >> year >> transactionType >> itemNumbers >> NumTransactions;
cout << month << skip << day << skip << year << " " << transactionType << " " << itemNumbers << endl;
}

//thisweek << id << " " << itemName << " " << itemsInStock << " " << itemsOnOrder << " " << price << " " << sellingPrice << endl;


infile.close();
infile2.close();
thisweek.close();
sales.close();
orders.close();
return 0;
}


this is my code and my transactions file is:

11/29/2010 S 2907 1
11/29/2010 S 9673 4
11/30/2010 A 4321 30
11/31/2010 S 9673 12
12/01/2010 N 5789 wind_chimes 13.54 17.83
12/02/2010 S 5140 5

im trying to get only the date, the letter, and the 4 numbers. but everytime i try and get the information it skips the last line anyone tell me what im doing wrong?
You read a file that is made out of int'/'int'/'int'/' char int int however 5th line is int'/'int'/'int'/' char int string float float. When you tell stream to read an int but there is a string, an error occurs. If you don't want to read anything after the four digit number, use infile2.ignore(std::numeric_limits<std::streamsize>::max(), '\n');. If you find that confusing, you could also red a rubbish string with getline.

by the way, std::numeric_limits needs #include <limits>
Last edited on
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <limits>

using namespace std;

int main()
{
ifstream infile;
ifstream infile2;
ofstream thisweek;
ofstream sales;
ofstream orders;

int id;
int itemsInStock;
int itemsOnOrder;
int month;
int day;
int year;
int itemNumbers;
int NumTransactions;


char skip;
char transactionType;

float price;
float sellingPrice;


string itemName;
string date;



infile.open("lastweek.txt");
infile2.open("transaction.txt");
thisweek.open("thisweek.txt");
sales.open("sales.txt");
orders.open("orders.txt");


while(infile >> ws && !infile.eof())
{
infile >> id >> itemName >> itemsInStock >> itemsOnOrder >> price >> sellingPrice;
cout << id << " " << itemName << " " << itemsInStock << " " << itemsOnOrder << " " << price << " " << sellingPrice << endl;
thisweek << id << " " << itemName << " " << itemsInStock << " " << itemsOnOrder << " " << price << " " << sellingPrice << endl;

}

cout << endl;


while(infile2 >> ws && !infile2.eof())
{
infile2.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
infile2 >> date >> transactionType >> itemNumbers;
cout << date << " " << transactionType << " " << itemNumbers << endl;

}


sales << "DATE" << '\t' << "ITEM #" << '\t' << "ITEM" << '\t' << "QTY" << '\t' << "PRICE" << '\t' << "TOTAL" << endl;

//(transactionType == 'S')
//sales << month << skip << day << skip << year <<


infile.close();
infile2.close();
thisweek.close();
sales.close();
orders.close();
return 0;
}


this is my code and my transactions file is:

11/29/2010 S 2907 1
11/29/2010 S 9673 4
11/30/2010 A 4321 30
11/31/2010 S 9673 12
12/01/2010 N 5789 wind_chimes 13.54 17.83
12/02/2010 S 5140 5


i read in the file and yes it worked (thank you), however now instead of reading the second line which is 11/29/2010 it skips it and repeats the last line. any idea why?
Topic archived. No new replies allowed.