Welcome to Dream.In.Code
Become an Expert!

Join 150,033 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,593 people online right now. Registration is fast and FREE... Join Now!




Creating Text-Based RPG

 
Reply to this topicStart new topic

Creating Text-Based RPG, Need help with some code.

kiasta
7 Jun, 2008 - 05:29 AM
Post #1

New D.I.C Head
*

Joined: 18 Nov, 2007
Posts: 19


My Contributions
Here is what I have so far


Game.cpp:

CODE


#include "game.h"

#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <string>
#include <stdio.h>

using namespace std;

bool Game::run(void)
{
    char key = ' ';
    char gender = ' ';
    char startop = ' ';
    char quit = ' ';
    const char *race;
    string gend;
    string fname;
    string lname;
    string age;
beginning:
    system("cls");
    cout << "\n Welcome to Dark Lotus RPG!!\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    cout << " (S)tart\n";
    cout << " (C)ontinue\n";
    cout << " (Q)uit";
    cout << "\n\n\n\n\n\n Dark Lotus was programmed by Daniel Hopkins.";
    while (startop != 's' || startop != 'c' || startop != 'q')
    {
        while (!getInput(&startop))
        {
        }
        if (startop == 's' || startop == 'S')
        {
            goto start;
        }
        else if (startop == 'c' || startop == 'C')
        {
            system("cls");
            cout << "\n Sorry this function is not working atm, please choose 'Start' or 'Quit'.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
            cout << " press enter...";
            cin.ignore(cin.rdbuf()->in_avail() +1);
            goto beginning;
        }
        system("cls");
        cout << "\n Are you sure you want to quit?(y/n)";
        while (quit != 'y' || quit != 'n')
        {
            while (!getInput(&quit))
            {
            }
            if (quit == 'y')
            {
                goto exit;
            }
            else if (quit == 'n')
            {
                startop = ' ';
                quit = ' ';
                goto beginning;
            }
        }
    }

start:

    system("cls");
    cout << "\n I need to acquire some information from you. Before we begin your long journey \n\n I would like to ask you a couple of questions.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    cout << " press enter...";
    cin.ignore(cin.rdbuf()->in_avail() +1);

info:

    system("cls");
    cout << "\n What is your first name? ";
    cin >> fname;
    system("cls");
    cout << "\n What is your last name? ";
    cin >> lname;
    system("cls");
    cout << "\n How old are you? ";
    cin >> age;
    system("cls");

    cout << "\n What is your gender?(m/f) ";
    while (gender != 'm' || gender != 'f')
    {
        while (!getInput(&gender))
        {
        }
        if (gender == 'm' || gender == 'M')
        {
        gend = "male";
        goto raceselection;
        }
        else if (gender == 'f' || gender == 'F')
        {
        gend = "female";
        goto raceselection;
        }
    }

raceselection:

    system("cls");
    cout << "\n Choose your race:\n\n";
    cout << "    Press A - Elf\n";
    cout << "    Press B - Human\n";
    cout << "    Press C - Orc\n";
    cout << "    Press D - Gnome\n";
    cout << "    Press E - Faery\n";
    cout << "    Press F - Halfling\n";
    cout << "    Press G - Half-Elf\n";
    cout << "    Press H - Dragonian\n";
    cout << "    Press I - Undead\n\n";
    cout << " Or Press Q - Quit";

    while (key != 'q')
    {
        while (!getInput(&key))
        {
        }
        if (key == 'a')
        {
            char elf = ' ';

            system("cls");
            cout << "\n Are you sure you want to be an Elf?(y/n)";
            
            while (elf != 'y' || elf != 'n')
            {
                while (!getInput(&elf))
                {
                }
                if (elf == 'y')
                {
                    race = "Elf";
                    goto cont;
                }
                else if (elf == 'n')
                {
                    goto raceselection;
                }
            }
        }
        else if (key == 'b')
        {
            char human = ' ';

            system("cls");
            cout << "\n Are you sure you want to be a Human?(y/n)";
            
            while (human != 'y' || human != 'n')
            {
                while (!getInput(&human))
                {
                }
                if (human == 'y')
                {
                    race = "Human";
                    goto cont;
                }
                else if (human == 'n')
                {
                    goto raceselection;
                }
            }
        }
        else if (key == 'c')
        {
            char orc = ' ';

            system("cls");
            cout << "\n Are you sure you want to be an Orc?(y/n)";
            
            while (orc != 'y' || orc != 'n')
            {
                while (!getInput(&orc))
                {
                }
                if (orc == 'y')
                {
                    race = "Orc";
                    goto cont;
                }
                else if (orc == 'n')
                {
                    goto raceselection;
                }
            }
        }
        else if (key == 'd')
        {
            char gnome = ' ';

            system("cls");
            cout << "\n Are you sure you want to be a Gnome?(y/n)";
            
            while (gnome != 'y' || gnome != 'n')
            {
                while (!getInput(&gnome))
                {
                }
                if (gnome == 'y')
                {
                    race = "Gnome";
                    goto cont;
                }
                else if (gnome == 'n')
                {
                    goto raceselection;
                }
            }
        }
        else if (key == 'e')
        {
            char faery = ' ';

            system("cls");
            cout << "\n Are you sure you want to be a Faery?(y/n)";
            
            while (faery != 'y' || faery != 'n')
            {
                while (!getInput(&faery))
                {
                }
                if (faery == 'y')
                {
                    race = "Faery";
                    goto cont;
                }
                else if (faery == 'n')
                {
                    goto raceselection;
                }
            }
        }
        else if (key == 'f')
        {
            char halfling = ' ';

            system("cls");
            cout << "\n Are you sure you want to be a Halfling?(y/n)";
            
            while (halfling != 'y' || halfling != 'n')
            {
                while (!getInput(&halfling))
                {
                }
                if (halfling == 'y')
                {
                    race = "Halfling";
                    goto cont;
                }
                else if (halfling == 'n')
                {
                    goto raceselection;
                }
            }
        }
        else if (key == 'g')
        {
            char halfelf = ' ';

            system("cls");
            cout << "\n Are you sure you want to be a Half-Elf?(y/n)";
            
            while (halfelf != 'y' || halfelf != 'n')
            {
                while (!getInput(&halfelf))
                {
                }
                if (halfelf == 'y')
                {
                    race = "Half-Elf";
                    goto cont;
                }
                else if (halfelf == 'n')
                {
                    goto raceselection;
                }
            }
        }
        else if (key == 'h')
        {
            char dragonian = ' ';

            system("cls");
            cout << "\n Are you sure you want to be a Dragonian?(y/n)";
            
            while (dragonian != 'y' || dragonian != 'n')
            {
                while (!getInput(&dragonian))
                {
                }
                if (dragonian == 'y')
                {
                    race = "Dragonian";
                    goto cont;
                }
                else if (dragonian == 'n')
                {
                    goto raceselection;
                }
            }
        }
        else if (key == 'i')
        {
            char undead = ' ';

            system("cls");
            cout << "\n Are you sure you want to be a Undead?(y/n)";
            
            while (undead != 'y' || undead != 'n')
            {
                while (!getInput(&undead))
                {
                }
                if (undead == 'y')
                {
                    race = "Undead";
                    goto cont;
                }
                else if (undead == 'n')
                {
                    goto raceselection;
                }
            }
        }
    }
    system("cls");
    quit = ' ';
    cout << "\n Are you sure you want to quit?(y/n)";
    while (quit != 'y' || quit != 'n')
        {
            while (!getInput(&quit))
            {
            }
            if (quit == 'y')
            {
                goto exit;
            }
            else if (quit == 'n')
            {
                quit = ' ';
                key = ' ';
                goto raceselection;
            }
        }

exit:

    system("cls");
    cout << "\n You have chosen to exit the game. Have a nice day!";
    cin.ignore(cin.rdbuf()->in_avail() +1);
    return true;

cont:

    char infconf = ' ';

infconf:

    system("cls");
    cout << "\n This is the information I have gathered:\n\n Your name is "
        << fname << " " << lname << " you are a " << gend << " " << race <<
        " and you are " << age << "\n\n years old. Is this information correct?(y/n/Q)";
    while (infconf != 'y' || infconf != 'n' || infconf != 'q')
    {
        while (!getInput(&infconf))
        {
        }
        if (infconf == 'y')
        {
            goto storystart;
        }
        else if (infconf == 'n')
        {
            goto info;
        }
        else if (infconf == 'q')
        {
            system("cls");
            cout << "\n Are you sure you want to quit?(y/n)";
            while (quit != 'y' || quit != 'n')
            {
                while (!getInput(&quit))
                {
                }
                if (quit == 'y')
                {
                    goto exit;
                }
                else if (quit == 'n')
                {
                    quit = ' ';
                    infconf = ' ';
                    goto infconf;
                }
            }
        }
    }

storystart:

    system("cls");
    cout << "\n Great to meet you, my name is sgk'uhmklit'rehtlire which roughly translates to \n\n Shayla in your native tongue.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    cout << " press enter...";
    cin.ignore(cin.rdbuf()->in_avail() +1);
    
    system("cls");
    cout << "\n This world is being devoured by the infamous Dark Lotus. You have been \n\n resurrected because you and you alone are the only one who can stop him.\n\n Please save our world from this vile Dragon.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    cout << " press enter...";
    cin.ignore(cin.rdbuf()->in_avail() +1);
}
bool Game::getInput(char *race)
{
    if (_kbhit())
    {
        *race = _getch();
        return true;
    }
    return false;
}







