Thread: Test script error

  1. #1
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48

    Test script error

    i was making a script to test some operations, and i can't seem to run it, because i get an error on line 21. could you guys tell me what i'm doing wrong.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    //dex = dexterity str = strength ac = the armor of the goblin dmg = damage
    int main()
    {
        int dex, str, ac;
        cout << "what is your dex?(1-20): ";  //lets the player choose the dex
        cin >> dex;
        cout << "\nwhat is your str?(1-20): ";  //lets the player choose the str
        cin >> str;
        ac = rand(20);
        if(dex && str<20);  //checks to make sure the player isnt cheating
        {
            if(dex>ac); //checks to see if the player's dex is high enough to hit the goblin
            {
                 float HtoH = .5; //HtoH = hand to hand skill
                 float dmg = dex * str;
                 dmg = dmg * HtoH;
                 cout << "you hit the goblin for " << dmg << endl; //tells the player how much he hit for
            }
            else 
            {
                 cout<< "you did not hit" endl;  //if the players dex was too low
            }
        else
        {
            cout<< "not above 20!" endl;  //if the player tried to cheat
        }
    return 0;
    }
    i bet it totaly obvious, but i can't seem to find it.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Code:
    if(dex>ac);     // Get rid of semicolon

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if(dex && str<20);
    Both your if() have a stray ;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    hah, wow thanks guys!

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: Test script error

    Originally posted by Aalmaron
    Code:
    //dex = dexterity str = strength ac = the armor of the goblin 
    [...]
    if(dex && str<20);  //checks to make sure the player isnt cheating
    Don't use short variable names that makes to type comments like the one above. Use obvious names like goblinArmour etc.

    And that if-statment should look like this (I imagine):
    Code:
    if (dex<=20 && str<=20)
    Also make sure each { has a matching }.
    Last edited by Sang-drax; 01-07-2004 at 09:25 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    yeah i was thinking about changing my variable names, shorthand sucks.

    my old if statement worked, but you're right, it isn't pretty like yours

    i've been messing with it alot though, it barely looks like that anymore. if you want, i'll post it tomorrow after i fix the random goblin attack.

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Originally posted by Aalmaron
    my old if statement worked, but you're right, it isn't pretty like yours
    Do you mean if (dex && str < 20) worked? If so, I doubt it worked in all cases. Users can put in whatever positive number they want for the dexterity and the if statement would evaluate to true. If I gave 5000 as my dexterity and 10 as my strength, then the if statement would be if ((5000) && (10<20)). Since 5000 is non-zero, it evaluates to true and true, which is true, but not what you want.

  8. #8
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    wow, i didnt test it like that, you're right.

    i had changed it to before
    Code:
    if(dex && str <= 20)
    ok now i changed it to
    Code:
    if(dex <= 20 && str <= 20 && dex >= 0 && str >= 0)


    also, i'm having form problems with my damage numbers.


    Code:
    float phealth = 100;
    int dex, str, PlayerArmor; 
    float dmg;
    float HandtoHand = .5;
    int weaponbonus = 5; // weapon bonus, only 5 because its his fists
    those are my player variables. playerarmor is randomed, but that doesnt matter here.

    Code:
                       if(dex > GoblinArmor) //checks to see if the player's dex is high enough to hit the goblin
                         {
                         dmg = dex * str;
                         dmg = dmg * HandtoHand;
                         dmg = dmg * AttackRandom;
                         dmg = dmg + weaponbonus;
                         cout << "you hit the goblin for " << dmg << " damage" << endl; //tells the player how much he hit for
                         ghealth = ghealth - dmg;
                         cout << "the goblin has " << ghealth << " Hit points left" << endl;
                         }//closing player hit if
                       else
                         {
                         cout << "you did not hit" << endl;  //if the players dex was too low
                         }//closing player hit else
    thats my code for attack. but when i play it, it deals 5 damage every time, which is the weapon bonus. when i take the add weapon bonus line out, then i do normal damage.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  3. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM