Can you reformate the input file?
If this is a project where you must use that specified input, you need to parse each line, by looping through that buffer you read in each loop.
After you read a line using getline the first 5 chars of buffer are the id. Then comes the name, which ends with a space. Then you have the 3 numbers, all together. You will have to break these apart. ie first 3 chars after the space represent the quanity. the next 3 represent the reorder point, and the remaining 3 are the order size.
I would reformat the input file to look like this
BKT01 015 020 025 Prunes
BKT05 032 020 025 Pears
BKT07 011 020 025 Peaches
Declare the vars
char id[6],name[100];
int qty,reorder,size;
Then to read in a line do this.
stock >> id; //read in the id
stock >> qty >> reorder >> size; //read these in as ints
stock.getline(name,99,'\n'); //get the rest of the line which is the name
Put that code inside your while loop, and use the reformatted input file. Now you can do
if( qty <= reorder )
{
cout << "You should order " << size << " units of " << name << " now!!" << endl;
}
Code:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int main()
{
char id[6],name[100];
int qty,reorder,size;
ifstream stock ("stock.dat");
if (! stock.is_open())
{
cout << "Error opening file";
exit (1);
}
while (! stock.eof() )
{
stock >> id; //read in the id
stock >> qty >> reorder >> size; //read these in as ints
stock.getline(name,99,'\n'); //get the rest of the line which is the name
if( qty <= reorder )
{
cout << "You should order " << size << " units of " << name << " now!!" << endl;
}
} //end while loop
return 0;
}
Using this as stock.dat
BKT01 015 020 025 Prunes
BKT05 032 020 025 Pears
BKT07 011 020 025 Peaches