Hello. I am working on a game called Kingdom Management. It was working fine until I added in the buildings you can make. Then it started skipping over the building, lawmaking, do nothing, and quit sections. All it could do is research.
Here is the code:

/*******************************************************************************
Kingdom Management the Game v.A-0
Creator: Bryce A Hart (BAH)
Owner:   "     " "    (""")
Title:   Kingdom Management, the Game
Version: Alpha 0.0.0
Notes:   PROOF OF CREATION/OWNERSHIP: DATE CREATED (Shows while hovering mouse over file)
         1/21/2010.

*******************************************************************************/
#include<iostream>
using namespace std;

int main(){
    //This is a game where you manage a kingdom and try to end with the most gold, land, or army.
    //Intro
    cout << "Welcome to Kingdom Management."<<endl;
    cout << "Let's begin."<<endl;
    system("pause");
    //Declare Variables (STATS)
    int selection, selection1, selection2, selection3, selection4, selection5, selection6, selection7; // This holds a selection for a console input (cin).
    int redo = 1; // This is for while loops at selections.
    int gold; // This is the ammount of money the kingdom has.
    int army; // This is the size of the kingdom's army.
    int pop = 10; // This is your population of workers available.
    int glory = 0; //This is your kingdom's "score."
    int turn = 1; //This is what turn you are on.
    //GAME BEGINS
    while (redo == 1){
    cout << "What type of kingdom would you like to have? Here are your selections:"<<endl;
    cout << "(1)Aggressive: \t 20 Gold \t 50 Army"<<endl;
    cout << "(2)Wealthy: \t 50 Gold \t 20 Army"<<endl;    
    cout << "(3)Balance: \t 35 Gold \t 35 Army"<<endl;
    cout << "Type the number in parenthasese next to the one you want: "<<endl;
    cin >> selection1;
    if (selection1 == 1){
         gold = 20;
         army = 50;
         redo = 0;
         }
    else if (selection1 == 2){
         gold = 50;
         army = 25;
         redo = 0;
         }
    else if (selection1 == 3){
         gold = 10;
         army = 10;
         redo = 0;
         }
    else{
         cout << "Wrong Selection."<<endl;
         redo = 1;
         }
    }//ENDS WHILE LOOP
    cout << "Nice selection."<<endl;
    //Declare Variables (BUILDINGS AND RESEARCH)
    //Buildings
    int barracks = 0; //Tells whether or not you have a barracks.
    int town_hall = 0; //Tells whether or not you have a town hall.
    int market = 0; //Tells whether or not you have a market.
    int farm = 0; // Tells whether or not you have a farm.
    //Research
    int lawmaking = 0; //Tells if you have researched lawmaking or not.
    int siege_weapon = 0; //Tells if you have researched seige weapons.
    int agriculture = 0; //Tells if you have researched large-area agriculture.
    int livestock = 0; //Tells if you have researched raising livestock.
    redo = 1;
    while (redo == 1){
          if (army == 0){
             cout << "Game Over. Your army has 0 people."<<endl;
             return 0;
             }
          else if (gold == 0){
               cout << "Game Over. You ran your kingdom out of money and were overthrown."<<endl;
               }
          else {
          if (turn == 1){
             cout << "Population is the ammount of workers you have available."<<endl;
             cout << "Glory is your score. It is based on your actions as king."<<endl;
             }
          else {
          cout << endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
          }
          cout << "TURN "<<turn<<endl;
          cout << "====================="<<endl;
          if (barracks == 1){
             army = army + 5;
             }
          else if (market == 1){
               if (farm == 1){
                  gold = gold + 2;
                  }
               if (livestock == 1){
                  gold = gold + 2;
                  }
               gold = gold + 5;
               }
          else if (pop < 3){
               pop = pop + 1;
               }
          else if (pop > 3, pop < 5){
               pop = pop + 2;
               }
          else if (pop >5, pop < 10){
               pop = pop + 3;
               }
          else if (pop > 10){
               pop = pop + 5;
               }
          cout << "Population: \t" << pop <<endl;
          cout << "Gold: \t \t"<< gold <<endl;
          cout << "Army: \t \t"<< army <<endl;
          cout << "Glory: \t \t" << glory <<endl;
          system("pause");
          cout << "What would you like to do?"<<endl;
          cout << "(1)Research"<<endl<<"(2)Build"<<endl<<"(3)Enact Law (COMING SOON)"<<endl;
          cout << "(4)Nothing"<<endl<<"(5)Quit Game"<<endl;
          cin >> selection;
        if (selection == 1){
             cout << "Here is the research list. Please select one."<<endl;
             cout << "-#-|----NAME---- \t ----COST---- \t ----DESCRIPTION----"<<endl;
             cout << "(1)|Lawmaking \t \t 5G, 0A, 0P \t Step 1 To Enacting Laws"<<endl;
             cout << "(2)|Siege Weapons \t 10G, 3A, 3P \t Better Fighting Power"<<endl;
             cout << "(3)|Agriculture \t 5G, 0A, 5P \t Step 1 To Markets"<<endl;
             cout << "(4)|Livestock \t \t 10G, 0A, 10P \t Better Markets"<<endl;
             cin >> selection3;
             if (selection3 == 1){
                if (lawmaking == 0){
                   gold = gold - 5;
                   lawmaking = 1;
                   cout << "You have researched LAWMAKING."<<endl;
                   turn = turn + 1;
                   }
                else{
                   cout << "You have already researched LAWMAKING."<<endl;
                   }
             }
             else if (selection3 == 2){
                  if (siege_weapon == 0){
                  gold = gold - 10;
                  army = army - 3;
                  pop = pop - 3;
                  siege_weapon = 1;
                  cout << "You have reasearched SIEGE WEAPONS."<<endl;
                  turn = turn + 1;
                  }
                  else {
                  cout << "You have already resarched SIEGE WEAPONS."<<endl;
                  }
             }
             else if (selection3 == 3){
                  if (agriculture == 0){
                  gold = gold - 5;
                  pop = pop - 5;
                  agriculture = 1;
                  cout << "You have reasearched AGRICULTURE."<<endl;
                  turn = turn + 1;
                  }
                  else {
                  cout << "You have already resarched AGRICULTURE."<<endl;
                  }
                  }
             else if (selection3 == 4){
                  if (livestock == 0){
                  gold = gold - 10;
                  pop = pop - 10;
                  livestock = 1;
                  cout << "You have reasearched LIVESTOCK."<<endl;
                  turn = turn + 1;
                  }
                  else {
                  cout << "You have already resarched LIVESTOCK."<<endl;
             redo = 1;
             }
             }
        else if (selection == 2){
           cout << "Here is the building list."<<endl;
           cout << "-#-|----NAME---- \t ----COST---- \t ---------DESCRIPTION---------"<<endl;
           cout << "(1)|Barracks \t 10G, 5A, 5P \t Adds 5 to your army each turn."<<endl;
           cout << "(2)|Town Hall \t 10G, 1A, 5P \t Unlocks Lawmaking."<<endl;
           cout << "(3)|Market \t 5G, 0A, 3P \t Adds 5 gold every turn."<<endl;
           cout << "(4)|Farm \t 5G, 0A, 5P \t Market Upgrade."<<endl;
           cout << "What shall you build?"<<endl;
           cin >> selection5;
               if (selection5 == 1){
                  if (barracks == 0){
                     gold = gold - 10;
                     army = army - 5;
                     pop = pop - 5;
                     barracks = 1;
                     cout << "You have built a BARRACKS."<<endl;
                     turn = turn + 1;
                     redo = 1;
                     }
                     else{
                     cout << "You already have a BARRACKS."<<endl;
                     redo = 1;
                     }
                     }
                  else if (selection5 == 2){
                  if (town_hall == 0){
                     if (lawmaking == 1){
                      town_hall = 1;
                      cout << "You have built a TOWN HALL."<<endl;
                      turn = turn + 1;
                      redo = 1;
                      }
                   else
                       cout << "You must have researched in LAWMAKING to make a TOWN HALL."<<endl;
                       redo = 1;
                       }
                else{
                   cout << "You already have a BARRACKS."<<endl;
                   redo = 1;
                   }
                   }
             else if (selection5 == 3){
                  if (farm == 0){
                  gold = gold - 5;
                  pop = pop - 3;
                  farm = 1;
                  cout << "You have built a MARKET."<<endl;
                  turn = turn + 1;
                  redo = 1;
                  }
                else{
                   cout << "You already have a MARKET."<<endl;
                   redo = 1;
                   }
                   }
             else if (selection5 == 4){
                  if (farm == 0){
                     if (agriculture == 1){
                     gold = gold - 5;
                     pop = pop - 5;
                     farm = 1;
                     cout << "You have built a FARM."<<endl;
                     turn = turn + 1;
                     redo = 1;
                     }
                     else
                     cout << "You need to research AGRICULTURE first."<<endl;
                     redo = 1;
                   }
                else{
                   cout << "You already have a FARM."<<endl;
                   redo = 1;
             }
             }
        else if (selection == 3){
           if (town_hall == 1){
              cout << "LAWMAKING COMING SOON."<<endl;
              turn = turn + 1;
              redo = 1;
              }
              else
              cout << "You must research Lawmaking and build a Town Hall before you can make laws."<<endl;
              }
        else if (selection == 4){
           cout << "You wait out the turn. The people want a working king!"<<endl;
           turn = turn + 1;
           redo = 1;
           }
        else if (selection == 5){
           cout << "Are you sure you want to quit?"<<endl;
           cout << "(1)Quit \t (2)Stay"<<endl;
           cin >> selection4;
             if (selection4 == 1){
                cout << "Goodbye!"<<endl;
                cout << "You ended the game with the following stats:"<<endl;
                cout << "You finished on turn "<<turn<<"."<<endl;
                cout << "Population: \t" << pop <<endl;
                cout << "Gold: \t \t"<< gold <<endl;
                cout << "Army: \t \t"<< army <<endl;
                cout << "Glory: \t \t" << glory <<endl;
                return 0;
                }
             else {
                cout << "Good! Let's continue!"<<endl;
                redo = 1;
                }
          }
          else {
                cout << "Wrong Selection."<<endl;
                redo = 1;
                }
          }
    }
}
}
}

