Bug in my barely-started text-based game program

This is a discussion on Bug in my barely-started text-based game program within the C++ Programming forums, part of the General Programming Boards category; Here's the code, explanation of what's wrong at the moment at the bottom. Code: #include <iostream> #include <fstream> #include <string> ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    91

    Help needed with a text-based game program

    Here's the code, explanation of what's wrong at the moment at the bottom.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main () {
        string select;
        
        cout << "Welcome to Link's Grand Quest!\n";
        cout << "Select an option:" << endl << endl;
        cout << "Create Character (press c)\n";
        cout << "Load Character (press l)\n";
        cout << "Creator (press r)\n";
        cout << "Exit (press e)\n";
        cin.ignore(80, '\n');
        cin >> select;
        if (select == "c" || select == "C") {
                   cout << "I'm sorry, this feature is not available yet";
        }
        else if (select == "l" || select == "L") {
             cout << "I'm sorry, this feature is not available yet";
        }
        else if (select == "r" || select == "R") {
             cout << "I'm sorry, this feature is not available yet";
        }
        else if (select == "e" || select == "E") {
        }
        else {
    //         while (select != "c" || select != "C" || select != "l" || select != "L" || select != "r" || select != "R" || select != "e" || select != "E") {
    //               cout << "I'm sorry, you did not select an option. Please select from below:\n\n";
    //               cout << "Create Character (press c)\n";
    //               cout << "Load Character (press l)\n";
    //               cout << "Creator (press r)\n";
    //               cout << "Exit (press e)\n";
    //               cin >> select;
    //               if (select == "c" || select == "C") {
    //                          cout << "I'm sorry, this feature is not available yet";
    //               }
    //               else if (select == "l" || select == "L") {
    //                    cout << "I'm sorry, this feature is not available yet";
    //               }
    //               else if (select == "r" || select == "R") {
    //                    cout << "I'm sorry, this feature is not available yet";
    //               }
    //               else if (select == "e" || select == "E") {
    //                    cout << "I'm sorry, this feature is not available yet";
    //               }
    //               else {
    //               }
    //         }
        }
        cin.get();
    }
    I commented out the parts I don't want running in the program for now, and the bug still exists, so I know the bug resides somewhere in the first cin >> select;'s area. When I type "c", it doesn't output a single thing, and you have to input something else (although enters don't seem to work) to get the program to close after that point. No matter what I use on cin >> select;, it does this. Any help?
    Last edited by linkofazeroth; 08-27-2005 at 11:51 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Scratch that, it requires two of typing the same selection to get it to output, but the "Create Character option" doesn't do anything but close the program.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Please don't make multiple consecutive posts. It is often considered spamming or thread-bumping, both of which are specifically prohibited in the forum rules. Instead, please use the 'edit' feature in the bottom-right-hand corner of each post.

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    aside from your really uneasy on the eye choice of
    bracket positioning, your problem is location
    of cin.ignore() , here your revised copy

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main () 
    {
    	string select;
    	   
    	cout << "Welcome to Link's Grand Quest!\n";
    	cout << "Select an option:" << endl << endl;
    	cout << "Create Character (press c)\n";
    	cout << "Load Character (press l)\n";
    	cout << "Creator (press r)\n";
    	cout << "Exit (press e)\n";
    	cin >> select;
    	cin.ignore(80, '\n');
    	
    	if (select == "c" || select == "C") 
    	{
    		cout << "I'm sorry, this feature is not available yet";
    	}
    	else if (select == "l" || select == "L") 
    	{
    		cout << "I'm sorry, this feature is not available yet";
    	}
    	else if (select == "r" || select == "R") 
    	{
    		cout << "I'm sorry, this feature is not available yet";
    	}
    	else if (select == "e" || select == "E") 
    	{
    	}
    	else if(select != "c" || select != "C" || select != "l" || select != "L" || select != "r" || select != "R" || select != "e" || select != "E")
    	{
    		cout << "I'm sorry, you did not select an option. Please select from below:\n\n";
    		cout << "Create Character (press c)\n";
    		cout << "Load Character (press l)\n";
    		cout << "Creator (press r)\n";
    		cout << "Exit (press e)\n";
    		cin >> select;
    		cin.ignore(80, '\n');
    		if (select == "c" || select == "C") 
    		{
    			cout << "I'm sorry, this feature is not available yet";
    		}
    		else if (select == "l" || select == "L") 
    		{
    			cout << "I'm sorry, this feature is not available yet";
    		}
    		else if (select == "r" || select == "R") 
    		{
    			cout << "I'm sorry, this feature is not available yet";
    		}
    		else if (select == "e" || select == "E") 
    		{
    			cout << "I'm sorry, this feature is not available yet";
    		}
    	}
    	cin.get();
          return 0;
    }
    then again you never said what your problem was.
    Last edited by ILoveVectors; 08-27-2005 at 09:38 PM.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Get rid of this.
    Code:
        cin.ignore(80, '\n');
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    I've fixed that, and taken some time to at capabilities to the program, but now there's a minor bug on when I select the Dragon option in create(), then select the Main Menu option in the same function. When I choose to exit (type e) at that point, it repeats the main menu text, and requires me to enter it again to exit. Weird. Here's my updated code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void create();
    
    int main () {
        string select;
        
        cout << "Welcome to Link's Grand Quest!\n";
        cout << "Select an option:" << endl << endl;
        cout << "Create Character (press c)\n";
        cout << "Load Character (press l)\n";
        cout << "Creator (press r)\n";
        cout << "Exit (press e)\n";
        cin >> select;
        if (select == "c" || select == "C")
        {
                   create();
                   main();
        }
        else if (select == "l" || select == "L")
        {
             cout << "I'm sorry, this feature is not available yet.\n\n";
             main();
        }
        else if (select == "r" || select == "R")
        {
             cout << "Creator: linkofazeroth\n\n";
             main();
        }
        else if (select == "e" || select == "E")
        {
             cout << endl;
        }
        else
        {
             while (select != "c" || select != "C" || select != "l" || select != "L" || select != "r" || select != "R" || select != "e" || select != "E")
             {
                   cout << "I'm sorry, you did not select an option. Please select from below:\n\n";
                   cout << "Create Character (press c)\n";
                   cout << "Load Character (press l)\n";
                   cout << "Creator (press r)\n";
                   cout << "Exit (press e)\n";
                   cin >> select;
                   if (select == "c" || select == "C")
                   {
                              create();
                   }
                   else if (select == "l" || select == "L")
                   {
                        cout << "I'm sorry, this feature is not available yet.\n\n";
                        main();
                   }
                   else if (select == "r" || select == "R")
                   {
                        cout << "Creator: linkofazeroth\n\n";
                        main();
                   }
                   else if (select == "e" || select == "E")
                   {
                   }
             }
        }
    }
    
    void create() {
         string rac;
         
         cout << "Which race do you wish to be?\n\n";
         cout << "Dragon (press d)         WARNING! Level 10 must be attained to become a Dragon.\n";
         cout << "Elf (press e)\n";
         cout << "Human (press h)\n";
         cout << "Dwarf (press w)\n";
         cout << "Gnome (press g)\n";
         cout << "Main Menu (press m)\n";
         cin >> rac;
         cout << endl;
         if (rac == "d" || rac == "D")
         {
                 cout << "Dragons are the most powerful race on Couria, and require you to be level 10" << endl << "before being useable.\n";
                 cout << "Dragons have the best vision, and with their specially fit DragonBows, only" << endl << "Elves come close to their mastery of archery.\n";
                 cout << "The claws of a dragon, when properly taken care of, are the hardest substance" << endl << "found anywhere. Only the warhammers of the dwarves are more powerful.\n";
                 cout << "Dragon scales are nearly as hard as dragon claws, and provide defense that is" << endl << "second-to-none.\n";
                 cout << "With the power of flight, Dragons are so agile that only the Gnomes, with their mechanical creations, can keep up.\n\n";
                 cin.get();
                 create();
         }
         else if (rac == "e" || rac == "E")
         {
              cout << "Elves are the smartest race on Couria, being masterful archers and mages.\n";
              cout << "Elves have keen sight, better than a cat's, and thus their archery is second" << endl << "only to the Dragon's.\n";
              cout << "Unfortunately, as Elves are very slim, they're lacking in brute force. The same condition makes them easy prey when off guard.\n";
              cout << "The agility of the Elves are enviable by lesser races, but the Dragons, with" << endl << "their wings, and Gnomes, with their mechanical contraptions, are faster.\n\n";
              cin.get();
              create();
         }
         else if (rac == "m" || rac == "M")
         {
              main();
         }
         else
         {
             main();
         }
    }

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    no need to call main(), it takes you right back to main, when
    it done running. anyways stop saying you have an error.

    start posting your error, the responses will be much
    quicker if you do that.

    also look the code i posted above...
    main() returns an int, correct?
    wonderful so where your return 0; at
    you should have it at the end of your program for good measure.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    I didn't say I had an error, I said I had a bug, and the compiler didn't catch this one, so I can't show you the error report.

    It'd be a good idea to take my code and run it on your end so you can actually see the output. Follow these steps to get to my bug:

    1. Press c.
    2. Press d or e.
    3. Press m.
    4. Press e.

    EDIT: Solved the bug, ILoveVectors was right about the main();'s not needing to be there in create().
    Last edited by linkofazeroth; 08-27-2005 at 11:36 PM.

  9. #9
    Banned
    Join Date
    Jun 2005
    Posts
    594
    yea that what i was saying, that just because the compiler
    doesnt show you the error, doesnt mean you cant
    give us a explination of the error.

    such as what you do and what the program does
    to that response then mention what it should be
    doing.

    im not harrassing you so dont get upset im just letting
    you know the best course of action once you need
    help agian.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    I did say what the problem was, including at the very beginning. For the current (sorta) problem, I said "when I select the Dragon option in create(), then select the Main Menu option in the same function. When I choose to exit at that point, it repeats the main menu text, and requires me to enter it again to exit"

  11. #11
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Quote Originally Posted by ILoveVectors
    main() returns an int, correct?
    wonderful so where your return 0; at
    you should have it at the end of your program for good measure.
    Actually, main implicitly returns 0 if you don't specify a return value. And I'm pretty sure recursively calling main() is undefined (so don't do it!)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Ok, I just finished the function that's supposed to load the data from separate files and save them into separate variables, but when I tested the contents of the variables after loading them from the files, all that came out was a 0 for each variable. THe variables are global; could that be the cause? Here's some of my code:

    load()
    Code:
    void load() {
         string nam;
         string sel;
         
         cout << "Which character do you want to play as?\n\n";
    
    //...
    
         ifstream enam("enam.txt"); //Elf name
    
    //...
    
         if ( !enam.is_open() ) //Check for existence.
         {
         }
         else
         {
             enam >> nam;
             cout << nam << " the Elf (press e)\n";
         }
    
    //...
    
         else if (sel == "e" && enam.is_open())
         {
              ofstream char2("elev.txt"); //Start at char2 for consistency with createdata().
              ofstream char3("eatt.txt");
              ofstream char4("edef.txt");
              ofstream char5("eagi.txt");
              ofstream char6("emon.txt");
              ofstream char7("eexp.txt");
              char2 << lev; //Input into global variables
              char3 << att;
              char4 << def;
              char5 << agi;
              char6 << mon;
              char7 << exp;
              dnam.close(); //Close the name files
              enam.close();
              hnam.close();
              wnam.close();
              gnam.close();
              char2.close(); //Close the statistics files
              char3.close();
              char4.close();
              char5.close();
              char6.close();
              char7.close();
         }
    
    //...
    
         cout << lev << " " << att << " " << def << " " << agi << " " << mon << " " << exp << ".";
         cin.ignore(80,'\n');
         cin.get();
    }
    And here's createdata()
    Code:
    void createdata(string rac) {
    
    //...
    
         else if (rac == "e" || rac == "E")
         {
                 string enam;
                 
                 ofstream char1("enam.txt"); //Name
                 ofstream char2("elev.txt"); //Level
                 ofstream char3("eatt.txt"); //Attack
                 ofstream char4("edef.txt"); //Defense
                 ofstream char5("eagi.txt"); //Agility
                 ofstream char6("emon.txt"); //Money
                 ofstream char7("eexp.txt"); //Experience
                 cout << endl << "What is your character's name? ";
                 cin >> enam;
                 char1 << enam;
                 char2 << "1"; //Starting level
                 char3 << "50"; //Starting attack
                 char4 << "40"; //Starting defense
                 char5 << "65"; //Starting agility
                 char6 << "0"; //Starting money
                 char7 << "1"; //Starting experience
                 char1.close();
                 char2.close();
                 char3.close();
                 char4.close();
                 char5.close();
                 char6.close();
                 char7.close();
         }
    //...
    EDIT: Also, the files themselves contain 0, so it's not load() that's the problem. I think it's just how I'm sending the numbers to the files.
    Last edited by linkofazeroth; 08-28-2005 at 02:18 PM.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    I figured it out, the problem was I was using the wrong type of fstream.

    The problem this time is that I implemented my variables into a struct, but the compiler says "stats" does not name a type". Here's the format of my struct:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    void create();
    //Function to display race information. Leads to createdata().
    void createdata(string rac);
    //Function to create date files for use in the game.
    void load();
    //Function to load a character's data files and start the game.
    void game();
    //Storyline function. Not much else to say.
    void battle();
    //Battling function, gives exp and money if you win.
    void monst();
    //Randomly generate monsters for battle() according to level.
    void attack();
    //Attack the opponent generated in monst().
    void defend();
    //Increase defense temporarily.
    
    struct stats
    {
           string nam;
           int lev;
           int att;
           int def;
           int agi;
           int mon;
           int sta;
           float exp;
    }
    
    int main () {
        string select;
        stats player;
        stats enemy;
    
    //...

  14. #14
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    struct stats
    {
           string nam;
           int lev;
           int att;
           int def;
           int agi;
           int mon;
           int sta;
           float exp;
    };
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  15. #15
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    ...Oh. Thanks.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 05-30-2007, 06:47 PM
  2. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  3. making a text based game
    By MisterSako in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2004, 01:20 PM
  4. Creating Shell based game..Few questions
    By Saintdog in forum Game Programming
    Replies: 1
    Last Post: 12-01-2004, 08:14 PM
  5. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 06:13 PM

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