this is my first question, so I may miss the "correct structure".
Anyway, I have a header file, with a function. This function (void readFile()) is defined in a cpp file. Within this definition I have code which repeats itself a lot.
If it was in main, I would simply declare a new function, define the repeatable in it, and then call everytime the function. But since it's in a non-main cpp file, I am having issues with this process.
Basically, what my function does, is read through a file char by char, and saves the data to different objects, based on the text.
My code looks like:
source.open("bookings.txt", std::ios::in);
char c;
source.get(c);
while (c != '|'){
CurrentID.push_back(c);
source.get(c);
}
object.setID(CurrentID)
This code repeats itself, replacing only the line of "object.setID". I tried declaring function "search(std::ifstream x, char y, std::string z);" with definition
void Search(std::ifstream x, char y, std::string z){
x.get(y); // next after |
while (y != '|'){
z.push_back(y);
x.get(y);
}
}
But if I try to call this function within my "void readFile()" definition, like this:
// First block as a repeatable
source.get(c);
while (c != '|'){
CurrentID.push_back(c);
source.get(c);
}
object->setID(CurrentID)
CurrentID.clear();
// second block as a function, with repeatable code commented out
void Search(std::ifstream quelle, char c, std::string &CurrentID);
/* source.get(c);
while (c != '|'){
CurrentID.push_back(c);
source.get(c);
}*/
object->setPrice(stof (CurrentID));
CurrentID.clear();
It jumps from "CurrentID.clear()" in first block, directly to "object->setPrice" in second block, ignoring the existence of the void Search function. Any proposition how to make the function work, or maybe other way, to remove repeated code?