0

I am currently having issues with parsing a string to fill variables. At the moment the string is filled with unnecessary whitespace which I am erasing. After that, my goal is to parse the data into their specific variables

Reservation::Reservation() : resID(), resName(""), email(""), people(""), day(""), hour(""){}

Reservation::Reservation(const std::string& m_res) : stringfile(m_res)
{
    while (stringfile.find(" ") != std::string::npos) {
        auto pos = stringfile.find("");
        stringfile.erase(pos);
    }
    . 
    this->resName = stringfile.substr(0,8);
    std::cout << resName << std::endl;
}

Above is a snippet of my code. What is currently happening is that it seems everything gets erased. When running a test of the program the output is just whitespace. If I do this instead this->resName = m_res.substr(0,8);, it will return what I want but none of the whitespaces is trimmed.

To do this task I am using substr(). Is there something I am blindly missing? I'm not sure as to why my entire stringfile is blank, even if I just print``std::cout << stringfile << std::endl;`

Here is a snippet of the text file that needs to be parsed to make things easier

# ID    : Name    ,             email, # of people, Day, Time
#------------------------------------------------------------
 RES-001: John    ,  [email protected]  ,           2,   3,    5

I am also lost on how to find and parse each section to their own variable. It seems straight forward but I just can't figure it out.

3
  • at the moment the string is filled with unnecessary whitespace which I am erasing -- There is no need to do this if you used std::istringstream. Also, are those commas part of the input text file? Commented Oct 4, 2019 at 20:39
  • yes they are. the are skewed on purpose. @PaulMcKenzie Commented Oct 4, 2019 at 20:48
  • @user4581301 my dearest apologies. I walked away from my laptop and i guess my friend was being an a**hat. I'm sorry Commented Oct 4, 2019 at 21:30

1 Answer 1

1

The simplest thing is to

  1. Remove the commas from the input
  2. Use std::istringstream to parse the input

Here is an example:

#include <sstream>
#include <string>
#include <iostream>
#include <algorithm>

struct record
{
    std::string res, firstname, email;
    int numpeople, day, time;
};

int main()
{
   std::string test = "RES-001: John    ,  [email protected]  ,           2,   3,    5";

   // remove the commas by replacing with spaces
   std::replace(test.begin(), test.end(), ',', ' ');
   std::cout << "This is the string without commas\n" << test << "\n\n";

   // now use streams to read in the string
   std::istringstream strm(test);
   record rec;
   strm >> rec.res >> rec.firstname >> rec.email >> rec.numpeople >> rec.day >> rec.time;

   // output results
   std::cout << rec.res << "\n";   
   std::cout << rec.firstname << "\n";   
   std::cout << rec.email << "\n";   
   std::cout << rec.numpeople << "\n";   
   std::cout << rec.day << "\n";   
   std::cout << rec.time << "\n";   
}

Output:

This is the string without commas
RES-001: John       [email protected]              2    3     5    

RES-001:
John
[email protected]
2
3
5
Sign up to request clarification or add additional context in comments.

4 Comments

I think replacing the commas with withe spaces, would be faster than erasing
@PaulMcKenzie is there any way to make a loop and iterate through each "res" instead of them all printing at once? the file has more than one line. this is perfect but ive never used strm before and am confused on how it works. lets say theres 20 lines in the file and i want to check if each falls under a certain "time" to sort them into breakfast/lunch etc would this be possible using this method?
See the struct? Create an array or vector of those structs.
@PaulMcKenzie how exactly could i do that? ive never used vectors before. sorry i am completely lost in my project. all the documentation ive read has just boggled my mind. this is the first solution i have hope in

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.