if statement problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: if statement problem

  1. #1
    Join Date
    Oct 2011
    Posts
    0

    Exclamation if statement problem

    Hey guys, im writing a simple text game in C++, im using " if " statements, however, even if a number is equal to the if statement it keeps runs all the other things in the program.

    This is my code, look for mistakes.

    #include <iostream>
    using namespace std;
    int main ()

    {
    cout << " Oh hai der " << endl;
    int name;
    int health1;
    int health2;
    int health3;
    int health4;
    int mana1;
    int mana2;
    int mana3;
    int mana4;
    int score1;
    int score2;
    int score3;
    int score4;
    int attack1;
    int attack2;
    int attack3;
    int attack4;
    int action1;
    int action2;
    int action3;
    int action4;
    int ehealth1;
    int ehealth2;
    int ehealth3;

    health1 = 100;

    cout << " Whats is your desired character name? " << endl;
    cin >> name;
    cout << " Welcome to Sams first game programmed in C++ " << name << endl;
    cout << " Your mana is 100 " << endl;
    cout << " Your health is 100 " << endl;
    cout << " Your score is 0 " << endl;

    cout << " I wild rabbit appears, would you like to attack, or run away? " << endl;
    cout << " 1 is attack, 2 is run away " << endl;
    cout << " What would you like to do " << endl;
    cin >> action1;
    if (action1 == 2);
    cout << " You run away " << endl;
    if ( action1 == 1 );
    cout << " You have chosen to attack the rabbit. " << endl;
    cout << " Press 1 to swipe the enemy with your sword, 2 to block an attack, or 3 to use your special ability " << endl;


    if ( action2 ==1 );
    cout << " You swipe the ememy, hitting it for 5 " << endl;
    health1 = (100 - 5);
    cout << " Your health is now " << health1 << endl;
    cout << " You used up 2 mana " << endl;
    mana1 = ( 100 - 2 );
    cout << " Your mana is now " << mana1 << endl;
    ehealth1 =( ehealth1 - 5 );
    cout << " The health of your enemy is now " << ehealth1 << endl;
    if ( action2 ==2 );
    mana1 = ( mana1 - 2 );
    cout << "You chose to block an attack" << endl;
    cout << " Your health is 100 " << endl;
    cout << " You use 2 mana, your mana is now " << mana1 << endl;
    ehealth1 = ( ehealth1 - 0 );
    cout << " The health of your enemy is now " << ehealth1 << endl;
    if ( action2 ==3);
    cout << "You chose to use your special ability " << endl;
    cout << " This hit the enemy for a 10 " << endl;
    ehealth1 = ( ehealth1 - 10 );
    cout << " The enemys health is now " << ehealth1 << endl;













    system("pause");
    return 0;
    }

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: if statement problem

    Code:
    if (action1 == 2);
    A if-statement is not ended by a ;

    Code:
    if (a == b)
    {
    ....
    some code
    ....
    }

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