(C++) bizzare autokill

This is a discussion on (C++) bizzare autokill within the Game Programming forums, part of the General Programming Boards category; Alright, the damage is 2, the hp is 5, yet one hit kills: Code: battle() { time_t seconds; time(&seconds); srand((unsigned ...

  1. #1
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110

    (C++) bizzare autokill

    Alright, the damage is 2, the hp is 5, yet one hit kills:
    Code:
    battle() {
    
    time_t seconds;
    
    time(&seconds);
    
    srand((unsigned int) seconds);
    
    int monhp;
    int monmaxhp;
    char mon[20];
    int monid;
    char command[10];
    int monmp;
    int monsp;
    int monatk;
    int dmg;
    
    monid = rand()%10;
    
    strcpy(mon,"Zerra");
    monhp = 5;
    monatk = 1;
    dmg = 2;
    
    monmaxhp = monhp;
    while (monhp >= 1) {
    
    cout << mon << "\nHP: " << monhp <<"/"<< monmaxhp;
    cout << "What will you do?\n: ";
    cin >> command;
    
    if (strcmp(command,"attack") == 0) {
    monhp = monmp - dmg;
    cout << "You dealt " << dmg << " points of dammage.\n";
    strcpy(command,"new");
    }
    
    
    
    cout << "The "<<mon<<" has died";
    } // end loop
    return 0;
    } //end function protocol
    This isn't the final part of the script, I test often so I don't cp my errors

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    monhp = monmp - dmg;
    should be
    monhp -= dmg;

    monmp isn't even initialized. I don't know what it is.

    edit:
    That's not the only problem. Take a *close* look at what code is inside your while loop. Some indentation might help.
    Away.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    cout << "The "<<mon<<" has died";
    } // end loop
    It outputs 'The monster has died' at the end of every loop.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    heh, ok, I typoed the monhp part, so that would indicate my problem. However, I don't really like the indentation, I find it to encourage the horizontal scrollbar.

    and X, I know that, that was the plan

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Nope, that wasn't your plan... Your loop is displaying that the monster has died when it's hp is >= 1. You want to move the "it died" statement to outside of the loop, so that the code won't executed until the monster died. Like this:

    Code:
    } //end loop
    cout << "the "<<mon<<"died";
    Oh, a note on indentation - it's absolutely necessary to be able to read code easily, as you'll see when you get to larger programs. Try changing the indentation size in your editor if it's making you scroll a lot. (in MSVC it can be changed by right clicking on your code and selecting properties)
    Away.

  6. #6
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    I just noticed that, and as for indentation, I use windows note-pad as an editor, and turbo c++ for dos as a compiler, I seriously don't care about it right now, its my code, I comment it, I can read it easily.

  7. #7
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    how could indentation allow me to see my typo better?
    and yea, I've moved it out of the loop

    As for getting an editor, I've tried but the sites you sugest for editors only give annoying lists of files that aren't labeled very well
    still though, I can read my own code, and I'd use the editor in my compiler if it didn't overwrite the next letter, (compiler does indent).

  8. #8
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Hit insert to stop it from overwriting

    Indentation would have helped you see that your code which should have been outside the loop was, in fact, inside the loop. Trust us. We do this all the time.
    Away.

  9. #9
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    it also became apparent when tested
    but what do you guys sugest for editing programs,I checked the sites you sugest, but they haven't labeled the files very well

    Why won't my comiler accept this (manual says it accepts do whiles..)

    Code:
    do while ((mondmg > monatkh) || (mondmg < monatkl)) 
    mondmg = rand();
    Gives an error that the do statement must have a while...

  10. #10
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Originally posted by Dark Nemesis
    it also became apparent when tested
    but what do you guys sugest for editing programs,I checked the sites you sugest, but they haven't labeled the files very well

    Why won't my comiler accept this (manual says it accepts do whiles..)

    Code:
    do while ((mondmg > monatkh) || (mondmg < monatkl)) 
    mondmg = rand();
    Gives an error that the do statement must have a while...
    1) lol
    2)
    3) because you're doing it incorrectly
    Code:
    do
    {
         mondmg = rand();
    } while ((mondmg > monatkh) || (mondmg < monatkl));
    I think you could benefit from checking out these and more specifically, this
    Away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  2. Bizzare: Function Returning Junk
    By mangoMan in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2004, 01:58 PM

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