There you go.
I work in the Dev-C++ Environment.

Recommended Answers

All 10 Replies

Although we could debug your code, we charge too much, so you'll have to learn the craft of debugging yourself, as every programmer does/should.
If you follow the instructions listed here, you'll be able to avoid cases like this one in future.
Also your whole program is contained within the main() function, which is not a recommended practice, this case can serve as an example.
Since you didn't apply incremental testing, the bug can really lie everywhere in your program, but most likely of course in the last changes you've made (since the program was not behaving anymore like it should), so my advice would be to remove those last changes again, test your program thoroughly from that point, rethink your logic for these new features, add them again, and test your whole program again to see if everything works as desired.
Also look for parts of code which you could better place into a separate function, this will help you to maintain a more compact main() function.

What's with the comma?

else if (pop > 3, pop < 5){
               pop = pop + 2;
               }
          else if (pop >5, pop < 10){
               pop = pop + 3;
               }

main.cpp:102: warning: left-hand operand of comma has no effect
main.cpp:105: warning: left-hand operand of comma has no effect

Running this code through a beautifier (or doing a better job on the indenting yourself), the posted code might look like this:

/*******************************************************************************
Kingdom Management the Game v.A-0
Creator: Bryce A Hart (BAH)
Owner:   "     " "    (""")
Title:   Kingdom Management, the Game
Version: Alpha 0.0.0
Notes:   PROOF OF CREATION/OWNERSHIP: DATE CREATED (Shows while hovering mouse over file)
         1/21/2010.

*******************************************************************************/
#include<iostream>
using namespace std;

