Second Input...

This is a discussion on Second Input... within the Game Programming forums, part of the General Programming Boards category; Here's my code: Code: #include <iostream> using std::cin; using std::cout; using std::endl; int main(int argc,char *argv[]) { char A,S,D,W,move,move2,name[20]; { ...

  1. #1
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    Unhappy Second Input...

    Here's my code:

    Code:
    #include <iostream>                                                        
    using std::cin;                           
    using std::cout;
    using std::endl;
    int main(int argc,char *argv[])                
    {                                                                                               
      char A,S,D,W,move,move2,name[20];
      {                                             
        cout<<"SirCrono6 _ "<<endl;           
        cout<<"               /I /"<<endl;           
        cout<<"A text-   /  / "<<endl;           
        cout<<"based  /S /  "<<endl;
        cout<<"RPG    /   /   "<<endl;
        cout<<"         /O/    "<<endl;
        cout<<"        /_/     "<<endl;   
        cout<<" "<<endl;
        cout<<" "<<endl;
        cout<<"  Welcome to Iso."<<endl;
        cout<<"It is a world of"<<endl; 
        cout<<"of Warriors, mages,"<<endl;  
        cout<<"and more.  Enjoy"<<endl;
        cout<<"your stay at Iso,"<<endl;
        cout<<"for you may not"<<endl;
        cout<<"return..."<<endl;
        cout<<" "<<endl;
        cout<<"What is your name? ";
        cin.getline(name,20,'\n');
        cout<<" "<<endl;
        cout<<"Hello "<<name;
        cout<<"! Welcome to Iso!"<<endl;
        cout<<"                                          Forward=W"<<endl;
        cout<<"                                          Left=A"<<endl;
        cout<<"                                          Right=D"<<endl;
        cout<<"  You see a guard infront of you.         Back=S"<<endl; 
        cout<<"To your left, a castle.  To your "<<endl;
        cout<<"right, a gate.  What will you do?"<<endl;
        cin>>move;
      }
      if(move==A)
      {
        cout<<"The gate is locked, you turn back."<<endl;
      }
      else if(move==D)
      {
        cout<<"A town lies before you, the exit is"<<endl;
        cout<<"infront of you.  What will you do?"<<endl;
        cin>>move2;
      }
      if(move2==S)
      {
        cout<<"There is no reason to go back now."<<endl;
      }
      else if(move2==W)
      {
        cout<<"You are at the gate, will you go out?"<<endl;
      }
      return 0;
    }
    I'm making a text-based RPG, I got out all the errors so far and stuff. You can put your name and enter it, but when you go to put L or R at the second input, it will close as soon as you push enter! I like RPGs and I want to program my own, but with this I can't really go on much...
    Needing Help,
    SirCrono6

    Note: The messed up Iso title is because of the size of this window...
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    50
    • you need single quotes around your comparisions, eg move2 == 'A'
    • your program will still not work as you want it to unless you run it from the console, because once it reaches the end of the code it will quit and close the console. you need a input loop, with a specific input to escape (such as Q for quit). I would suggest a switch - case.
    • your going to be repeating yourself a LOT if you try to make a long program in this way. i suggest you try to modularise your code with functions to collect input and string tables for the user prompts/outputs. generally try to think about how to use the language to avoid any repetition.

  3. #3
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Alright I took your code and played with it.... basically this is to illustrate a game loop using a while(bPlay) loop. For txt rpg you need a lot of resource files(text or database) to store descriptions otherwise you will have a lot of hard coded well... code.. then it will have 0 scalability unless you add stuff and recompile it...
    Code:
    //---------------------------------------------------
    //  Includes
    //---------------------------------------------------
    #include <iostream>
    //---------------------------------------------------
    //  Namespaces
    //---------------------------------------------------
    using std::cin;                           
    using std::cout;
    using std::endl;
    //---------------------------------------------------
    //  Proto Types
    //---------------------------------------------------
    void StartupScreen();
    void Begin(char* myname);
    void Ending();
    //---------------------------------------------------
    //  Main Function
    //---------------------------------------------------
    int main(int argc,char *argv[])                
    {
        bool bPass=false;
        char Name[20];
        char Act;
        char test;
        StartupScreen();
        //Get Player Name
        while (!bPass)
        {
            cout<<"Please enter in a name:";
            cin>>Name;
            cout<<"\nIs your name "<<Name<<"? (y/n)";
            cin>>test;
            if (test=='y')bPass=true;
            if (test=='Y')bPass=true;
        } 
        Begin(Name);
        //Main Game Loop
        bool bPlay=true;
        while(bPlay)
        {
            cout<<"What will you do?"<<endl;
            cin>>Act;
            switch(Act)
            {
                case 'W':
                    cout<<"You are at the gate."<<endl;
                    break;
                case 'w':
                    cout<<"You are at the gate."<<endl;
                    break;
                case 'A':
                    cout<<"The gate is locked, you turn back."<<endl;
                    break;
                case 'a':
                    cout<<"The gate is locked, you turn back."<<endl;
                    break;
                case 'S':
                    cout<<"There is no reason to go back now."<<endl;
                    break;
                case 's':
                    cout<<"There is no reason to go back now."<<endl;
                    break;
                case 'D':
                    cout<<"A town lies before you, the exit is"<<endl;
                    cout<<"infront of you."<<endl;
                    break;
                case 'd':
                    cout<<"A town lies before you, the exit is"<<endl;
                    cout<<"infront of you.?"<<endl;
                    break;
                case 'Q': bPlay=false;break;
                case 'q':bPlay=false;break;
            }
        }                                                                         
        Ending();
        system("PAUSE");
        return 0;
    }
    //--------------------------------------------------
    //  Support Functions
    //--------------------------------------------------
    void StartupScreen()
    {
        cout<<"SirCrono6 _ "<<endl;           
        cout<<"               /I /"<<endl;           
        cout<<"A text-   /  / "<<endl;           
        cout<<"based  /S /  "<<endl;
        cout<<"RPG    /   /   "<<endl;
        cout<<"         /O/    "<<endl;
        cout<<"        /_/     "<<endl;   
        cout<<" "<<endl;
        cout<<" "<<endl;
        cout<<"  Welcome to Iso."<<endl;
        cout<<"It is a world of"<<endl; 
        cout<<"of Warriors, mages,"<<endl;  
        cout<<"and more.  Enjoy"<<endl;
        cout<<"your stay at Iso,"<<endl;
        cout<<"for you may not"<<endl;
        cout<<"return..."<<endl;
        cout<<" "<<endl;
    }
    void Begin(char* myname)
    {
        cout<<"Hello "<<myname;
        cout<<"! Welcome to Iso!"<<endl;
        cout<<"Action Keys:"<<endl;
        cout<<"Forward=W"<<endl;
        cout<<"Left=A"<<endl;
        cout<<"Right=D"<<endl;
        cout<<"Back=S"<<endl; 
        cout<<"You see a guard infront of you."<<endl;
        cout<<"To your left, a castle.  To your "<<endl;
        cout<<"right, a gate. (Enter Q to Quit)"<<endl;
    }
    void Ending()
    {
        cout<<"Thank you for playing\n";
    }
    Attached is the C file.
    In the attached C file you need to remove the words "What will you do?" from the switch statement in which you answer 'D'.
    Attached Files Attached Files
    Last edited by BillBoeBaggins; 11-26-2003 at 03:07 PM.

  4. #4
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Can I continuosly use move instead of using move2 etc.? Also, at the start, if they enter A how do I get it to loop back and not end?
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    132
    With the code that BillBoeBaggins posted, the program will not end even if they enter a (or A). It will continue to run unless q (or Q) is pressed. This is whats known as an input loop, the program wont end until that one condition is met.

    For a text-based RPG (if you want to hard code all the descriptions), I would suggest that you create a new function that all the d, s, a, and f (or whatever the four movements were) are passed to this function. Perhaps have it as follows:

    Code:
    void charmove(char dir)
    {
            static int charloc=0;
    
            if( dir == 'a' || dir == 'A' ) {
                      // move character right
            }
            else if( dir == 'd' || dir == 'D' ) {
                      // move character forward
            }
            .
            .
            .
            etc.
    }
    In the above, based on the charloc variable (which since it was declared as static will remain until the program ends and so you don't have to worry about losing the location of the character), you can move the character around the RPG world you created. Give each "room" or "location" a number and then based on that number you could display the appropriate description (perhaps through another function?) as well as all possible movements, etc.

    Hard coding all locations in this manner can get quite annoying but instead of hardcoding you could store all movement possiblities, description, etc. in a file (say each file would be called <charloc>.loc ... where <charloc> is, replace with the id number mentioned above given to each room or location) Then as each location is accessed, read in the information and store in a structure or class (depending if your using pure C++ or not) and then you have your game pretty much all wrapped up (as far as movements around the world go). Just have to finish the combat code for the game and it's just about a fully functional RPG game.

    Hope this helps,
    Tyouk

  6. #6
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Note: I made a new code:

    Code:
    #include <iostream>    
    using std::cin;
    using std::cout;
    using std::endl;    
    int main(int argc,char *argv[])    
    {  
      char Quit,Act,Act2,Name[20];
      while(Quit='Q')
      {  
        cout<<"             __      "<<endl;           
        cout<<"SirCrono6   |I |     "<<endl;           
        cout<<"Presents:   |  |     "<<endl;           
        cout<<"            |  |  I  "<<endl;
        cout<<"Iso,        |  |     "<<endl;
        cout<<"            |S |  S  "<<endl;
        cout<<"A           |  |     "<<endl; 
        cout<<"text-based  |  |  O  "<<endl;
        cout<<"RPG         |  |     "<<endl;
        cout<<"            |O |     "<<endl;
        cout<<"            |__|     "<<endl;
        cout<<"                     "<<endl;
        cout<<"                     "<<endl;
        cout<<"  Welcome to Iso, a  "<<endl;
        cout<<"world of warriors,   "<<endl;
        cout<<"mages, and more!     "<<endl;
        cout<<"Enjoy your stay at   "<<endl;
        cout<<"Iso, for you may not "<<endl;
        cout<<"return...            "<<endl;
        cout<<"                     "<<endl;
        cout<<"                     "<<endl;
        cout<<"What is your name?   "<<endl;
        cin.getline(Name,20,'\n');
        cout<<"                     "<<endl;
        cout<<"Ah! Welcome "<<Name<<"!"<<endl;
        cout<<"                                      Action Keys: "<<endl;
        cout<<"You are in a town, to                 5 Forward    "<<endl;
        cout<<"you left is a castle,                 2 Backward   "<<endl;
        cout<<"to your right, a gate                 1 Left       "<<endl;
        cout<<"What will you do?                     3 Right      "<<endl;
        cin>>Act;
        cout<<"                     "<<endl;
      }
      switch(Act)
      {
        case '1':
                  cout<<"The gate is locked, "<<endl;
                  cout<<"you turn back.      "<<endl;
                  break;
        case '3':
                  cout<<"You're in the middle"<<endl;
                  cout<<"of town, what will  "<<endl;
                  cout<<"you do?             "<<endl;
                  cin>>Act2;
                  break;
      }
      switch(Act2)  
      {
        case '2':
                  cout<<"There is no reason "<<endl;
                  cout<<"to go back now.    "<<endl;
                  break;
        case '5':
                  cout<<"You're at the gate."<<endl;
                  cout<<"What will you do?  "<<endl;
                  cout<<"Press Q to quit.   "<<endl;
                  cin>>Quit;
                  break;
      }
      return 0;
    }
    Well, anyways, Why won't this code work? It compiles and theres no bugs, and it runs, but when I enter 1, 2, 3, or 5 as a command, it will loop back and re-run the program. This code should work, shouldn't it?
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    132
    take that first part (where you display the ISO logo and the welcome/enter name section) out of your while loop, this is why the program is starting over

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 12:47 PM
  2. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 11:04 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-30-2007, 12:16 AM
  4. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 04:54 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 02:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21