Thread: Need help understanding games in C++

  1. #1
    bpc8891
    Guest

    Angry Need help understanding games in C++

    I have been trying to write a simple text adventure program using if statements and functions. When I run the program it always reruns the program from the start. Also one of the commands should be exit to exit the game how is that done.

    This is what I have but it is not working.
    char exit(char)
    {
    cout << "press any key to continue....\n";
    return 0;
    }


    cin >> direction;
    if (direction = "r" || "R")
    {
    net();
    }
    if (direction = (char) exit)
    {
    char exit(char);
    }

    the other problem in main - it loops back through

    void main ()
    {
    instructions();
    startGame();
    net();

    }

    I'm very new at this and very confused. If anyone has any suggestions it would sure help.

    Thanks.

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Code:
    int gameexit()
    {
    char var;
    cout << "press any key to continue....\n";
    cin>>var;
    return 0;
    }
    Code:
    cin >> direction;
    if (!(strcmp(direction,"r"))  || !(strcmp(direction,"R")))
    {
    net();
    }
    if (strcmp(direction,"e"))
    {
    gameexit();
    }
    Well I have no idea what it is you are trying to achieve with any of the code you have posted. You need to post all your code for it to make sense. But I have made some modifications to what is there that I think may help. Hopefully what is there is correct, been a while since I used some of these things. For strcmp to work #include <string.h>

    Also, I changed exit to gameexit as there is already a function exit(0) which will quit your program.

    Hope this helps.
    Last edited by minesweeper; 12-08-2002 at 05:31 PM.

  3. #3
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Another thing........void main().


    Change it to:

    Code:
    int main()
    {
    
    //program
    
    return 0;
    }

  4. #4
    bpc8891
    Guest
    Here is what I had & also your improvements. Thanks for your help.


    # include <iostream.h>
    # include <string.h>

    void instructions();
    void startGame();
    //char exit(char);
    int gameexit();
    void help(char);
    void net();

    //*********************
    // basic instructions *
    //*********************


    void instructions()
    {
    cout << "Use u, d, r, and l to move the character.\n";
    cout << "Type in exit to quit game or inventory to get a\n";
    cout << "list of items you are carrying.\n\n\n\n";

    }




    //****************
    // start of game *
    //****************


    void startGame()
    {
    char direction;
    cout << "You are 10 years old and become lost while\n";
    cout << "on a hiking trip with your family.\n";
    cout << "The only direction you can move is right.\n";
    cin >> direction;

    if (!(strcmp(direction,"r")) || !(strcmp(direction,"R")))
    {
    net();
    }
    if (strcmp(direction,"exit"))
    {
    gameexit();
    }

    if (strcmp(direction, "help"))
    {
    instructions();
    }

    else
    {
    cout << "Invalid direction. Try again./n";
    }

    }


    //******************************
    //able to retrieve a net *
    //from a tree. This is *
    //important as need to catch *
    //fish later on. *
    //******************************

    void net()
    {
    char direction;
    cout<< "You are further off the trail and deeper into the forest.\n";
    cout<< "There is a net hanging from a tree.\n";
    cout<< "You can move to the left or down.\n";
    cin >> direction;

    if (!(strcmp(direction,"r")) || !(strcmp(direction,"R")))
    {
    net();
    }
    if (strcmp(direction,"exit"))
    {
    gameexit();
    }

    if (strcmp(direction, "help"))
    {
    instructions();
    }

    else
    {
    cout << "Invalid direction. Try again./n";
    }

    }







    void main ()
    {
    instructions();
    startGame();
    net();

    }

    //char exit(char)
    //{
    // cout << "press any key to continue....\n";
    // return 0;
    //}

    int gameexit()
    {
    char var;
    cout << "press any key to continue....\n";
    cin>>var;
    return 0;
    }


    void help(char)
    {
    instructions();

    }

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    cout << "Press a key to continue..."
    cin >> var;

    this will wait until the user hits enter, you can use getch() or kbhit() insteat you can capture the key press without pressing enter.
    none...

  6. #6
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Code:
    void main ()
    {
    instructions();
    startGame();
    net();
    
    }
    God dammit man! int main!! If you take one thing away from cprog.com, let it be int main and not void!! ahhh!!!

    :: runs away screaming, holding his head and shouting ::

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. When done right, PC games are amazing
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-13-2008, 05:32 PM
  2. Violent video games?
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 58
    Last Post: 04-26-2006, 01:43 PM
  3. Replies: 8
    Last Post: 09-09-2005, 12:34 AM
  4. Video Games Industry. 5 years left.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 12-10-2002, 10:52 PM