int main()
{
   //This is a game where you manage a kingdom and try to end with the most gold, land, or army.
   //Intro
   cout << "Welcome to Kingdom Management."<<endl;
   cout << "Let's begin."<<endl;
   system("pause");
   //Declare Variables (STATS)
   int selection, selection1, selection2, selection3, selection4, selection5, selection6, selection7; // This holds a selection for a console input (cin).
   int redo = 1; // This is for while loops at selections.
   int gold; // This is the ammount of money the kingdom has.
   int army; // This is the size of the kingdom's army.
   int pop = 10; // This is your population of workers available.
   int glory = 0; //This is your kingdom's "score."
   int turn = 1; //This is what turn you are on.
   //GAME BEGINS
   while ( redo == 1 )
   {
      cout << "What type of kingdom would you like to have? Here are your selections:"<<endl;
      cout << "(1)Aggressive: \t 20 Gold \t 50 Army"<<endl;
      cout << "(2)Wealthy: \t 50 Gold \t 20 Army"<<endl;
      cout << "(3)Balance: \t 35 Gold \t 35 Army"<<endl;
      cout << "Type the number in parenthasese next to the one you want: "<<endl;
      cin >> selection1;
      if ( selection1 == 1 )
      {
         gold = 20;
         army = 50;
         redo = 0;
      }
      else if ( selection1 == 2 )
      {
         gold = 50;
         army = 25;
         redo = 0;
      }
      else if ( selection1 == 3 )
      {
         gold = 10;
         army = 10;
         redo = 0;
      }
      else
      {
         cout << "Wrong Selection."<<endl;
         redo = 1;
      }
   }//ENDS WHILE LOOP
   cout << "Nice selection."<<endl;
   //Declare Variables (BUILDINGS AND RESEARCH)
   //Buildings
   int barracks = 0; //Tells whether or not you have a barracks.
   int town_hall = 0; //Tells whether or not you have a town hall.
   int market = 0; //Tells whether or not you have a market.
   int farm = 0; // Tells whether or not you have a farm.
   //Research
   int lawmaking = 0; //Tells if you have researched lawmaking or not.
   int siege_weapon = 0; //Tells if you have researched seige weapons.
   int agriculture = 0; //Tells if you have researched large-area agriculture.
   int livestock = 0; //Tells if you have researched raising livestock.
   redo = 1;
   while ( redo == 1 )
   {
      if ( army == 0 )
      {
         cout << "Game Over. Your army has 0 people."<<endl;
         return 0;
      }
      else if ( gold == 0 )
      {
         cout << "Game Over. You ran your kingdom out of money and were overthrown."<<endl;
      }
      else
      {
         if ( turn == 1 )
         {
            cout << "Population is the ammount of workers you have available."<<endl;
            cout << "Glory is your score. It is based on your actions as king."<<endl;
         }
         else
         {
            cout << endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
         }
         cout << "TURN "<<turn<<endl;
         cout << "====================="<<endl;
         if ( barracks == 1 )
         {
            army = army + 5;
         }
         else if ( market == 1 )
         {
            if ( farm == 1 )
            {
               gold = gold + 2;
            }
            if ( livestock == 1 )
            {
               gold = gold + 2;
            }
            gold = gold + 5;
         }
         else if ( pop < 3 )
         {
            pop = pop + 1;
         }
         else if ( pop > 3, pop < 5 )
         {
            pop = pop + 2;
         }
         else if ( pop >5, pop < 10 )
         {
            pop = pop + 3;
         }
         else if ( pop > 10 )
         {
            pop = pop + 5;
         }
         cout << "Population: \t" << pop <<endl;
         cout << "Gold: \t \t"<< gold <<endl;
         cout << "Army: \t \t"<< army <<endl;
         cout << "Glory: \t \t" << glory <<endl;
         system("pause");
         cout << "What would you like to do?"<<endl;
         cout << "(1)Research"<<endl<<"(2)Build"<<endl<<"(3)Enact Law (COMING SOON)"<<endl;
         cout << "(4)Nothing"<<endl<<"(5)Quit Game"<<endl;
         cin >> selection;
         if ( selection == 1 )
         {
            cout << "Here is the research list. Please select one."<<endl;
            cout << "-#-|----NAME---- \t ----COST---- \t ----DESCRIPTION----"<<endl;
            cout << "(1)|Lawmaking \t \t 5G, 0A, 0P \t Step 1 To Enacting Laws"<<endl;
            cout << "(2)|Siege Weapons \t 10G, 3A, 3P \t Better Fighting Power"<<endl;
            cout << "(3)|Agriculture \t 5G, 0A, 5P \t Step 1 To Markets"<<endl;
            cout << "(4)|Livestock \t \t 10G, 0A, 10P \t Better Markets"<<endl;
            cin >> selection3;
            if ( selection3 == 1 )
            {
               if ( lawmaking == 0 )
               {
                  gold = gold - 5;
                  lawmaking = 1;
                  cout << "You have researched LAWMAKING."<<endl;
                  turn = turn + 1;
               }
               else
               {
                  cout << "You have already researched LAWMAKING."<<endl;
               }
            }
            else if ( selection3 == 2 )
            {
               if ( siege_weapon == 0 )
               {
                  gold = gold - 10;
                  army = army - 3;
                  pop = pop - 3;
                  siege_weapon = 1;
                  cout << "You have reasearched SIEGE WEAPONS."<<endl;
                  turn = turn + 1;
               }
               else
               {
                  cout << "You have already resarched SIEGE WEAPONS."<<endl;
               }
            }
            else if ( selection3 == 3 )
            {
               if ( agriculture == 0 )
               {
                  gold = gold - 5;
                  pop = pop - 5;
                  agriculture = 1;
                  cout << "You have reasearched AGRICULTURE."<<endl;
                  turn = turn + 1;
               }
               else
               {
                  cout << "You have already resarched AGRICULTURE."<<endl;
               }
            }
            else if ( selection3 == 4 )
            {
               if ( livestock == 0 )
               {
                  gold = gold - 10;
                  pop = pop - 10;
                  livestock = 1;
                  cout << "You have reasearched LIVESTOCK."<<endl;
                  turn = turn + 1;
               }
               else
               {
                  cout << "You have already resarched LIVESTOCK."<<endl;
                  redo = 1;
               }
            }
            else if ( selection == 2 )
            {
               cout << "Here is the building list."<<endl;
               cout << "-#-|----NAME---- \t ----COST---- \t ---------DESCRIPTION---------"<<endl;
               cout << "(1)|Barracks \t 10G, 5A, 5P \t Adds 5 to your army each turn."<<endl;
               cout << "(2)|Town Hall \t 10G, 1A, 5P \t Unlocks Lawmaking."<<endl;
               cout << "(3)|Market \t 5G, 0A, 3P \t Adds 5 gold every turn."<<endl;
               cout << "(4)|Farm \t 5G, 0A, 5P \t Market Upgrade."<<endl;
               cout << "What shall you build?"<<endl;
               cin >> selection5;
               if ( selection5 == 1 )
               {
                  if ( barracks == 0 )
                  {
                     gold = gold - 10;
                     army = army - 5;
                     pop = pop - 5;
                     barracks = 1;
                     cout << "You have built a BARRACKS."<<endl;
                     turn = turn + 1;
                     redo = 1;
                  }
                  else
                  {
                     cout << "You already have a BARRACKS."<<endl;
                     redo = 1;
                  }
               }
               else if ( selection5 == 2 )
               {
                  if ( town_hall == 0 )
                  {
                     if ( lawmaking == 1 )
                     {
                        town_hall = 1;
                        cout << "You have built a TOWN HALL."<<endl;
                        turn = turn + 1;
                        redo = 1;
                     }
                     else
                        cout << "You must have researched in LAWMAKING to make a TOWN HALL."<<endl;
                     redo = 1;
                  }
                  else
                  {
                     cout << "You already have a BARRACKS."<<endl;
                     redo = 1;
                  }
               }
               else if ( selection5 == 3 )
               {
                  if ( farm == 0 )
                  {
                     gold = gold - 5;
                     pop = pop - 3;
                     farm = 1;
                     cout << "You have built a MARKET."<<endl;
                     turn = turn + 1;
                     redo = 1;
                  }
                  else
                  {
                     cout << "You already have a MARKET."<<endl;
                     redo = 1;
                  }
               }
               else if ( selection5 == 4 )
               {
                  if ( farm == 0 )
                  {
                     if ( agriculture == 1 )
                     {
                        gold = gold - 5;
                        pop = pop - 5;
                        farm = 1;
                        cout << "You have built a FARM."<<endl;
                        turn = turn + 1;
                        redo = 1;
                     }
                     else
                        cout << "You need to research AGRICULTURE first."<<endl;
                     redo = 1;
                  }
                  else
                  {
                     cout << "You already have a FARM."<<endl;
                     redo = 1;
                  }
               }
               else if ( selection == 3 )
               {
                  if ( town_hall == 1 )
                  {
                     cout << "LAWMAKING COMING SOON."<<endl;
                     turn = turn + 1;
                     redo = 1;
                  }
                  else
                     cout << "You must research Lawmaking and build a Town Hall before you can make laws."<<endl;
               }
               else if ( selection == 4 )
               {
                  cout << "You wait out the turn. The people want a working king!"<<endl;
                  turn = turn + 1;
                  redo = 1;
               }
               else if ( selection == 5 )
               {
                  cout << "Are you sure you want to quit?"<<endl;
                  cout << "(1)Quit \t (2)Stay"<<endl;
                  cin >> selection4;
                  if ( selection4 == 1 )
                  {
                     cout << "Goodbye!"<<endl;
                     cout << "You ended the game with the following stats:"<<endl;
                     cout << "You finished on turn "<<turn<<"."<<endl;
                     cout << "Population: \t" << pop <<endl;
                     cout << "Gold: \t \t"<< gold <<endl;
                     cout << "Army: \t \t"<< army <<endl;
                     cout << "Glory: \t \t" << glory <<endl;
                     return 0;
                  }
                  else
                  {
                     cout << "Good! Let's continue!"<<endl;
                     redo = 1;
                  }
               }
               else
               {
                  cout << "Wrong Selection."<<endl;
                  redo = 1;
               }
            }
         }
      }
   }
}

