Where it says their name, followed by values for their attack, defense, level, etc. And I want to make it so that a user can type in a name of a person that they want to use again, then have the program search for that name in the text file, and extract the name and the values that follow, but I don't know how to make it search the file and do this. If I could get a little direction or an example, it'd be really appreciated.
Read the data from the file into a string. Save the players entry into a string. use the 'compare()' member function from the string library to search through the data file for the entry made by the player. While you're at it read up on the 'stoi()' function which is also part of the string library (x11).
Thanks for the help, I got it to now read line by line, and it can find the specific line based on a search with the compare(). But now I don't know how to convert the string to integers, I looked up some stuff on the stoi() function, but I can't get it to work for me. At the moment it keeps giving me the error "stoi was not declared in this scope" Here is my code for this portion. I'm sure it's just some simple thing that I don't know.
void getSave() {
ifstream saves;
string characterChoice, line;
bool leave = 0;
saves.open("saves.txt"); //opens the file
if(saves.fail()){ //checks if the file opened right
cout << "saves file open failed" << endl;
return;
}
while(leave==0) {
cout << "\nWhat character from the saves do you want to get?\n";
cin >> characterChoice;
size_t pos;
while(saves.good())
{
getline(saves,line); // get line from file
pos=line.find(characterChoice); // search
if(pos!=string::npos) // string::npos is returned if string is not found
{
cout <<"Found!";
cout << endl << line << endl;
break;
}
}
cout << "\nIs this who you wanted\n1: Yes\n0: No\n";
cin >> leave;
}
int classStat = stoi(line);//supposed to convert from string to int
cout << classStat << endl;
saves.close();
return;
}