Thread: C functions problem

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    8

    C functions problem

    [COLOR=#0000FF][FONT=Consolas]I am trying to add 2 functions to my program. one is displayMap which works ok , but the other is move which is aksing user which way he would like to move the x . i dont know how to pass value of row and col to this function and in the same time pass opt which is the letter which user will choose to move the X .
    Code:
    #include <stdio.h>
    int displayMap(int, int);
    void move();
    int main(void)
    {
      int i, j, row, col;
      char opt;
      int mul;
    
      do {
        printf("\nPlease enter an X coordinate (a number between 1 and 10): \n");
        scanf("%d", &col);
        printf("Please enter an Y coordinate (a number between 1 and 10): \n");
        scanf("%d", &row);
        if (col > 10 || row > 10) {
          printf("Numbers has to be between 1 and 10 \n");
        }
      } while (col > 10 || row > 10);
    
      while (opt != 'x' && opt != 'X') {
        mul = displayMap(col, row);
    
    
        printf("\nDo you want to move the 'X'? \n");
        printf("W - move up\n");
        printf("S - move down\n");
        printf("A - move left\n");
        printf("D - move right\n");
        printf("x - exit\n");
        while ((opt = getchar()) != '\n' && opt != EOF);
        scanf("%c", &opt);
    
        move(opt);
      }
    }
    
    int displayMap(col, row)
    {
      int i = 1, j = 1;
    
      while (i <= 10) {
        while (j <= 10) {
          if (i == row && j == col) {
            printf(" X ");
          } else {
            printf(" - ");
          }
          j++;
        }
        i++;
        j = 1;
        printf("\n");
      }
    }
    
    void move(opt, mul)
    {
      int displayMap(col, row)
      if (opt == 'W' || opt == 'w') {
        row--;
        if (row == 0) {
          row = row + 10;
        }
      } else if (opt == 'S' || opt == 's') {
        row++;
        if (row == 11) {
          row = row - 10;
        }
      } else if (opt == 'A' || opt == 'a') {
        col--;
        if (col == 0) {
          col = col + 10;
        }
      } else if (opt == 'D' || opt == 'd') {
        col++;
        if (col == 11) {
          col = col - 10;
        }
      } else if (opt == 'X' || opt == 'x') {
        printf("Thank You for using my program Bye");
      }
    }
    Last edited by Salem; 06-03-2020 at 10:33 AM. Reason: Removed crayola

  2. #2
    Registered User
    Join Date
    May 2020
    Posts
    8
    i could have a function called displayMap and another one called move (whichtakes in the user’s 'w'/'a'/'s'/'d' input as a parameter).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well,

    int displayMap(int, int);
    void move();

    1. Give move a proper prototype.


    int displayMap(col, row)
    void move(opt, mul)

    2. Make the definitions match the prototypes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions Problem
    By tahmod in forum C Programming
    Replies: 5
    Last Post: 01-07-2012, 02:26 PM
  2. Problem with functions
    By WTFSEAN in forum C Programming
    Replies: 5
    Last Post: 05-10-2008, 02:01 AM
  3. Problem with functions
    By lizardking3 in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2003, 04:34 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Problem with Functions!
    By dizz in forum C++ Programming
    Replies: 22
    Last Post: 11-19-2002, 08:25 PM

Tags for this Thread