13 Replies - 1398 Views - Last Post: 16 February 2012 - 01:37 PM Rate Topic: -----

#1 Sid++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-February 12

Task with c++ text adventure game:

Posted 16 February 2012 - 08:48 AM

#include <iostream>

using namespace std;


int main()


{
    system("color 0A");
    cout <<"\tWelcome to Krinick...\n";

    char userName[100];

    cout <<"\nWhat is your name?: ";

    cin >>userName;

    cout <<"Very well, "<<userName<<"\n\n";

    

    cout <<"Where were you born? (1, 2, 3): \n";

    cout <<"1 - Carngean Empire (Human)\n";
    cout <<"2 - Fraulian Hold (Orc)\n";
    cout <<"3 - Delvian Darkwoods (Elf)\n";

    int pickRace;

    cout <<"...";

    cin >>pickRace;

    

    switch (pickRace)

    {

           case 1:
           

                cout <<"What is a Carngick doing in Krinick? None-the-less I grant you access\n";
                cout <<"through the gates of our world.../n";
                break;

           case 2:

                cout <<"Ahh, an Orc. Do you fancy a blacksmith?\n";

                break;
                case 3:
                cout <<"Your a long way from your friendly darkwoods, Elf.\n";
                cout <<"Fortunately we have woods here in Krinick...\n";
                break;

           default:

                   cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
                   
                   

    }

    

    int Gender;

    cout <<"\nWhat gender are you, if you do not mind my asking?\n";

    cout <<"1 - Male\n";

    cout <<"2 - Female\n";



    cout <<"...";

    cin >>Gender;

    

    switch (Gender)

    {

           case 1:

                cout <<"You are a male.\n\n";

                break;

           case 2:

                cout <<"You are a female.\n\n";

                break;


           default:

                   cout <<"Error - Invalid input; only 1,2 or 3 allowed.\n";

    }

  
    {                  
                       char Answer[100];
                       
                   cout <<"Let me stamp you papers. But before I do, What weopon do you use for\n";
                   cout <<"killing your enimies?\n";
                   
                   cin >>Answer;
                   
                   cout <<""<<Answer <<"?\n\n";
                   cout <<"Interesting...\n";
                   cout <<"I seem to have a(n)"<<Answer<<" in my possession.\n";
                   cout <<"Here, as a gift.\n";
                   cout <<"Got "<< Answer<<"\n";
                   
  
                   cout <<"The gates open and the world of Krinick expands in front of you.\n";
                   cout <<"You set off to the main city Cryzean " <<Answer<<" in hand.\n"; 
                              
    }
    
    
                              
    
   system("pause");
}



I want to know if it is possible to change case 1, 2etc... to letter or even word choices? Example: Zork, Dungeon choises...
Also, how do you tell your program to remember cin codes throughout the program, such as a name or item?

Is This A Good Question/Topic? 0
  • +

Replies To: Task with c++ text adventure game:

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,209
  • Joined: 23-August 08

Re: Task with c++ text adventure game:

Posted 16 February 2012 - 09:04 AM

Letter yes, word no. cases in a switch statement must be integers (character are actually integers...check out an ASCII table).
Was This Post Helpful? 1
  • +
  • -

#3 Sid++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-February 12

Re: Task with c++ text adventure game:

Posted 16 February 2012 - 09:09 AM

View PostJackOfAllTrades, on 16 February 2012 - 09:04 AM, said:

Letter yes, word no. cases in a switch statement must be integers (character are actually integers...check out an ASCII table).

Are unsure of the cin question?
Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Task with c++ text adventure game:

Posted 16 February 2012 - 09:16 AM

Giving someone a choice of numbers is much easier than getting them to type in a word and then comparing it. As for your other question, when you declare pickRace and then it gets set to whatever is passed in from std::cin it will stay at that value until you change it, you can then use it further into the program. You might want to just rename it to playerRace.

Another option is to create an array of strings that hold the different races as strings.

std::string raceNames[3];
raceNames[0] = "Human";
raceNames[1] = "Orc";
raceNames[2] = "Elf";

int playerRace = 0;

std::cin >> playerRace;

// user inputs 3, playerRace is now 3 an elf

void printPlayerStats()
{
    std::cout << "Player stats\n\n";
    std::cout << "Name: " << userName << "\n";
    std::cout << "Race: " << raceNames[playerRace - 1] << "\n";
}

// etc etc



you might also think about making player a class or a struct and holding the data in there.

Hope that helps some :)
Was This Post Helpful? 1
  • +
  • -

#5 Sid++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-February 12

Re: Task with c++ text adventure game:

Posted 16 February 2012 - 09:28 AM

When I close one and open another it states that the cin >>Eample>> is undeclared although it is used before...
I am confused...

This post has been edited by stayscrisp: 16 February 2012 - 09:39 AM
Reason for edit:: removed quote

Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Task with c++ text adventure game:

Posted 16 February 2012 - 09:39 AM

You want it to remember the data after you close the application?
Was This Post Helpful? 0
  • +
  • -

#7 Sid++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-February 12

Re: Task with c++ text adventure game:

Posted 16 February 2012 - 10:00 AM

View Poststayscrisp, on 16 February 2012 - 09:39 AM, said:

