Reading in a string with spaces

Greetings

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.
Show us your failed attempts and we might be able to help you fix them.
Areas in bold are where I have been changing things...I have been saving over each new attempt. Sorry there is so much code...




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

using namespace std;

ifstream infile;
ofstream outfile;




/* Function Prototypes: */

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 */



/* Functions */

void multiply(float &product,int amount,float cost)
{ product=amount*cost;
return;
}
/************************************************************************************************/
void percent(float &stockpercent,float value,float total_inventory)
{ stockpercent=100*value/total_inventory;
return;
}

void header()
{ outfile<<left<<setw(6)<<"Item"<<" ";
outfile<<setw(20)<<"Item"<<" ";
outfile<<setw(6)<<"# in"<<" ";
outfile<<setw(6)<<"Item"<<" ";
outfile<<setw(8)<<"Stock"<<" ";
outfile<<setw(7)<<"% Total"<<" ";
outfile<<setw(8)<<"Re-order"<<" "<<endl;

outfile<<left<<setw(6)<<"#"<<" ";
outfile<<setw(20)<<"Name"<<" ";
outfile<<setw(6)<<"Stock"<<" ";
outfile<<setw(6)<<"Price"<<" ";
outfile<<setw(8)<<"Value"<<" ";
outfile<<setw(7)<<"Stock"<<" ";
outfile<<setw(8)<<"Flag"<<" "<<endl;

outfile<<"***************************************************************************"<<endl;
return;
}

void border()
{ outfile<<"___________________________________________________________________________"<<endl;
}

void printout(int a,char b[20], int c,
float d, float e, float f, string g)
{ outfile<<left<<setw(6)<<a<<" ";
printchar(b);
outfile<<setw(6)<<c<<" ";
outfile<<setw(6)<<fixed<<setprecision(2)<<d<<" ";
outfile<<setw(8)<<fixed<<setprecision(2)<<e<<" ";
outfile<<setw(7)<<fixed<<setprecision(3)<<f<<" ";
outfile<<setw(8)<<g<<endl;
return;
}


void printchar(char ary[20])
{ for(int a=0;a<20;a++)
{outfile<<ary[a];
}
return;
}





/* 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;

while(!infile.eof())
{ count=count+1;
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;
}

/* Bubble sort by item number */
for(int j=0;j<count;j++)
{ for(int i=0;i<count-1;i++)
{ if(input[i].item_num > input[i+1].item_num)
{ intable temp;
temp=input[i];
input[i]=input[i+1];
input[i+1]=temp;
}
}
}

/* copy values from input to output array */
for(int i=0;i<count;i++)
{ output[i].item_number=input[i].item_num;
for(int j=0;j<20;j++)
{ output[i].item_name[j]=input[i].item[j];
}

output[i].quantity=input[i].item_qnt;
output[i].price=input[i].item_price;
}

/* 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;
}

/* Calculate Stock Percentages */
for(int i=0;i<count;i++)
{ percent(output[i].stock_percent,output[i].stock_value,total_stockval);
}
/* Printed Output */
outfile<<"Store Inventory Report:"<<endl<<endl;
header();
for(int i=0;i<count;i++)
{ printout(output[i].item_number,output[i].item_name,output[i].quantity,output[i].price,
output[i].stock_value,output[i].stock_percent,output[i].flag);
}
border();

/* Summary data report - Total Stock Value */
outfile<<endl<<"Total Stock Value: "<<fixed<<setprecision(2)<<total_stockval<<endl;
border();

/* Resort data by highest Stock percent */
for(int j=0;j<count;j++)
{ for(int i=0;i<count;i++)
{ if(output[i].stock_percent < output[i+1].stock_percent)
{ outtable temp2;
temp2=output[i];
output[i]=output[i+1];
output[i+1]=temp2;
}
}
}

/* Summary Data - Highest Invetory Valued Stock Percentage */
outfile<<endl<<"The highest Inventory-valued item comprises "<<fixed<<setprecision(3)
<<output[0].stock_percent<<"% of Inventory Value."<<endl<<endl;

header();
for(int i=0;i<1;i++)
{ printout(output[i].item_number,output[i].item_name,output[i].quantity,output[i].price,
output[i].stock_value,output[i].stock_percent,output[i].flag);
}
border();

/* Summary Data - Highest 3 Inventory Stocks Compbined Percentage */
float three_stock;
three_stock=output[0].stock_percent+output[1].stock_percent+output[2].stock_percent;
outfile<<endl<<"The 3 highest Inventory-valued items comprise "<<fixed<<setprecision(3)
<<three_stock<<"% of Inventory Value."<<endl<<endl;

header();
for(int i=0;i<3;i++)
{ printout(output[i].item_number,output[i].item_name,output[i].quantity,output[i].price,
output[i].stock_value,output[i].stock_percent,output[i].flag);
}
border();

/* Close filestreams */
infile.close();
outfile.close();
return 0;
}
Last edited on
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.
}
Topic archived. No new replies allowed.