Thread: Looping error

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    Looping error

    After my program gets agility, strength, and defense it loops back to the opening menu. I have highlighted what I think to be the problem area. I have checked the the logic many times over and I have probably overlooked it, but I can't seem to find the problem.


    Code:
    /*This game was made by Shelby Smith. Please don't try to 
    redistribute this as your own, but feel free to use as an
    example for some of your code. Some of the techniques I 
    use are most definetely not the best ones considering all the 
    if statments I will need.*/
    
    #include <cstdlib>
    #include <iostream>
    #include <ctime>
    #include <time.h>
    
    
    using namespace std;
    void sleep_seconds ( long seconds ) {
      clock_t limit, now = clock();
      limit = now + seconds * CLOCKS_PER_SEC;
      while ( limit > now )
        now = clock();
    }
    
    void sleep_ticks ( long ticks ) {
      clock_t limit, now = clock();
      limit = now + ticks;
      while ( limit > now )                   //waiting stuff
        now = clock();
    }
    
    void wait(long milliseconds)
    {
      long timeout = clock() + milliseconds;
      while( clock() < timeout ) continue;
    }
    void intro()                     //story start
    {
    cout<<"*Strange man runs towards you and away from the castle frantically.\n";
    wait(5000);
    cout<<"\nHe draws closer and closer to you, but you can see that he is shaken and\ndistraught\n";
    wait(5000);
    cout<<"\nHe tells you he has just escaped from the king's castle.\n";
    wait(5000);
    cout<<"\nHe tells you of horrible atrocities and the evil that is spreading like\n\nwildfire starting with the king and is slowly moving down the ranks of the\n\nmilitary and soon to the citizens.\n";
    wait(8000);
    cout<<"\nYou see him becoming all the more nervous as you hear footsteps in the distance\n";
    wait(5000);
    cout<<"\nYou see a guard approach.\n";
    wait(3000);
    cout<<"\nIn a violent voice he shouts at the man.\n";
    wait(3000);
    cout<<"\nWhat are you doing out here?!\n";
    wait(3000);
    cout<<"\nWhat have you told him?!\n";
    wait(3000);
    cout<<"\nCome, your punsishment awaits.\n";
    wait(3000);
    cout<<"\nThe gaurd glares at you.\n";
    wait(3000);
    cout<<"\nYou best forget what you have been told.\n";
    wait(3000);
    cout<<"\nTo insure you tell no one I'll need your name.\n";
    wait(3000);
    }
    //all weapon structures
    struct weapon{			//bronze sword stats
    	int bswordatt;
    	int bswordweight;
    	bool bswordi;
    	bool bsworde;
    	int bdaggerwatt;		//bronze dagger
    	int bdaggerweight;
    	bool bdaggeri;
    	bool bdaggere;
    	};
    
    	struct monster{
    	int manhp;
    	bool manfight;
    	int manatt;
    	int mandef;
    	int gobhp;
    	};
    
    int main(int argc, char *argv[])
    {
    	monster man;
    	man.manhp=20;
    	man.manfight;
    	man.manatt;
    	man.mandef;
    	
    	monster goblin;
    	goblin.gobhp=40;
    
    	//weapon structures in int main()
    	weapon bsword;		//bronze sword
    	bsword.bswordatt=2;
    	bsword.bswordweight=10;
    	bsword.bswordi;
    	bsword.bdaggere;
    	
    	weapon bdagger;		//bronze dagger
    	bdagger.bdaggerwatt;
    	bdagger.bdaggerweight;	
    	bdagger.bdaggeri;
    	bdagger.bdaggere;
    	
    	
    	
    	char namen[100];
    	char input[100];
    	int monrand;
    	int initial;
    	int strength;
    	int agility;
    	int defense;
    	char command;                  //a hole crap load of variables
    	char direction;
    	int xco=0;
    	int yco=0;
    	int hp;
    	char combat;
    	int mhp1;
    	int fight;
    	int fcom;
    	int finalcom;
    	int randenc;
        
        srand( (unsigned int) time (0));         //seed mr. generator
        
     while(initial!=1||initial!=2||initial!=3){
        cout<<"What would you like to do?";
        cout<<"\n1.New game\n2.Load game\n3.Exit\n"; //Menu
        cin>>initial;
        switch(initial){ //menu switch statment
        case 1:
    		 cout<<"\n\n\n\n";	
    		 	                  //begin story line function		
             cout<<"\n\nEnter your name:"; //ask for name
             cin>>namen;
             cout<<namen<<" is it. Alright then\n";
             cout<<"\nEnter your strength agility and defense again(must=20):\n";
             cin>>strength>>agility>>defense;
    	while(strength+agility+defense!=20){     //loop if str ag and def not 20
    	cout<<"Enter strength, agility, and defense(must=20):";
    	cin>>strength>>agility>>defense;
    	}
        break;
        case 2:                     //loading game
    		 cout<<"Not availiable yet";
    	cin.get();
    	cin.ignore();
        break;
        case 3:          //exit
             return 0;
        break;
        default:          
             cout<<"error";        //default
        cin.get();
        cin.ignore();
        break;
        }
        }
        //intro
        cout<<"                        Welcome to naushmien\n\n*Dramatic Music*\n\n";
        for(int loop=0;loop<36000;loop++){  //main loop
        cout<<"\nCommand>";
    	cin>>command;
    	switch(command){
    	case 'm':                    //movement
    	cout<<"which way would you like to move?";
    	cin.get();
    	cin>>direction;
    	if (direction=='n' &&(yco<=8 && yco>=-5)){
    	yco++;
    	cout<<"\nY="<<yco<<" X="<<xco;}
    	if (direction=='s' && (yco<=8 && yco>=-5)){
    	yco--;
    	cout<<"\nY="<<yco<<" X="<<xco;}
    	if (direction=='e' && (xco<=8&& xco>=-5)){
    	xco++;
    	cout<<"\nY="<<yco<<" X="<<xco;}
    	if (direction=='w' && (xco<=8 && xco>=-5)){
    	xco--;
    	cout<<"\nY="<<yco<<" X="<<xco;}
    	randenc=rand()%10+1;
    	break;
    
    	case 'h':                     //help
    	cout<<"h-help(who woulda guessed)    m-move/n,e,s,w are the directions.\nf-fight f-subcommands a,d,f. all the commands for now.";
    	break;
    	case 'f':
    	monrand=rand()%2+1;
    	do{
    	switch(monrand){
    	case 1:
    	cout<<"You encounter a man\n";	
    	finalcom=man.manhp;
    	break;
    	case 2:
    	cout<<"You encounter a goblin\n";
    	finalcom=goblin.gobhp;
    	break;
    	}
    	}while(monrand<1 && monrand>2);
    	while(finalcom>0){              //fight
    	fight=rand()%5+1;
    	cout<<"How are you going to combat you opponent?";
    	cin>>combat;
    	if(combat=='a'){                //attack
    	fcom=(((strength*3)+agility)/fight);
    	finalcom=mhp1-fcom;
    	mhp1=finalcom;
    	}
    	if(finalcom>0){
    	cout<<"His hp is "<<finalcom<<"\n";
    	}
    	}
    	cout<<"\nHe is dead\n";                    //death
    	break;
    	case 'i':
    	if (bsword.bswordi==1){
    	cout<<"Bronze Sword\n";
    	}
    	break;
    	default:
    	cout<<"messed";
    	break;
    	}
    	}
        
    	cin.get();
    	cin.ignore();
    }
    My computer is awesome.

  2. #2
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    you're right in highlighting what you did... more specifically the problem area is your while statement. should you be using ||? OR should you be using &&? if you say something like "if my number is NOT equal to 5 OR my number is not equal to 6" that statement will ALWAYS evaluate to true. no matter what your number is. it's either not going to be 5, or it's not going to be 6. but if you said "if my number is not equal to 5 AND my number is not equal to 6" then if your number is neither, it will evaluate to false. this is what you want.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Ok, sorry to waste your time, but I didn't change that part of the code at all. I don't know how it got like that. It was working just fine before.
    Last edited by cerin; 01-19-2006 at 09:37 PM.
    My computer is awesome.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Just an idea. the code is looking good, but I would suggest using classes instead of structs when you get into more detail later on. I find them easier to maintain. But its looking good!

    Example of a class could be for your player:

    Code:
    class Player
    {
    public:
    Player();
    ~Player();
    
    private:
    int health;
    short score;
    string name;
    
    protected:
    m_pGameEngine*; 
    };
    it's just an idea

  5. #5
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    yea, i definitely agree. there is no difference between structs and classes, except for the fact you will have functions specific to the class. plus, i checked out your game, and there seems to be some kind of mathematical error with the monster hp system. you attack once or twice, and all of a sudden their hp jumps up to around 4000000. classes will probably help you manage that whole system a lot more easily.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM