trouble with a loop (beginner)

This is a discussion on trouble with a loop (beginner) within the C++ Programming forums, part of the General Programming Boards category; Let me start of by saying hello to ye all, since this is my first post. I took up c++ ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    1

    trouble with a loop (beginner)

    Let me start of by saying hello to ye all, since this is my first post. I took up c++ programming about a week ago, with the help of "The C++ Programming Language 3rd ed" and various tutorials and the like on the internet.. Ahh, now that that's out of the way lets go to my problem. Consider this part of my code:

    Code:
    int movement()
    {
    	cout << "\t\tTurn: " << turn+1 << endl << endl;
    
    	cout << "You are in zone "<< cordX << "-" << cordY << "-" << cordZ << endl;
    	
    	int a=0;
    	while(a==0)
    	{
    	
    		mov=getch();
    		
    
    		
    		
    	if(mov=='w')		//north
    	{
    		a=a+1;
    		cordX=cordX+1;
    		turn=turn+1;
    	}
    	
    	if(mov=='s')		//south
    	{
    		a=a+1;
    		cordX=cordX-1;
    		turn=turn+1;
    	}
    	if(mov=='d')		//east
    	{
    		a=a+1;
    		cordY=cordY+1;
    		turn=turn+1;
    	}
    	if(mov=='a')		//west
    	{
    		a=a+1;
    		cordY=cordY-1;
    		turn=turn+1;
    	}
    	if(mov=='z')		//in
    	{
    		a=a+1;
    		cordZ=cordZ+1;
    		turn=turn+1;
    	}
    	if(mov=='x')		//out
    	{
    		a=a+1;
    		cordZ=cordZ-1;
    		turn=turn+1;
    	}
    	else				//anything else is an error
    	{
    		
    		cout << "You cannot do that!" << endl;
    	}
    		
    	}
    return 0;
    }
    Now, when I run the program it does what it is supposed to, but also what is in the "else part" (You cannot do that!)
    I'm sure it's just some trivial mistake, but I can't see it..

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    Try using switch... case instead of if else.

    Anyways, in your code the else condition is checked only when if(mov=='x') is false.
    Success is a two word story, "WORK" works.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    if(mov=='w')		//north
    	{
    		a=a+1;
    		cordX=cordX+1;
    		turn=turn+1;
    	}
    	
    	else if(mov=='s')		//south
    	{
    		a=a+1;
    		cordX=cordX-1;
    		turn=turn+1;
    	}
    	else if(mov=='d')		//east
    	{
    		a=a+1;
    		cordY=cordY+1;
    		turn=turn+1;
    	}
    	else if(mov=='a')		//west
    	{
    		a=a+1;
    		cordY=cordY-1;
    		turn=turn+1;
    	}
    	else if(mov=='z')		//in
    	{
    		a=a+1;
    		cordZ=cordZ+1;
    		turn=turn+1;
    	}
    	else if(mov=='x')		//out
    	{
    		a=a+1;
    		cordZ=cordZ-1;
    		turn=turn+1;
    	}
    	else				//anything else is an error
    	{
    		
    		cout << "You cannot do that!" << endl;
    	}
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with while loop
    By anything25 in forum C Programming
    Replies: 4
    Last Post: 05-23-2009, 03:59 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 03:45 AM
  3. Help Please, Beginner Question, rewrite loop
    By office888 in forum C Programming
    Replies: 4
    Last Post: 12-11-2006, 10:07 AM
  4. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  5. Trouble with a counting loop
    By RedZippo in forum C++ Programming
    Replies: 4
    Last Post: 03-21-2004, 03:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21