I'm trying to make a simple program that can read data from a file (inventory.txt) into a structure and then output that structure to the user. Every time I run the program, if the part of the file that would be a string (with spaces) has anything on it, the rest of the data gets omitted. This is my code so far;
# include <fstream>
# include <iostream>
# include <string>
# include <iomanip>
using namespace std;
struct inven
{
int part,quant;
string descript;
float price;
};
int main()
{
cout << setprecision (2) << fixed;
inven inventory[100];
ifstream filein;
ofstream fileout;
filein.open ("inventory.txt");
if (filein.good())
{
for (int i = 0; i < 100; i++)
{
filein >> inventory[i].part;
getline (filein, inventory[i].descript);
filein >> inventory[i].quant;
filein >> inventory[i].price;
};
}
else
{
fileout.open ("inventory.txt", ios::trunc);
}
for (int i = 0; i < 100; i++)
{
cout << "Part Number: " << inventory[i].part << endl;
cout << "Quantity: " << inventory[i].quant << endl;
cout << "Description: " << inventory[i].descript << endl;
cout << "Price per item: $" << inventory[i].price << endl;
cout << "Total price: $" << inventory[i].quant * inventory[i].price << endl << endl << endl;
if (inventory[i+1].part == 0)
{
i=100;
}
}
return 0;
}
My text file currently looks like;
1
hammer
10
10
The above text file is really just a sample file, the finished product should be able to handle 100 different products.
Thanks in advance.