Thread: Selecting and connect a number.

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    5

    Selecting and connect a number.

    I am building a c program to select and move a number in an array.My aim is to connect 2 pairs of same number in the array. But I am not sure why the selected number be can't moved. Need help, thanks in advance.

    Code:
    
    
    Code:
    void playgame(char box[ROW][COL]) { int x, y, choice2,num,direction=0; char input; do{ printf("Please select a number (1-7) : "); scanf("%i",&num); if(num==0||num >7) { printf("Invalid!\n"); } else { printf("\nNumer %i is currently selected!\n", num); } }while(num==0||num >7); printf("\n[1]Move\n[2]Sign out\n"); printf("\nEnter choice: \n"); scanf("%d",&choice2); switch(choice2) { case1: { printf("Press 'e' to go up\n"); } { printf("Press 'd' to go right\n"); } { printf("Press 's' to go left\n"); } { printf("Press 'x' to go down\n"); } fflush(stdin); scanf("%c",&input); break; case2: printf("Bye!\n"); } for(x=0; x<9; x++) for(y=0; y<9; y++) { if(input =='e') if(box[x][y]==num) { box[--x][y] =num; } if(input =='d') if(box[x][y]==num) { box[x][++y] = num; } if(input =='s') if(box[x][y] == num) { box[x][--y] = num; } if(input =='x') if(box[x][y]==num) { box[++x][y] = num; } } }
    Last edited by META-C; 06-12-2016 at 01:06 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code formatted and indented; please pay attention to make sure your post is readable.
    Code:
    void playgame(char box[ROW][COL])
    {
      int x, y, choice2, num, direction = 0;
      char input;
    
      do {
        printf("Please select a number (1-7) : ");
        scanf("%i", &num);
    
        if (num == 0 || num > 7) {
          printf("Invalid!\n");
        } else {
          printf("\nNumer %i is currently selected!\n", num);
        }
      } while (num == 0 || num > 7);
    
      printf("\n[1]Move\n[2]Sign out\n");
      printf("\nEnter choice: \n");
      scanf("%d", &choice2);
    
      switch (choice2) {
      case 1:
        {
          printf("Press 'e' to go up\n");
        }
        {
          printf("Press 'd' to go right\n");
        }
        {
          printf("Press 's' to go left\n");
        }
        {
          printf("Press 'x' to go down\n");
        }
    
        fflush(stdin);
        scanf("%c", &input);
        break;
    
      case 2:
        printf("Bye!\n");
      }
    
      for (x = 0; x < 9; x++)
        for (y = 0; y < 9; y++) {
          if (input == 'e')
            if (box[x][y] == num) {
              box[--x][y] = num;
            }
          if (input == 'd')
            if (box[x][y] == num) {
              box[x][++y] = num;
            }
          if (input == 's')
            if (box[x][y] == num) {
              box[x][--y] = num;
            }
          if (input == 'x')
            if (box[x][y] == num) {
              box[++x][y] = num;
            }
        }
    }
    > box[--x][y] = num;
    Presumably, you want to actually break out of these loops having moved the number once?
    Doing --x is a sure way of messing up your loop control as well; consider using x-1 instead of --x

    Also, you're not moving the number, since you don't actually clear box[x][y] at the same time.

    > fflush(stdin);
    > scanf("%c", &input);
    Flushing input streams is undefined behaviour.
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    If you want the next non-space character, then use
    scanf(" %c", &input); // note the initial leading space in the scan format.

    > for (x = 0; x < 9; x++)
    You should replace literal 9 with the symbolic constants ROW and COL respectively.
    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.

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    5
    Selecting and connect a number.-q8bnx-jpg

    Hi , what I want is connect the selected number. But I still can't figure it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Selecting 1 struct out of 2
    By yrthilian in forum C Programming
    Replies: 7
    Last Post: 04-28-2011, 02:32 PM
  2. not selecting random number again
    By diego in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2010, 03:45 PM
  3. How to tell what you're selecting?
    By azjherben in forum Game Programming
    Replies: 4
    Last Post: 03-07-2010, 03:55 AM
  4. Selecting an area
    By lord mazdak in forum Windows Programming
    Replies: 1
    Last Post: 10-01-2007, 11:29 PM
  5. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM

Tags for this Thread