C++/Loop

Advertisement


Question
I am new in programming so please have understanding, I am trying to make a text base game and I ran into some problems:
After I enter anything else than a number after a first pass through the loop it makes an infinite loop, I really can not find a solution so here is the code I made so far:

# include <iostream>
using namespace std;
int Town (int);
int Blacksmith (int);
int BuyWeapon (int);
int Stats (int,int,int,int,int,int);
void Message ();
int p=0,c1=0,c2=0,c3=0,c4=0,strength=1,dexterity=1,intelligence=1,charisma=1,armor=1,attack=1;
void main ()
{   
  cout<<"\t\t************************************************* "<<endl;  
  cout<<"\t\t*\t\t\t\t\t\t* "<<endl;    
  cout<<"\t\t*\t\t\t\t\t\t* "<<endl;    
  cout<<"\t\t*\t\tSHADOW LORDS\t\t\t* "<<endl;
  cout<<"\t\t*\t\t\t\t\t\t* "<<endl;    
  cout<<"\t\t*\t\t\t\t\t\t* "<<endl;    
  cout<<"\t\t************************************************* "<<endl;  
cout<<"\n\nThe World is in need of heroes once again. A neverending battle between light\nand darkness is upon us and you are the only hope of this god forgotten world.";
cout<<"\nYou begin your quest with the sunrise."<<endl;
cout<<"Where do you want to go:\n1.)Town\n2.)Forest\n3.)Ruins of Elenor\n4.)Neverwinter land"<<endl;
cin>>c1;
if (c1==1)
{   Town (c1);   
  if (c1==1)
  {   Blacksmith (c2);
     if (c2==1)
     {do
     {   BuyWeapon(c3);
     switch (c4)
        {   case 1:{
         attack=2;
         Message ();
         }
         break;
         case 2:{
         attack=4;
         Message ();
         }
         break;
         case 3:{
         attack=8;
         Message ();
         }
         break;
         case 4:{
         attack=15;
         Message ();
         }
         break;
         case 5:{
         attack=50;
         Message ();
         }
         break;
         case 6:p++;
         break;
         case 7:Stats(strength,dexterity,intelligence,charisma,armor,attack);
         default: BuyWeapon(c3);
        }
     }while (p==0);
     
     Blacksmith(c4);
     }
}
}
}
int Town (int c1)
{   cout<<"You stand and watch people talking, kids running and many shops."<<endl;
  cout<<"Where do you wish to go:";
  cout<<"\n1.)Blacksmith\n2.)Mage Guild\n3.)Tavern\n4.)Back"<<endl;
  cin>>c2;
  return c2;
}
int Blacksmith (int c2)
{   cout<<"How can I be of service?"<<endl;
  cout<<"1.)I would like to buy a weapon.\n2.)I would like to buy an armor.\n3.)Rumors.\n4.)Back."<<endl;
  cin>>c3;
  return c3;
}
int BuyWeapon (int c3)
{   cout<<"\n1.)Rusty dagger - 50 cl\n2.)Long Sword - 200 cl\n3.)Spear - 300 cl\n4.)Mace - 500 cl\n5.)Enchanted Sword - 1000 cl\n6.)Back\n";
  cin>>c4;
  return c4;
}
int Stats (int strength,int dexterity,int intelligence,int charisma,int armor,int attack)
{   int m=0;
  cout<<"Strength:"<<strength<<"\t\tDexterity:"<<dexterity<<"\nIntelligence:"<<intelligence<<"\t\tCharisma:"<<charisma<<"\nArmor:"<<armor<<"\t\t\tAttack:"<<attack<<endl<<endl;
  return strength;
}
void Message ()
{
     cout<<"Thank you for the buying!"<<endl;
}

Answer
There is nothing in the do/while loop that changes p when the input is 0 (what it would be if a number wasn't entered).  Your input statements should have error checking and only return data the main loop and other functions are able to accept.

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.