V confused

This is a discussion on V confused within the C Programming forums, part of the General Programming Boards category; Apologies for it being very long horizintally - but you dont need any of the code on the far right ...

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

    V confused

    Apologies for it being very long horizintally
    - but you dont need any of the code
    on the far right anyway

    right. I have
    Code:
    scanf("%c",&look);
    and its not doing as its told.
    Under it follow a whole forray of if statements
    the last of which was you entered an invlaid character
    (N, S, W, E are valids, the rest non valid) The only problem is
    when i run the code it doesnt actually ask for an input for look
    , and then continues as if an invalid character has been entered

    The whole function is below and is almost an exact duplicate of another i have written that works fine.

    Code:
    void inspect(object *p1, rover *myRover, location **map)
    {
    
    	int mapsizex, mapsizey;
    	char look;
    
    	
    	mapsizex=myRover->mapsizex;
    	mapsizey=myRover->mapsizey;
    
    
    	map[myRover->Current_x][myRover->Current_y-1].ObjId=12;
    
    	printf("\n\nIn which direction would you like to look??\nPlease enter N,E,S, or W\n\n");
    	scanf("%c",&look);
    	
    	if(look=='N')
    	{
    		if( ((myRover->Current_x&&myRover->Current_y)>=0) && 
    			((myRover->Current_x<=(mapsizex-1))&&(myRover->Current_y<=(mapsizey-1))))
    		{
    			if((map[myRover->Current_x][myRover->Current_y+1].ObjId!=12) && 
    				(map[myRover->Current_x][myRover->Current_y+1].ObjId!=9))
    			{
    				printf("\nHere there is a %s, %s\n",p1->objname[map[myRover->Current_x+1][myRover->Current_y].ObjId], p1->objtype[map[myRover->Current_x+1][myRover->Current_y].ObjId]);
    				(myRover->power)--;
    			}
    
    			else printf("\nSorry, You can not move to that square, it is occupied by:\n%s, \n%s\n\n", p1->objname[map[myRover->Current_x][myRover->Current_y].ObjId], p1->objdesc[map[myRover->Current_x][myRover->Current_y+1].ObjId]);
    		}
    		else printf("\nSorry you are at the edge of the map\n");
    
    	}
    
    	else if(look=='S')
    	{
    		if( ((myRover->Current_x&&myRover->Current_y)>=0) &&
    			((myRover->Current_y<=(mapsizey))))
    		{
    			if((map[myRover->Current_x][myRover->Current_y-1].ObjId!=12) && 
    				(map[myRover->Current_x][myRover->Current_y-1].ObjId!=9))
    			{
    				printf("\nHere there is a %s, %s\n",p1->objname[map[myRover->Current_x-1][myRover->Current_y].ObjId], p1->objtype[map[myRover->Current_x-1][myRover->Current_y].ObjId]);
    				(myRover->power)--;
    			}
    
    			else printf("\nSorry, You can not move to that square, it is occupied by:\n%s, \n%s\n\n", p1->objname[map[myRover->Current_x][myRover->Current_y-1].ObjId], p1->objdesc[map[myRover->Current_x][myRover->Current_y-1].ObjId]);
    		}
    		else printf("\nSorry you are at the edge of the map\n");
    	}
    
    	else if(look=='E')
    	{
    		if( ((myRover->Current_x&&myRover->Current_y)>=0) && 
    			((myRover->Current_x<=(mapsizex-1))&&(myRover->Current_y<=(mapsizey-1))))
    		{
    			if((map[myRover->Current_x+1][myRover->Current_y].ObjId!=12) && 
    				(map[myRover->Current_x+1][myRover->Current_y].ObjId!=9))
    			{
    				printf("\nHere there is a %s, %s\n",p1->objname[map[myRover->Current_x][myRover->Current_y+1].ObjId], p1->objtype[map[myRover->Current_x][myRover->Current_y+1].ObjId]);
    				(myRover->power)--;
    			}
    
    			else printf("\nSorry, You can not move to that square, it is occupied by:\n%s, \n%s\n\n", p1->objname[map[myRover->Current_x+1][myRover->Current_y].ObjId], p1->objdesc[map[myRover->Current_x+1][myRover->Current_y].ObjId]);
    		}
    		else printf("\nSorry you are at the edge of the map\n");
    	}
    
    	else if(look=='W')
    	{
    		if( ((myRover->Current_x&&myRover->Current_y)>=0) && 
    			((myRover->Current_x<=(mapsizex))))
    		{
    			if((map[myRover->Current_x-1][myRover->Current_y].ObjId!=12) && 
    				(map[myRover->Current_x-1][myRover->Current_y].ObjId!=9))
    			{
    				printf("\nHere there is a %s, %s\n",p1->objname[map[myRover->Current_x-1][myRover->Current_y].ObjId], p1->objtype[map[myRover->Current_x-1][myRover->Current_y].ObjId]);
    				(myRover->power)--;
    			}
    
    			else printf("\nSorry, You can not move to that square, it is occupied by:\n%s,\n%s\n\n", p1->objname[map[myRover->Current_x-1][myRover->Current_y].ObjId], p1->objdesc[map[myRover->Current_x-1][myRover->Current_y].ObjId]);
    		}
    		else printf("\nSorry you are at the edge of the map\n");
    	}
    
    	else
    	{
    		printf("\nThe direction must be given as a capital letter, N,S,E or W\nyou entered: %c \n\n",look);
    	}
    
    	printf("%d %d\n", myRover->Current_x, myRover->Current_y);
    
    
    	system("pause");
    
    
    
    
    
    
    }
    i realise its ugly, but its all structures in structures....
    but its not even going into the primary five if statements,
    just jumping straight to the fifth and saying invalid input from the user.

    Any ideas?

    Cheers,
    Mitch
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( (look = fgetc( stdin )) != '\n' );
    printf("\n\nIn which direction would you like to look??\nPlease enter N,E,S, or W\n\n");
    scanf("%c",&look);
    Try that. You probably have something left in your input stream. That will clear it.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    41
    Spot on!

    Thanks a lot !!!
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused
    By jeev2005 in forum C Programming
    Replies: 5
    Last Post: 06-01-2006, 02:04 PM
  2. Confused
    By (TNT) in forum C# Programming
    Replies: 1
    Last Post: 11-23-2005, 03:49 PM
  3. So Now Im getting confused?!?!?
    By zergdeath1 in forum C++ Programming
    Replies: 11
    Last Post: 03-06-2004, 04:41 PM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 10:11 PM

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