-1

I am trying to make a text based C++ game.

I have a player class set up and I am now working on a method inside that class called displayMenu(), which will ask the user a variety of questions based on their player and send the data to the main/client code and then that data will create an object of player class via a constructor of player class.

My main question is...

  1. I am trying to compare the input (string) from the user to the (string) they need to inputting, but I am getting an error which says "lower()" can not be resolved. I believe you have to compare each character, but I think there may be a more effective way to code this to provide simplicity and readability. What exactly am I doing wrong? What is the best way to code this?

Here is my code...

void Player::displayMenu(std::string& PlaName, std::string& cName, int& lvl, int& HP)
{
    std::cout << "Player Creation Menu" << std::endl;
    std::cout << "====================" << std::endl;
    std::cout << std::endl;
    std::cout << std::endl;
    std::cout << "What is your name? " << std::endl;
    std::cin >> PlaName;
    std::cout << "What is your specitality? " << std::endl;
    std::cin >> cName;
    while(cName.lower() != "brawler" || cName.lower() != "thief" || cName.lower() != "persuader" || cName.lower()
            != "gunman")
    {
        std::cout << "That is not your true specitality..." << std::endl;
        std::cout << "You must pick from { 'Brawler', 'Thief' , 'Persuader', 'Gunman' }" << std::endl;
        std::cin >> cName;

    }
}
Bowen
  • 55
  • 8
  • 2
    `std::string` has no method named `lower`. – Aykhan Hagverdili Mar 28 '20 at 17:14
  • Does this answer your question? [How to convert std::string to lower case?](https://stackoverflow.com/questions/313970/how-to-convert-stdstring-to-lower-case) – Aykhan Hagverdili Mar 28 '20 at 17:24
  • You may want to consider using a table for the menus. The entries can include a pointer to the function to execute for the selection. Also search the internet for "c++ state design pattern". – Thomas Matthews Mar 28 '20 at 17:48
  • @Ayxan yes but how would I even code that? it is insane, like how would I go character by character and make sure it is right?? – Bowen Mar 28 '20 at 18:25
  • Not sure what is insane for you. The linked duplicate has a good answer with code examples. But iterating over a string and applying a function to each character should be a basic task anyway. Take it as a challenge ;) – Lukas-T Mar 28 '20 at 18:41

1 Answers1

0

I have several remarks on your original code:

  1. Reading and comparing the strings seems like a bit complicated for this use-case. It is common to see usage of first character as identifier, to make it simpler.
  2. The specialty is a classic example for enum (or enum class, which is enum that you must use always with it's name)
  3. The displayMenu method should not be part of the Player class, since it isn't a behavior (an action) of the player. It should be part of the "Game"/"UI" class.

If you really want to use the complete string in order to identify the specialty, you can use the code examples in the first answer in the link Ayxan put in the comments.

Here is my proposed code:

#include <iostream>

constexpr char INVALID_CHARACTER_INPUT = '@';

enum class CharacterSpecialty 
{
    BRAWLER,
    THIEF,
    PERSUADER,
    GUNMAN,
    NUM_OF_SPECIALITY_TYPES
};
`
class Player
{
    public:
        Player(const std::string& player_name, CharacterSpecialty char_specialty) :
            name(player_name),
            specialty(char_specialty)
        {
        }
    private:
    std::string name;
    CharacterSpecialty specialty;
};

Player displayMenuAndCreatePlayer()
{
    std::cout << "\nPlayer Creation Menu\n"  << "====================\n\n" << std::endl;

    std::cout << "Enter your name: " << std::endl;
    std::string player_name{};
    std::cin >> player_name;

    CharacterSpecialty char_specialty = CharacterSpecialty::NUM_OF_SPECIALITY_TYPES;

    while(char_specialty == CharacterSpecialty::NUM_OF_SPECIALITY_TYPES)
    {
        std::cout << "What is your specialty?\n" << "[B]rawler, [T]hief, [P]ersuader or [G]unman"<< std::endl;
        std::string char_type_input;
        std::cin >> char_type_input;

        char input = char_type_input.size() == 1 ? char_type_input[0] : INVALID_CHARACTER_INPUT;

        switch(char_type_input)
        {
            case 'b':
            case 'B':
                char_specialty = CharacterSpecialty::BRAWLER;
                break;
            case 't':
            case 'T':
                char_specialty = CharacterSpecialty::THIEF;
                break;
            case 'p':
            case 'P':
                char_specialty = CharacterSpecialty::PERSUADER;
                break;
            case 'g':
            case 'G':
                char_specialty = CharacterSpecialty::GUNMAN;
                break;
            default:
                std::cout << "Invalid Specialty Entered!\n" << std::endl;
                break;
        }
    }

    return Player(player_name, char_specialty);
}

int main()
{
    Player player = displayMenuAndCreatePlayer();
}
Kerek
  • 1,106
  • 1
  • 8
  • 18
  • is there anyway you can private message me and explain some coding to me? – Bowen Mar 29 '20 at 01:57
  • As far as I know, there is no way to communicate over SO. If it is connected to this code, you can ask here if you want to. – Kerek Mar 29 '20 at 13:32
  • well I have a lot of questions. How would I begin to even make a text based game? Where should I start? What are some good practices? I feel like I get started on a project and then boom I hit a wall and then I feel like I am in the complete wrong direction and not making the correct code to complete the project. Feel like I am way off.. – Bowen Mar 29 '20 at 21:56
  • Start by designing your project - you must know *what* you are going to do, before you start programming. When you know what you want to do, think about how you are going to write it down (it doesn't mean you need to know it 100%). Mainly how do you divide the code into classes (and always remember the rule - 1 class 1 responsibility!). You can even draw the class diagram of the project. Then, when you are stuck, look at SO for questions that has been answered about it. Good luck! – Kerek Mar 30 '20 at 17:04