Thread: World movement

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

    World movement

    I want it to get direction from user, and then add 1 to int xco if the direction was north. I had it semi-working before it would either display zero or some random number. Now it gets the if statement you typ in n and it terminates.
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <ctime>
    using namespace std;
    void to()                     //first territory
    {
    cout<<"You can move south or east. type h for help.";
    }
    
    int main(int argc, char *argv[])
    {
        char namen[100];
        int initial;
        int strength;
        int agility;
        int defense;
        char command;
        char direction;
        char n;
        int xco=0;
        
        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<<"Enter your name:"; //ask for name
             cin>>namen;
             cout<<namen<<" is it. Alright then\n";
             cout<<"Enter 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";
    	system("PAUSE");
        break;
        case 3:          //exit
             return 0;
        break;
        default:          
             cout<<"error";        //default
        system("PAUSE");
        }
        //intro
        cout<<"                        Welcome to naushmien\n\n*Dramatic Music*\n\n";
        to();
        for(int loop=0;loop<100;loop++){  //main loop
    	cin>>command;
    	switch(command){
    	case 'm':                    //movement
    	cout<<"which way would you like to move?";
    	cin>>direction;
    	if (direction=n)
    	return xco+1;
    	cout<<xco;          //for test
    	break;
    	break;
    	default:
    	cout<<"messed";
    	break;
    	}
    	}
        
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Its making me mad. Then of course there is probably dumb reason it won't work or an if statment won't go there.
    My computer is awesome.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by cerin
    I want it to get direction from user, and then add 1 to int xco if the direction was north. I had it semi-working before it would either display zero or some random number. Now it gets the if statement you typ in n and it terminates.

    Its making me mad. Then of course there is probably dumb reason it won't work or an if statment won't go there.
    1. Bug: if (direction = n) should probably be if(direction == 'n')

    2. To see what's going on, put in some informative output statements. Note that for some cases the program returns from main() without any useful debugging information.

    Perhaps your loop could look something like this:

    Code:
      for (int loop = 0; loop < 100; loop++){  //main loop
        cout << endl << "Enter Command: " << flush;
        cin >> command;
    
        switch(command){
        case 'm':                    //movement
          cout << "Which way would you like to move? " << flush;
          cin >> direction;
          if (direction =='n') {
            //return xco+1; // instead of this, show the results
            xco++;
            cout<<"moved n: xco = " << xco << endl;
          }
          else {
            cout << "You entered the direction " << direction << endl;
          }
          break;
    
        default:
          cout << "You entered the command " << command << endl;
          break;
        }
      }
    (You can ctrl-c out of the thing if you don't want to enter the command a hundred times.)

    Regards,

    Dave

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <ctime>
    using namespace std;
    void to()                     //first territory
    {
    cout<<"You can move south or east. type h for help.";
    }
    
    int main(int argc, char *argv[])
    {
        char namen[100];
        int initial;
        int strength;
        int agility;
        int defense;
        char command;
        char direction;
        char n,s,e,w;
        int xco=0;
        int yco=0;
        
        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<<"Enter your name:"; //ask for name
             cin>>namen;
             cout<<namen<<" is it. Alright then\n";
             cout<<"Enter 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";
    	system("PAUSE");
        break;
        case 3:          //exit
             return 0;
        break;
        default:          
             cout<<"error";        //default
        system("PAUSE");
        }
        //intro
        cout<<"                        Welcome to naushmien\n\n*Dramatic Music*\n\n";
        to();
        for(int loop=0;loop<100;loop++){  //main loop
    	cin>>command;
    	switch(command){
    	case 'm':                    //movement
    	cout<<"which way would you like to move?";
    	cin.get();
    	cin>>direction;
    	if (direction=n)
    	yco++;
    	cout<<"Y="<<yco<<" X="<<xco;
    	if (direction=s)
    	yco--;
    	cout<<"Y="<<yco<<" X="<<xco;
    	if (direction=e)
    	xco++;
    	cout<<"Y="<<yco<<" X="<<xco;
    	if (direction=w)
    	xco--;
    	cout<<"Y="<<yco<<" X="<<xco;
    	break;
    	default:
    	cout<<"messed";
    	break;
    	}
    	}
        
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    How do I put all of the stuff in red in a function? or how can I do something to it so the switch changes for each region your in?
    My computer is awesome.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by cerin

    1. How do I put all of the stuff in red in a function?

    or

    2. how can I do something to it so the switch changes for each region your in?
    1. If you want the function to change xco and/or yco, you could pass pointers to xco and yco to the function.

    2. I don't know exactly what you mean by this. If you mean that action depends on some present values of xco and yco, then you could have a function that returns a value depending on whatever conditions determine what region you are in. Then you would have condional statements (switch or if-else) that perform the action required for a given region.

    You still have the bug that I mentioned previously (direction = n), except it seems to have propagated to s, e, and w. Sometimes these things take on a lives of their own if you don't stamp them out as soon as they are discovered.

    Regards,

    Dave

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    No I think i see how to fix most of this stuff now. My logic is that it reads xco and yco aka x coordinate and y coordinate. The map would be like a graph and regions, towns, etc.. are represented by points on the graph. direction=n hmmmmmmm if I change this to direction=='n' then is just displays the default message. I want it to add 1 to xco and then display xco and yco.
    My computer is awesome.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    New code:
    Code:
    cout<<"which way would you like to move?";
    	cin.get();
    	cin>>direction;
    	if (direction=='n' &&(yco<='8' && yco>='-5'))
    	yco++;
    	cout<<"Y="<<yco<<" X="<<xco;
    	else{
    	cout<<"can't do that";
    	}
    	if (direction=='s' && (yco<='8' && yco>='-5'))
    	yco--;
    	cout<<"Y="<<yco<<" X="<<xco;
    	else{
    	cout<<"can't do that";
    	}
    	if (direction=='e' && (xco<='8'&& xco>='-5'))
    	xco++;
    	cout<<"Y="<<yco<<" X="<<xco;
    	else{
    	cout<<"can't do that";
    	}
    	if (direction=='w' && (xco<='8' && xco>='-5'))
    	xco--;
    	cout<<"Y="<<yco<<" X="<<xco;
    	else{
    	cout<<"can't do that";
    	}
    	break;
    	default:
    	cout<<"messed";
    	break;
    	}
    	}
    I have a lot of problems syntax errors before all else statments. when I have it check xco and yco in the ifs it thinks I'm trying to use a multi character constant.
    My computer is awesome.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by cerin
    New code:
    Code:
    cout<<"which way would you like to move?";
    	cin.get();
    	cin>>direction;
    	if (direction=='n' &&(yco<='8' && yco>='-5'))
    	yco++;
    	cout<<"Y="<<yco<<" X="<<xco;
    	else{
    	cout<<"can't do that";
    	}
    I have a lot of problems syntax errors before all else statments. when I have it check xco and yco in the ifs it thinks I'm trying to use a multi character constant.

    the statement
    Code:
    if (direction=='n' &&(yco<='8' && yco>='-5'))
    only affects the very next statement. So, after this:
    Code:
    	if (direction=='n' &&(yco<='8' && yco>='-5'))
    	yco++;
    that is the end of the if stuff unless you have an else statement right here.

    Since the next statement is not an else, that is the end.

    Now, the compiler encounters an orphan else and gives you the error. It will do this every time (every time).

    Now, if you want to include more than one statment in a block of code after an if statement, use braces {}, something like this
    Code:
      if (direction=='n' &&(yco<='8' && yco>='-5')) {
        yco++;
        cout<<"Y="<<yco<<" X="<<xco;
      }
      else {
        cout<<"can't do that";
      }
    The block of statements between the braces are treated as a single statement as far as if-else are concerned.

    Braces never hurt, even if there is only one statement in the block. You might want to consider getting in the habit of using braces for if(), for(), while(), etc. All of them affect only the single following statement unless you use braces for a block.

    Regards,

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The 7 New Wonders of the World
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 12-08-2006, 01:55 PM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  3. OpenGL coordinates
    By Da-Nuka in forum Game Programming
    Replies: 5
    Last Post: 01-10-2005, 11:26 AM
  4. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  5. Too much to ask ?
    By deflamol in forum C Programming
    Replies: 2
    Last Post: 05-06-2004, 04:30 PM