Wondering where the error is D:

This is a discussion on Wondering where the error is D: within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm coding a function for a game somewhat like minesweeper and I have encountered a problem. How this ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Wondering where the error is D:

    Hey guys, I'm coding a function for a game somewhat like minesweeper and I have encountered a problem. How this function is suppose to work is the user gives it a movement command and if it is a valid move, the command is executed. I have a test driver to see if the coordinates are actually correct, problem is the coordinates are displayed twice when I hit a valid command. I cant seem to find why this is happening. Any help will be much appreciated#include "util.h"
    Code:
    /* file: driver */
    #include "mine.h"
    #include<stdio.h>
    
    main()
    {
            char command;
            int row = 0;
            int col = 0;
    
            printf("enter command(press q to quit):");
            command = getchar();
    
            while(command != 'q')
            {
                    if(coordinate(command, &row, &col))
                            printf("new coordinates:[%d][%d]\n",row,col);
                    else
                            printf("invalid move\n");
                    command = getchar();
            }
    }
    
    /*file: util.c */
    #include "tfdef.h"
    #include "mine.h"
    #include<stdio.h>
    int coordinate(char command, int *row, int *col)
    {
            switch(command)
            {
                    case 'y':
                    case 'Y':
                    *row = *row - 1;
                    *col = *col - 1;
                    break;
                    case 'u':
                    case 'U':
                    *row = *row - 1;
                    break;
                    case 'i':
                    case 'I':
                    *row = *row - 1;
                    *col = *col + 1;
                    break;
                    case'h':
                    case'H':
                    *col = *col - 1;
                    break;
                    case 'k':
                    case 'K':
                    *col = *col + 1;
                    break;
                    case 'n':
                    case 'N':
                    *row = *row + 1;
                    *col = *col - 1;
                    break;
                    case 'm':
                    case 'M':
                    *row = *row + 1;
                    break;
                    case ',':
                    case '<':
                    *row = *row+1;
                    *col = *col+1;
                    break;
            }
    
            if(*row < START||*col < START||*row >= MAXR)
                    return FALSE;
            else
                    return TRUE;

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    It looks like your code is processing both the key press, as well as '\n'.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It might be because you are feeding it the command and a carriage return character when pressing enter.

    Try using scanf("%c",&command);

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    but if that is the case, shouldn't it print the new coordinate and then invalid command?

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Quote Originally Posted by Thanksbro View Post
    but if that is the case, shouldn't it print the new coordinate and then invalid command?
    Where's the code that handle's "invalid command"?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    In the driver I have if the function coordinate is true, print the coordinates. Otherwise print invalid command.

  7. #7
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    And what are the conditions for that function to return true or false?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The logic for that driver, is not doing what you think it is, and you don't want a valid command returned with an invalid command, every time, right?

    So add a getchar() right underneath the other getchar(), and pull the newline char off the keyboard buffer, and problem solved.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    if(*row < START||*col < START||*row >= MAXR)
    return FALSE;
    implying that you can't have a row or column less then 0 and that you can't have a row greater or equal to the maximum row.

  10. #10
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Does that have anything to do with the command entered?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    I see what you are getting at, but my if statement does solve one of my problems. However, I have just solved the problem of double printing. while(command = getchar() != '\n'); takes care of it. Thanks for everyone's help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wondering if this would be a dangerous use of globals.
    By suzakugaiden in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 09:51 PM
  2. just wondering
    By sreetvert83 in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2003, 08:24 PM
  3. wondering about people here
    By moemen ahmed in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 07-08-2002, 09:33 PM
  4. Just wondering
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 02-12-2002, 11:14 PM

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