Jump to content
  • Advertisement
Sign in to follow this  
KiNgSGB

Text Based RPG

This topic is 1957 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm fairly new to programming and I'm making a text based RPG as I learn. I have the part where it asks for your name but the problem is when selecting a class. Instead of allowing the user to input and having the console reply, it ends the program. Here is the code. Any help would be appreciated.

 

 

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main (int nNumberofArgs, char* pszArgs[])
{
    int name;
    int classs;
    int choice;
    cout <<"What is your name stranger?"
    <<endl;
    cin >>name;
    cout <<"Choose a class:"<< endl;
    cout <<"1. Warrior"<< endl;
    cout <<"2. Mage"<< endl;
    cout <<"3. Rogue"<< endl;
    cout <<"4. Cleric"<< endl;
    cin >>choice;
    system("PAUSE");
 
    switch (classs)
    {
    case '1':
        cout <<"You have chosen to be a warrior.";
        break;
    case '2':
        cout <<"Yoy have chosen to be a mage.";
        break;
    case '3':
        cout <<"You have chosen to be a rogue.";
        break;
    case '4':
 
        cout <<"You have chosen to be a cleric.";
        break;
    }
    system("PAUSE");
    return 0;
 
 
}
 

Share this post


Link to post
Share on other sites
Advertisement
You are calling cin with choice, but you are calling switch on classs, which was declared but never initialized with anything and never set to any value. Since there is no default: tag in your switch, and since the value of classs is likely not 1,2,3 or 4, given that it was never initialized or set, then the switch falls through without any execution branch being taken and the program will exit after the pause.

Edit: Additionally, you are using a variable of type int for name. I assume you want to use a string there instead. Edited by JTippetts

Share this post


Link to post
Share on other sites

I fixed it but then right after entering the name it doesn't let me input the choice value. It just says "Press Key to Continue". Which then exits me out. Here is the updated one.

 

 

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main (int nNumberofArgs, char* pszArgs[])
{
    int name;
    int classs;
    int choice = 0;
    cout <<"What is your name stranger?"
    <<endl;
    cin >>name;
 
    cout <<"Choose a class:"<< endl;
    cout <<"1. Warrior"<< endl;
    cout <<"2. Mage"<< endl;
    cout <<"3. Rogue"<< endl;
    cout <<"4. Cleric"<< endl;
    cin >>choice;
    system("PAUSE");
 
    switch (choice)
    {
    case '1':
        cout <<"You have chosen to be a warrior.";
        break;
    case '2':
        cout <<"Yoy have chosen to be a mage.";
        break;
    case '3':
        cout <<"You have chosen to be a rogue.";
        break;
    case '4':
 
        cout <<"You have chosen to be a cleric.";
        break;
    }
    system("PAUSE");
    return 0;
 
 
}

Share this post


Link to post
Share on other sites
I suspect you are trying to enter an actual string for name, but since a string can't be stored in an int it's gobbling some input and throwing it in name, gobbling some more of the input for choice, and falling through because choice holds miscellaneous input from entering a name. Change name to a string type if you want to actually enter a name.

Edit: Also, look at your switch statement again. You are calling cin >> choice, where choice is an int, but in the switch statement you are comparing choice with the character literals '1', '2', '3', and '4' rather than the integers 1,2,3,4. Edited by JTippetts

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!