0

The problem I have is that I am able to successfully tokenize the string parameterswhich 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;
Carsomyr
  • 11
  • 4
  • Use `std::getline()` and `std::istringstream` for further parsing. – πάντα ῥεῖ Dec 02 '16 at 17:11
  • @NathanOliver I need a solution that doesn't involve vectors or regex or boost or any of that other stuff. Simple basic homework here. – Carsomyr Dec 02 '16 at 17:17
  • @πάντα ῥεῖ Please explain a little better where you are going with that, new guy here. – Carsomyr Dec 02 '16 at 17:17
  • May be this helps a bit to understand what I mean: http://stackoverflow.com/questions/23047052/why-does-reading-a-record-struct-fields-from-stdistream-fail-and-how-can-i-fi – πάντα ῥεῖ Dec 02 '16 at 17:19
  • I want to know why it is that I can edit a string passed as a parameter to my function but not edit a string retrieved using getline. I'm looking for an explanation of the logic. Neither one of those links tells me that. Perhaps my question topic is worded poorly. – Carsomyr Dec 02 '16 at 17:33

0 Answers0