Main.cpp:

CODE


#include "game.h"

int main()
{
    Game gameControl;
    gameControl.run();
    return 0;
}






Game.h:

CODE


#ifndef GAME_H
#define GAME_H

class Game
{
public:
    bool run(void);
protected:
    bool getInput(char *race);
    void timerUpdate(void);

};
#endif






Now just remember this is not a game I am going to release, I am making this solely for learning purposes so it will not be very original. And also it is in a very original stage pretty much the the beginning. And also I am very new to programming so please don't bash on me too much about how I have things set up. If you have advise I would love to hear it of course.

What I am having trouble with is how to make the age only accept numbers instead of both.

*edit* Also the reason I have all those /n/n/n/n is just for me because the windows size in Visual Studio 2008 Express, i actually don't know how else to make it so it looks like the way it is without skipping lines like that. */edit*

This post has been edited by kiasta: 7 Jun, 2008 - 05:48 AM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Creating Text-Based RPG
7 Jun, 2008 - 06:09 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,904



Thanked: 159 times
Dream Kudos: 1375
My Contributions
Why not make age an int instead of a string?
User is offlineProfile CardPM
+Quote Post

kiasta
RE: Creating Text-Based RPG
7 Jun, 2008 - 06:19 AM
Post #3

New D.I.C Head
*

