My goal is to create an object from a string pulled from a text file.
Text File
1 2 3 4
Suckers
Chocolate
Mints
JawBreakers
Each one of these in the text file is a class in the program.
I want to take each one create an object from it.
I want to also dynamically allocate an array of candy and initialize the elements with 1 to n.
Here are two functions I made to use the text file to my benefit.
For the numberOfCandy(), I'm planning on having some numbers added in later to the text file for other uses, that's why I have it set up to differentiate between an integer and not.
int numberOfCandy(){ //Return the first number it reads from file
string line;
int Result;
int total = 0;
ifstream myfile ("Candy.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
stringstream convert(line); // stringstream used for the conversion of line to an int
if ( !(convert >> Result) ) //give the value to Result using the characters in the string
Result = -1; //If it's not an integer set to -1
if(Result == -1) //If it's a Candy increment the candy count by 1
total++;
}
return total;
}
}
void createObjectCandy(){ //creates an object candy from the text file
string line;
int Result;
int i = 0;
int NUM = numberOfCandy(); //Number of Instruments in text file
char *stuff;
stuff = newchar[i];
ifstream myfile ("Candy.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
stringstream convert(line); // stringstream used for the conversion initialized with the contents of Text
if ( !(convert >> Result) ) //give the value to Result using the characters in the string
Result = -1; //If it's not an integer set to -1
if(Result == -1){
if(i<NUM) //This is where I'm struggling
strcpy(stuff, line.c_str()); //with the whole dynamically allocating
//and the object from the text file
cout << stuff[i] << endl;
i++;
}
}
myfile.close();
}
stuff[2].getName(); //And would like to do something like this if possible to display whatever
//Name is set to in the Mints class for example
}
Any advice or fix to this situation is greatly appreciated. Thank you!
So, I've been doing my research and started to learn polymorphism. I've implemented it into the program and got it to work correctly! So now I think my only issue is being able to use the line from the text and converting to be able to make an object.
void createObjectCandy(){ //creates an object instrument from the text file
string line;
int Result;
int NUM = numberOfCandy(); //Number of Candy in text file
vector<Candy *> cand;
ifstream myfile ("Candy.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
stringstream convert(line); // stringstream used for the conversion initialized with the contents of Text
if ( !(convert >> Result) ) //give the value to Result using the characters in the string
Result = -1; //If it's not an integer set to -1
if(Result == -1){
cand.push_back(new line()); //Conversion to be able to use "line" to create object??
}
myfile.close();
}
}
else cout << "Unable to open file";
stru.front()->Name(); //Test line
stru.back()->Name(); //Test Line
}
So I guess I'm down to only having the problem of how to convert "line" into a usable class name such as cand.push_back(new Suckers()); where line = Suckers.
I've searched and haven't found anything that was clear to me on how or even if this is possible.
Also, I haven't looked into this yet, but is it possible to return vector so I could use it in my main program?
Any help or advice would be much appreciated! Thank you!