Thread: Help with a for statement

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    Help with a for statement

    I am trying to execute a for loop, this is the only part of the code which I cannot seem to figure out.

    Code:
    	for(n=0;n<9;n++)
    	{
    		if(n == 0 || n == 2 || n == 4 || n == 6 || n == 7)
    		{
    			printf("Player X enter the letter of the space you want to occupy> ");
    			scanf("%c",&currentX);
    			player_X(currentX, board);
    			print_board(board);
    		}
    		else
    		{
    			printf("Player O enter the letter of the space you want to occupy> ");
    			scanf("%c",&currentO);
    			player_O(currentO, board);
    			print_board(board);
    		}
    	}
    What happens is that "Player X" can enter the space he/she wants to occupy, then the loop runs thru again with n = 1 (which should make it execute the else part of the loop,) the printf runs fine but the scanf never executes, Any help is appreciated.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    Solved.

    I solved it on IRC, for anyone wondering there needed to be a space before both of the %c's in the scanfs because they were reading the '\n's instead of wanting user input.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a FAQ on getting input from the user. scanf is tricky to get working the way you want. For single characters, there are better methods. In any event, you have to think about what's actually happening. You press 'd' (or whatever key) and then what? You hit enter. That's two keys, not one. You have to account for that in your program.


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

Popular pages Recent additions subscribe to a feed