Thread: My program is exiting the while loop.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    14

    Unhappy My program is exiting the while loop.

    Hello, everyone, I am creating a text based RPG. The problem I have is that it is exiting the while loop when the condition is still true. The game is going to have 17 boss enemies. It is supposed to exit the while loop when the 17th boss is defeated (and the value is changed to 0.)

    Code:
    //A simple text based rpg designed to be a fun and lighthearted game.
    
    
    #include <iostream>
    using namespace std;
    
    
    int main(void)
    {
    	struct cractr {
    		int dmg;
    		int spd;
    		int hp;
    		int regen;
    		int intel;
    		int res;
    		int exp;
    		int head;
    		int arm;
    		int leg;
    		int heart; };
    		cractr player = { 1, 1, 15, 1, 1, 0, 0, 5, 5, 5, 5 };
    		cractr cat = { 1, 6, 1, 1, 4, 8, 0, 3, 3, 3, 3 };
    
    
    int choice = 0, fish = 0, blood = 0, carrot = 0, gold = 0, done = 0;
    int boss1 = 0, boss2 = 1, boss3 = 1, boss4 = 1, boss5 = 1, boss6 = 1, boss7 = 1, boss8 = 1, boss9 = 1, boss10 = 1, boss11 = 1, boss12 = 1, boss13 = 1, boss14 = 1, boss15 = 1, boss16 = 1, boss17 = 1;
    unsigned char i = 0, ii = 0, iii = 0, iv = 0, v = 0, vi = 0, vii = 0, viii = 0, ix = 0;
    cout << "You have been brought into existence as a mortal...\nbut you can leave it a hero,\nif you would only try...";
    cin.get();
    cout << "\nYou wish to fight the demons, I see.\nWell, you're going to need some bait first!\nHere's a fishing rod;\nit will be all you need to get started off.\n";
    cin.get();
    while (boss17 = 1) {
    cout << "What will you do today?\nFish(1) Hunt(2) Move on(3)\n";
    cin >> choice;
    if (choice == 1) {
    	cout << "You obtain a fish.\n"; fish++; }
    else if (choice == 2)
    {
    	cout << "Ah, so you wish to hunt for\nthe demons and become stronger.\nWhich demon shall you fight?\nCat(1)\n";
    	cin >> choice;
    if (choice == 1 && fish > 0) {
    	cout << "You shall battle a cat!\n";
    	i = 213;
    	ii = 94;
        iii = 32;
        iv = 184;
        v = 179;
        vi = 48;
        vii = 95;
        viii = 212;
        ix = 190;
        cout << i << ii << iii << ii << iv << "\n";
        cout << v << vi << iii << vi << v << "\n";
        cout << v << iii << vii << iii << v << "\n";
        cout << viii << vii << vii << vii << ix << "\n";
    		
    							  }
    else if (choice == 1 && fish == 0)
    	cout << "You lack the necessary type of bait.\n";
    }
    if (choice == 3)
    	cout <<" You wish to leave your current area?\nThen you must battle a gate demon.\nAre you sure you are ready?\nYes(1) No(2)";
    return 0;
    }
    }
    After I do anything once (such as fishing or trying to hunt) it exits the program. This makes me assume that it is exiting the while loop. I tried putting cin.get(); right after the while loop ends, and it doesn't even read it. I don't put anything in to the cin.get(); command before it is instantly closed.

    To clarify, the problem is, (let's say I choose to fish) it will put out the output and it probably increments fish, but it closes right after as opposed to looping. Also, if I choose to hunt, since I start with 0 fish, it will output that I don't have enough bait to bait out a cat and then instantly close the program.

    What do I do?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    proper formatting of your code will reveal your problem.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    while (boss17 = 1)
    Is = used for comparison?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    no. the correct operator is ==

    also, regardless of what option(s) your user chooses, it will always return 0 on the first time thru the loop.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    14
    Quote Originally Posted by Elkvis View Post
    proper formatting of your code will reveal your problem.
    Sorry, I don't know the proper way to properly format my code.
    Quote Originally Posted by rags_to_riches View Post
    Code:
    while (boss17 = 1)
    Is = used for comparison?
    *facepalm* No... I just want to point out, before I fix my poorly formatted code, that all of the == operators in their now used to be = operators.
    ಥ_ಥ

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by KittenAqua View Post
    Sorry, I don't know the proper way to properly format my code.
    Then it should be an urgent priority to learn.

    Programming without indentation is like fumbling through the dark. You probably wont find what you're looking for, and you might hurt yourself in the process.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-08-2011, 10:24 AM
  2. Exiting Endless Loop
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 11-23-2008, 06:02 PM
  3. Program exiting
    By sql.scripter in forum C Programming
    Replies: 9
    Last Post: 01-28-2006, 08:51 PM
  4. Exiting a program
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 07-14-2004, 08:51 PM
  5. exiting program
    By spike232 in forum C# Programming
    Replies: 4
    Last Post: 05-25-2002, 08:18 PM