Project

I am having so much trouble with this part of my project. How do I use the if statement to see if the player's name matches the string "BLANK"? Can anyone help me please?

This is what it is asking me to do:
loadData()- This function will load data from a text file that has been saved only if a previous session’s data was saved to the file. To track this, the player's default name must be "BLANK" (all uppercase). You can than use an if statement to see if the player's name matches that string "BLANK". If it does, you know the player shouldn't be allowed to input the continue option and must start a new game. Display that error message and set the option to new game. If data loads successfully from the last session, display it out to the screen.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int startMenu(int);
bool menuInputValidation(int);
string saveData(string);
string loadData(string);

int main()
{
string name;
int choice=0;


cout << "1. Continue" << endl;
cout << "2. Start a new game" << endl;
cout << "3. Quit" << endl;

startMenu(choice);

saveData(name);

loadData(name);



return 0;
}

int startMenu(int choice)
{

cin >> choice;

if (choice == 3)
{
exit(0);
}
if (choice >= 3)
{

}

return choice;
}

bool menuInputValidation(int choice)
{

if (choice >= 3)
{
return true;
}
else

return false;
}

string saveData(string name)
{

ofstream outputFile;
outputFile.open("data.txt");


cout << "Username:" << endl;
cin >> name;
outputFile << name << endl;
outputFile << "level 1" << endl;

outputFile.close();

return name;
}

string loadData(string name)
{

ifstream inputFile;
inputFile.open("data.txt");

if (inputFile.fail())
{
cerr << "Error" << endl;
exit(0);
}

inputFile.close();

return name;
}
Last edited on
std::string provides an overloaded "==" operator that you can use to test for the equality of string to others.

1
2
3
4
5
6
7
8
9
10
11
12
string string1 = "abcd";
string string2 = "xyzf";

if(string1 == string2)
{
    cout << "They are equal" << endl;
}
else
{
    cout << "They are not equal" << endl;
}


In this case, because the strings are not exactly the same, they would not be considered equal and "They are not equal" would print.
Last edited on
Topic archived. No new replies allowed.