Beginner/consoleapp/ifelseproblems/Please help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Beginner/consoleapp/ifelseproblems/Please help

  1. #1
    Join Date
    May 2011
    Posts
    2

    Beginner/consoleapp/ifelseproblems/Please help

    Hi, first post. Im working on a simple adventure game, and im sure you will all say spaghetti code lol. BUT, I have a problem with my program. In this section of the code:
    *********code*********
    cout << " Type left to feel around on your left, and right to feel around on your right " << endl;
    cin >> bm;
    {
    if (bm == "Left" || "left")
    {
    cout << " You find a jaged peice of glass, could be useful " << endl;
    cout << endl;
    cout << " ^^GLASS ADDED TO INVENTORY^^ " << endl; }
    else
    cout << "Invalid input " << endl;


    if ( bm == "Right" || "right" )
    {
    cout << " You find an old 2x4, perfect for a makeshift crutch! " << endl;
    cout << endl;
    cout << " ^^CRUTCH ADDED TO INVENTORY^^ " << endl;
    }

    else
    cout << " Invalid input " << endl;

    }
    *******endcode*******
    It accepts the left and right if's, but if the user types something else then it displays the output from both the left and right options.

    Here is the complete source code so far. Aside from this problem if you want to give me any tips on how to improve my code please do so, i'd really appreciate it.


    // a simple game
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <iomanip>
    #include <istream>



    using namespace std;

    int main()
    {
    string mystr; //declaring variables
    std::string nameevil;
    string name;
    char tf;
    string bm;

    cout << " <<<>>> DEATH BRINGER 49 <<<>>> " << endl; //Title screen
    cout << " ****************************** " << endl;
    cout << " A text based adventure game by Nicholas jeffs " << endl;
    cout << " ****************************** " << endl;
    cout << " ****************************** " << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << " Every hero needs a name! Enter your name to start the game. " << endl;
    cout << " ******************************* " << endl;
    getline (cin, name) ; // assigns value to string name
    cout << " Alright " << name << " are you prepared to start the game? (type y or n) " << endl;
    cin >> tf;
    if (tf == 'y' || tf == 'Y' )
    {
    cout << " Remember, keep your wits about you and youll be fine! (PRESS ENTER) " << endl;
    cin.ignore();
    cin.get();
    cout << string(50, '\n'); // gives illusion of clearing screen by going down 50 lines


    }
    else
    {
    cout << " Too afraid? Come back when you have courage!(Press enter to exit) " << endl;
    cin.ignore();
    cin.get();
    return 0;
    }
    cin.get();
    cout << string(50, '\n'); // gives illusion of clearing screen by going down 50 lines

    cout << " ------------- " << endl;
    cout << " ------------- " << endl;
    cout << " ------------- " << endl;
    cout << " ( You are surrounded by darkness ) " << endl;
    cout << " ( You hear a sinister voice, but cant pinpoint its location ) " << endl;
    cout << " ------------- " << endl;
    cout << " ------------- " << endl;
    cout << " *Hello " << name << " ....... " << endl;
    cout << " *Yes we know your name " << endl;
    cout << " *Yes, as you know ours..... tell me, what is our name in your tounge? " << endl;
    cin >> nameevil ;

    cout << endl;
    cout << endl;
    cout << " *Ah, " << nameevil << " is it ? " << endl;
    cout << " *Well then..... It is us, " << nameevil << ". We decided to play some games! " << endl;
    cout << " *Just like we used to in your dreams, " << endl;
    cout << " *dont pretend you could ever forget, ever... " << endl;
    cout << " *Ill be dropping you into my inter dimensional death trap." << endl;
    cout << " *You will have to find a way out, or die in the maze.. " << endl;
    cout << " *The choice is yours, good luck (Snicker snicker ) " << endl;
    cout << " * Whoops, almost forgot. Any message from me will be preceded with a * " << endl;
    cout << " * Ill be keeping an eye on you, bon voyage! (PRESS ENTER) " << endl;
    cin.ignore();
    cin.get();
    cout << string (50, '\n');

    cout << " Chapter one: Immobilized " << endl;
    cout << " ****************************** " << endl;
    cout << " ------------- " << endl;
    cout << " ------------- " << endl;
    cout << " ------------- " << endl;
    cout << " ( You feel the ground dissapear beneath your feet and begin to plumit rapidly ) " << endl;
    cout << " ( Above is the harsh kackling of " << nameevil << ", below an unknown fate ) " << endl;
    cout << " ------------- " << endl;
    cout << " ------------- " << endl;
    cout << "." << endl;
    cout << " ." << endl;
    cout << " . NOOOOOOOOOOOOO..." << endl;
    cout << " . OOOOOOOO" << endl;
    cout << " .AHHHHH" << endl;
    cout << " .HHH...... " << endl;
    cout << " . " << endl;
    cout << " . " << endl;
    cout << " . " << endl;
    cout << ". *THUD" << endl;
    cout << " PRESS ENTER TO WAKE UP!!!! " << endl;
    cin.ignore();
    cin.get();
    cout << " ------------- " << endl;
    cout << " ------------- " << endl;
    cout << " ------------- " << endl;
    cout << " ( You awake hours later in a pitch black room. The only features you ) " << endl;
    cout << " ( can make out are the stench of fetided stagnant water, ) " << endl;
    cout << " ( and the sound of your own screams. Both your legs are broken and you must ) " << endl;
    cout << " ( crawl to survive. Try and search the immediate area for usefull items ) " << endl;
    cout << endl;
    cout << endl;
    tryagain:

    cout << " Type left to feel around on your left, and right to feel around on your right " << endl;
    cin >> bm;
    {
    if (bm == "Left" || "left")
    {
    cout << " You find a jaged peice of glass, could be useful " << endl;
    cout << endl;
    cout << " ^^GLASS ADDED TO INVENTORY^^ " << endl; }
    else
    cout << "Invalid input " << endl;


    if ( bm == "Right" || "right" )
    {
    cout << " You find an old 2x4, perfect for a makeshift crutch! " << endl;
    cout << endl;
    cout << " ^^CRUTCH ADDED TO INVENTORY^^ " << endl;
    }


    else
    cout << " Invalid input " << endl;

    }


    cin.get(); // stops program from automatically closing once run
    return 0;
    }

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,637

    Re: Beginner/consoleapp/ifelseproblems/Please help

    This
    Code:
    if (bm == "Left" || "left")
    is not how it's done. Compare it with this proper statement
    Code:
    if (tf == 'y' || tf == 'Y' )
    Do you see the difference?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  3. #3
    Join Date
    May 2011
    Posts
    2

    Re: Beginner/consoleapp/ifelseproblems/Please help

    Yes i do thank you very much, foolish mistake!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center