May 22, 2012 at 2:23am UTC
Im writing a program to practice with the language, but im getting some pretty weird output from code that seems right to me.
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <iostream>
#include <fstream>
#include <list>
struct Car
{
std::string make;
std::string model;
int partNo;
double price;
int quantity;
std::string partname;
};
void AddItem();
void _Update(int PartNo, int quantity);
void UpdateList(std::list<Car>& _Car);
int main()
{
std::list<Car> _Car;
UpdateList(_Car);
for (std::list<Car>::iterator iter = _Car.begin(); iter != _Car.end(); iter++)
{
std::cout << iter->make << " " << iter->model << " " << iter->partNo << " " << iter->price << " " << iter->quantity << " " << iter->partname << std::endl;
}
}
void UpdateList(std::list<Car>& _Car)
{
std::ifstream File("CarParts.txt" );
if (!File.is_open())
std::cerr << "Bad file input....... closing...." ;
while (!File.eof())
{
Car tempObj;
File >> tempObj.make >> tempObj.model >> tempObj.partNo >> tempObj.price >> tempObj.quantity;
getline(File,tempObj.partname);
_Car.push_back(tempObj);
}
File.close();
}
Outpost given:
Pajero NA1H25 1 3.65 11 BLADE W/S WIPER
Honda_Sivic R34gFk 2 4.97 15 ENGINE CHANGE
2 4.97 15
Notepad file:
Pajero NA1H25 1 3.65 11 BLADE W/S WIPER
HondaSivic R34gFk 2 4.97 15 ENGINE CHANGE
what is with the three numbers under the two lines i actually wanted printed out? It's really confusing me... Thanks if you can help!
Last edited on May 22, 2012 at 2:36am UTC
May 22, 2012 at 7:23pm UTC
You are looping on eof, which is an error. eof is not set until you attempt to read from the file after it is completely read.
May 22, 2012 at 7:34pm UTC
What should i use to replace the eof?
May 22, 2012 at 7:40pm UTC
Try
1 2 3 4 5 6 7
for (Car tempObj;
File >> tempObj.make >> tempObj.model >> tempObj.partNo
>> tempObj.price >> tempObj.quantity
&& getline(File, tempObj.partname) ; )
{
_Car.push_back(tempObj);
}
Or, better yet, give your Car an
operator >>
Last edited on May 22, 2012 at 7:40pm UTC
May 23, 2012 at 2:48am UTC
how would that for loop run? wouldn't it just run until it gets an error? Also, what could i do if a gave the Car an operator>>? I appreciate the help, please excuse my lack of knowledge!
May 23, 2012 at 10:27am UTC
@Need4Sleep just the way you thought your loop ran. It stops when it reaches the end of file.
If you gave Car an operator>>, you could write simpler loops:
1 2
for (Car tempObj; File >> tempObj; )
_Car.push_back(tempObj);
Or, you could create containers directly from input streams (through iterators):
1 2 3
ifstram File("CarParts.txt" );
istream_iterator<Car> beg(File), end;
list<Car> _Car(beg, end);
Last edited on May 23, 2012 at 10:27am UTC
May 23, 2012 at 7:02pm UTC
If car is not in a class, how would i give it the operator? i thought i could only use the overloading in a class
May 23, 2012 at 7:12pm UTC
A struct can have constructors, member functions, operator overloads, etc, just like any other class.