And then also the linter tells me (among other things)...

else if ( selection == 2 )
/* main.cpp:210: Info 774: Boolean within 'if' always evaluates to False */
else if ( selection == 3 )
/* main.cpp:299: Info 774: Boolean within 'if' always evaluates to False */
else if ( selection == 4 )
/* main.cpp:310: Info 774: Boolean within 'if' always evaluates to False */
else if ( selection == 5 )
/* main.cpp:316: Info 774: Boolean within 'if' always evaluates to False */

It would seem that selection3 is what you might have wanted.

commented: Very nice job :) +19
commented: Excellent job. +7

When (as you have, after Dave's formatting), you see 6 closing braces in a row, and the last one is over 300 lines from it's buddy, then it's time to REFACTOR.

Add some functions man, there is no reasonable way to test that code in an incremental fashion.

So u r saying I should use

else if (selection3 == 1){

else if (selection3 == 2){

else if (selection3 == 3){

else if (selection3 == 4){

else if (selection3 == 5){

The first thing to notice is how the indentation of Dave's post progressively gets more severe.

This usually means you missed out a } somewhere earlier on.

That would explain the problems like

else if ( selection == 5 )
      /* main.cpp:316: Info 774: Boolean within 'if' always evaluates to False */

Essentially, you've managed to nest

if ( cond1 == 4 ) {
  // lot of code
  if ( cond1 == 5 ) {  // cond1 MUST be 4 here, so this is always false!
  }
}

Now, if you had half a dozen functions, no more than 20 or so lines long, then you'd be able to see that right off.

When (as you have, after Dave's formatting), you see 6 closing braces in a row, and the last one is over 300 lines from it's buddy, then it's time to REFACTOR.

Add some functions man, there is no reasonable way to test that code in an incremental fashion.

Can you teach me how to go back and forth through functions?
Would u use:

int main(1)

and to get there would I use:

return 1;

or is there some other way?? Plz teach me

Fixed the problem

That's good to hear. But after all, maybe it still would be better to put the game aside for some time, and learn how to use functions/structures instead. What do you think? This is just to say that don't overdo yourself with a single function consisting of several hundreds of lines of code.

I added a bunch of functions. One for every building, research, law, attack, etc.

commented: Well Done! +4
commented: Excellent! +19
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.