Thread: my do-while loop won't exit

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    36

    my do-while loop won't exit

    I'm trying to make a very basic text-based pokemon RPG type battle program for fun, but for right now my do-while loop condition isn't working. Do I have to add an incrementer or decrementer at the end?

    Code:
        do {
            cout << "1. Scratch" << endl;
            cout << "2. Fly" << endl;
            cout << "3. Flamethrower" << endl;
            cout << "4. Wing Attack" << endl << endl;
    
            cout << "Attack number -> ";
            cin >> move;
    
            switch (move)
            {
                case 1:
    
                cout << "Your " << pk1 << " does scratch!" << endl;
                damage = ((((2 * level / 5 + 2) * e_atk * scratch / def) / 50) + 2) * stab * weak * randnum / 100;
                e_hp = e_hp - damage;
                cout << pk2 << " is inflicted with " << damage << " damage!" << endl;
                cout << pk2 << " has " << e_hp << " left!" << endl << endl;
                break;
            }
    
        } while ( (hp > 0) || (e_hp > 0) );

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps because your while loop basically says
    while ( i'm alive || he's alive )

    This will be true all the time, unless both manage a kill in the same strike.

    Perhaps && instead, if you want to exit as soon as one is dead.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    36
    Thanks, I had my logic mixed up with AND and OR there with the while loops. Gotta remember AND comes to false if none or only one of the inputs are true, and OR comes to false if neither inputs are true.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Hmm, these attacks remind me of something... is it a clone of Pokémon?
    Devoted my life to programming...

  5. #5
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Quote Originally Posted by GReaper View Post
    Hmm, these attacks remind me of something... is it a clone of Pokémon?
    My interpretations of the variable pk1 and the S.T.A.B. (Same Type Attack Bonus) multiplier tells me yes. I used a Pokémon nerd >.<
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why it can not exit the while loop
    By letmoon in forum C Programming
    Replies: 3
    Last Post: 10-30-2008, 09:59 AM
  2. Can't exit a do/while loop - please help
    By Vireyda in forum C++ Programming
    Replies: 11
    Last Post: 08-08-2005, 06:46 AM
  3. Cannot exit 'while' loop
    By s_ny33 in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2005, 04:59 PM
  4. loop-exit problem
    By duvernais28 in forum C Programming
    Replies: 4
    Last Post: 02-01-2005, 08:26 PM
  5. Can't exit this loop
    By Goalie35 in forum C Programming
    Replies: 4
    Last Post: 10-11-2001, 01:39 PM