Joined: 18 Nov, 2007
Posts: 19


My Contributions
Well when I do that I am still able to input letters, and it just comes out with a weird number. Perhaps I am missing something? Also when letters (or whatever the proper term is) are entered it calls 0 when I acknowledge everything is correct and ends the program:

CODE
The program '[2940] DarkLotus.exe: Native' has exited with code 0 (0x0).


This post has been edited by kiasta: 7 Jun, 2008 - 06:23 AM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Creating Text-Based RPG
7 Jun, 2008 - 06:22 AM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,904



Thanked: 159 times
Dream Kudos: 1375
My Contributions
No you'll have to do a isDigit() check if you don't want letters.
User is offlineProfile CardPM
+Quote Post

kiasta
RE: Creating Text-Based RPG
7 Jun, 2008 - 06:25 AM
Post #5

New D.I.C Head
*

Joined: 18 Nov, 2007
Posts: 19


My Contributions
Oh ok I'll look that up and see how to properly apply it, thanks alot.
User is offlineProfile CardPM
+Quote Post

kiasta
RE: Creating Text-Based RPG
7 Jun, 2008 - 07:28 AM
Post #6

New D.I.C Head
*

Joined: 18 Nov, 2007
Posts: 19


My Contributions
OK so this is what I did:

CODE
    system("cls");
    cout << "\n How old are you? ";
    cin >> age;
    isdigit(age);
    system("cls");


Still the problem exists, they can still enter letters, and when they enter any letters it checks for numbers and then crashes. Unless I am missing the proper implication of this function. I guess perhaps I asked the wrong question, which I apologize for.
User is offlineProfile CardPM
+Quote Post

.Maleficus.
RE: Creating Text-Based RPG
7 Jun, 2008 - 01:37 PM
Post #7

D.I.C Head
**

Joined: 7 Mar, 2008
Posts: 127



Thanked: 2 times
My Contributions
Put it in a try..catch block to do error checking.
cpp
try {
isDigit(age);
} catch (exception& e) {
cout << "Please enter a number for the age";
}

Or something along those lines.

This post has been edited by .Maleficus.: 7 Jun, 2008 - 01:41 PM
User is offlineProfile CardPM
+Quote Post

kiasta
RE: Creating Text-Based RPG
9 Jun, 2008 - 06:25 AM
Post #8

New D.I.C Head
*

Joined: 18 Nov, 2007
Posts: 19


My Contributions
Well thanks alot for all the help, I still have not found a solution to my problem, but I have learned how to catch exceptions and such which will help me out in the future. I am just trying to find a way to make it where they cannot enter any letters at all for age. I am probably asking something that is very complicated or maybe you have told me and I am just too stupid to figure it out, but anyways thanks for the help.
User is offlineProfile CardPM
+Quote Post

stayscrisp
RE: Creating Text-Based RPG
9 Jun, 2008 - 05:13 PM
Post #9

D.I.C Regular
***

Joined: 14 Feb, 2008
Posts: 284



Thanked: 11 times
My Contributions
what about just bodgin it into if they enter a letter then you tell them this is not a correct value for this field please enter a number or something along those lines

if(!isDigit)
{
cout>> blah blah blah
}

meh it may work

This post has been edited by stayscrisp: 9 Jun, 2008 - 05:14 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:39PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month