The problem I have is that I am able to successfully tokenize the string parameters
which is input into my function menucall
, but using the same method I cannot properly tokenize a string obtained from a file using getline
and I do not understand why. Both strings are separated into tokens properly. All tokens made from the string parameters have their leading spaces removed, while all the leading spaces on tokens made from the string book are not.
Book and Bookstore are classes that i have created. There is more code after this but it is dependent on the tokenization of these 2 strings. My function call looks like this:
menuCall("1, BookInput.txt", store);
and the first line in my input file looks like this:
12345, Michael Main, Data Structures, 100.00
void menuCall(string parameters, Bookstore bookstore)
{
ifstream in;
int i, o, option;
int j = 0, p = 0;
string optionToken, bookToken, book;
string *sp, *so;
Book newBook;
sp = new string[5];
so = new string[4];
while (parameters != "") //Check if the string is empty
{
i = parameters.find(",", 0); //Find next comma in the string
if (i > 0) //If a comma is found
{
optionToken = parameters.substr(0, i); //Substring ends at first comma
parameters.erase(0, i + 1); //Delete substring and comma from original string
while (optionToken[0] == ' ') //Check for spaces at beginning of token
{
optionToken.erase(0, 1); //Delete spaces at beginning of token
}
}
if (i < 0) //If no comma is found
{
optionToken = parameters; //Substring ends at the end of the original string
parameters.erase(0, i); //Delete substring from original string
while (optionToken[0] == ' ') //Check for spaces at beginning of token
{
optionToken.erase(0, 1); //Delete spaces at beginning of token
}
}
sp[j] = optionToken; //Token is added to dynamic array
j++; //Keeps track of iterations
}
option = stoi(sp[0]); //Change option number from string to usable int
switch (option) //Switch to determine what menu option is to be performed
{
case(1): //Case 1
in.open(sp[1]); //Open file from extracted token containing file name
while (!in.eof())
{
getline(in, book); //Get string containing book information from input file file
while (book != "") //Check if the string is empty
{
o = book.find(",", 0); //Find the next comma in the string
if (o > 0) //If a comma is found
{
bookToken = book.substr(0, o); //Substring ends at first comma
book.erase(0, o + 1); //Delete substring and comma from original string
while (bookToken[0] == ' ') //Check for spaces at beginning of token
{
bookToken.erase(0, 1); //Delete spaces at beginning of token
}
}
if (o < 0) //If no comma is found
{
bookToken = book; //Substring ends at the end of the original string
book.erase(0, o); //Delete substring from original string
while (bookToken[0] == ' ') //Check for spaces at beginning of token
{
bookToken.erase(0, 1); //Delete spaces at beginning of token
}
}
so[p] = bookToken; //Token is added to dynamic array
p++; //Keeps track of iterations
}
}
break;