I have written a program that is to read in data from a file.
One line of input appears like:
item# itemname: qnt price safe
00456 base balls 16 3.42 10
Everything works with one exception.
I created an infile stream but dont know how to store a 20 character itemname such as "base balls" as a string since there is a space in the itemname. All item names are 20 cahracters including spaces. (If I delete spaces in the input file my code works as desired)
I have looked at get() and getline(), but have failed in my attempts to use these...I have also tried making a character array but cannot figure out how to read this in from the file and then save it as a string.
Any help would be appreciated I've been at this for four hours.
void multiply(float &product,int amount,float cost);
/* multiply finds the float value of amount multiplied by cost and passes it back */
void header();
/* header provides the header line for the output table(s). */
void percent(float &stockpercent,float value,float total_inventory);
/* percent calculates the stock percent given a value compared to total */
void border();
/* border places a line when called to add to aesthetics of the output */
void printout(int a,char b[20],int c,float d,float e,float f,string g);
/* printout prints the columns of the table and works within a counted loop in
* program */
void printchar(char ary[20]);
/* printchar prints out a 20 character array one character at a time */
/* Main Function */
int main ()
{ infile.open("invt2.txt");
outfile.open("inventory2.out");
/* Create structures to handle input and output data */
struct intable
{ char item[20];
int item_num;
int item_qnt;
float item_price;
int safe_stock;
};
struct outtable
{ int item_number; char item_name[20];
int quantity;
float price;
float stock_value;
float stock_percent;
string flag;
};
/* Create input and output arrays of proper structure above */
typedef intable Array_in[100];
typedef outtable Array_out[100];
Array_in input;
Array_out output;
/* loop with primer to fill input variable array*/
int count=0; infile.getline(input[count].item,20);
infile>>input[count].item_num;
infile>>input[count].item_qnt;
infile>>input[count].item_price;
infile>>input[count].safe_stock;
/* Identify items to be flagged as understock */
for(int i=0;i<count;i++)
{ if(input[i].item_qnt < input[i].safe_stock)
{ output[i].flag="FLAG";
}
else
{ output[i].flag=" ";
}
}
/* Multiply quantity * price to calculate stock for each */
for(int i=0;i<count;i++)
{ multiply(output[i].stock_value,output[i].quantity,output[i].price);
}
/* Sum total store stock */
float total_stockval;
total_stockval=0.0;
for(int i=0;i<count;i++)
{ total_stockval=total_stockval+output[i].stock_value;
}
What kind of logic can be used to tell if it's a two word name ? If there was a ':' after it, it would be easier to tell. Now you can only check that there are two words. The thing to use is that int i; file >> i; will fail if there is a string in the stream.
Reading that line could look like
1 2 3 4 5 6 7 8 9
file >> first_number;
std::string name;
file >> name;
while( ! file >> another_number ){//repeat until a number is found
file.clear();//clear error flags
std::string part_name;
file >> part_name;//read another word
name += " " + part_name;//append it to the name.
}