Broken While Loop

This is a discussion on Broken While Loop within the C++ Programming forums, part of the General Programming Boards category; i have a broken while loop on my hands. I know how to code the arguments but the arguments take ...

  1. #1
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38

    Broken While Loop

    i have a broken while loop on my hands. I know how to code the arguments but the arguments take one loop more than they should.

    my code looks something like this...

    Code:
    while((monsterhp>0)&&(playerhp>0))
    {
      //player attacks using magic or item
      monsterhp-=damage;
      //monster now attacks
      playerhp-=damage;
      //yes the damage int does change since last time i used it.
    }
    when i run the program it takes one extra turn to pick up that one or the other is dead.

    if you need morecoding just say so and ill try to be more in depth.
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Q: It takes one extra while loop iteration to reach your resolution? A: change while((monsterhp>0)&&(playerhp>0)) to while((monsterhp>1)&&(playerhp>1))



    C++ == easy
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    A: change while((monsterhp>0)&&(playerhp>0)) to while((monsterhp>1)&&(playerhp>1))
    Unless I'm wrong that will end the loop prematurely. That will end the loop when either the monster or the player reaches 1 or lower not 0 or lower.
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Give us some more code. The code that you posted should work, so the problem is elsewhere.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    the only other code really that is after that is as follows.

    Code:
    //after while loop
    
    if(monsterhp>playerhp)
    cout<<"I'm sorry you have lost.";
    else
    cout<<"Congrats you won!";
    thats pretty much all i have so i dont know why it isnt working.
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    All the code you've posted has worked. The problem is in code that you haven't posted.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    thats wat im tryin to tell you the only othercode i have are functions that deal damage to the monster or the player.

    and i know those are rite but ill try to post them later today to prove to you doubters.
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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