You want it to remember the data after you close the application?

Yes and no but what I meant to say was after I close one function and start another it "forgets" the cin hence the undeclared message...
Was This Post Helpful? 0
  • +
  • -

#8 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Task with c++ text adventure game:

Posted 16 February 2012 - 10:13 AM

This is because variables only exist in the scope they are created. For example if you create a variable inside a function then it will only exist inside that function. If you wish to use variables in different functions then you have a few choices.

You could use global variables

#include <iostream>
int global = 0; // a global variable

void setGlobal()
{
    global = 10;
}

void printGlobal()
{
   std::cout << global;
}

int main()
{
    setGlobal();
    printGlobal();

    return 0;
}



Or you could do what I previously suggested and make your player data into a struct

#include <iostream>

struct Player
{
    int race;
    std::string userName;
};

void printPlayerStats(Player* player)
{
    std::cout << "-- Player Stats --\n\n";
    std::cout << player->race << "\n";
    std::cout << player->userName << "\n";
}

void changePlayerStats(Player* player)
{
    player->race += 1;
    player->userName = "newUser";
}

int main()
{
     Player thePlayer; // create an instance of your player struct

     thePlayer.race = 1;
     thePlayer.userName = "Bob";

     printPlayerStats(&thePlayer);
     changePlayerStats(&thePlayer);
     printPlayerStats(&thePlayer);

     return 0;
}



This is a much better option because global variables are hideous. http://c2.com/cgi/wi...VariablesAreBad
Was This Post Helpful? 0
  • +
  • -

#9 Serapth  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 153
  • Joined: 17-August 11

Re: Task with c++ text adventure game:

Posted 16 February 2012 - 11:05 AM

View PostJackOfAllTrades, on 16 February 2012 - 04:04 PM, said:

Letter yes, word no. cases in a switch statement must be integers (character are actually integers...check out an ASCII table).




This is actually not right; switch expression must be integral types. This means a switch can handle char, int, bool, short and long, but not float.


More importantly however, switch can work with enum types, which is exactly what you want here.


An enum(eration) can be thought of as a single variable with a fixed set of ( behind the scenes numeric ) values. Therefore what you can do is declare:

enum Races { 
Human = 0,
Elf,
Troll,
Goat, 
French };





Then in your switch you can use:

player.Race = Human;
...

....

switch(player.Race)
{
 case Human:
   DoHumanStuff();
   break;
 case Elf:
   DoFairyWussyStuff();
   break;
 case French:
   FaireDesChosesFrançaises();
   break;
 default:
   DoNothing();
}




Enums are inititally confusing, but in the end, are exactly what you want.


This coincidentally is one of those things that makes C# much more appropriate for new developers, in C# you could simply do:

string race = "Human";
switch(race.toLower())
{
  case "human":
    DoHumanStuff();
}





Vastly easier to comprehend, although an order of magnitude slower.

This post has been edited by Serapth: 16 February 2012 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

#10 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Task with c++ text adventure game:

Posted 16 February 2012 - 11:28 AM

This depends on what the OP wants to achieve. My impression is that he wants a user to be able to input a word to choose their race, enums would not help to achieve this. The only thing using enums would improve is code readability.

Also, JackOfAllTrades is right. All of the types you mentioned are in fact integers. Integral types are integers, at least where c++ is concerned.
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,209
  • Joined: 23-August 08

Re: Task with c++ text adventure game:

Posted 16 February 2012 - 11:34 AM

View PostSerapth, on 16 February 2012 - 01:05 PM, said:

View PostJackOfAllTrades, on 16 February 2012 - 04:04 PM, said:

Letter yes, word no. cases in a switch statement must be integers (character are actually integers...check out an ASCII table).




This is actually not right; switch expression must be integral types. This means a switch can handle char, int, bool, short and long, but not float.


More importantly however, switch can work with enum types, which is exactly what you want here.


All of which are, in fact, integers at their core.
Was This Post Helpful? 1
  • +
  • -

#12 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Task with c++ text adventure game:

Posted 16 February 2012 - 11:39 AM

I was editing as you posted that :)
Was This Post Helpful? 0
  • +
  • -

#13 Serapth  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 153
  • Joined: 17-August 11

Re: Task with c++ text adventure game:

Posted 16 February 2012 - 12:36 PM

View PostJackOfAllTrades, on 16 February 2012 - 06:34 PM, said:

View PostSerapth, on 16 February 2012 - 01:05 PM, said:

View PostJackOfAllTrades, on 16 February 2012 - 04:04 PM, said:

Letter yes, word no. cases in a switch statement must be integers (character are actually integers...check out an ASCII table).




This is actually not right; switch expression must be integral types. This means a switch can handle char, int, bool, short and long, but not float.


More importantly however, switch can work with enum types, which is exactly what you want here.


All of which are, in fact, integers at their core.


Brain fart, my bad. I read int...
Was This Post Helpful? 0
  • +
  • -

#14 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Task with c++ text adventure game:

Posted 16 February 2012 - 01:37 PM

View PostSerapth, on 16 February 2012 - 07:36 PM, said:

Brain fart, my bad.

Haven't heard that phrase for the longest while! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1