Thread: Problem in the loop

  1. #1
    Registered User PunchOut's Avatar
    Join Date
    Jun 2008
    Location
    norfolk, va
    Posts
    16

    Problem in the loop

    I am in the early stages of creating a game of fifteen but i am having a problem. When i run this program i can do "up" and "down" all day long, but when i try "left" or "right" or type anything else in for that matter it performs "down".

    I know there is a simple answer to this, but i cant seem to find the problem and its been driving me nuts!

    if you have a better idea of how i should be using functions in this code feel free to let me know

    thanks in advance

    heres the code
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void show(int numbers[][4]);
    void move(char input[]);
    
    main()
    {
      int numbers[4][4]={15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 1, 2, 0};
      int correct[4][4]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
      char input[5];
      int x, y, tmp;
      x=y=3;
      show(numbers);
      move(input);
      while(numbers != correct)
      {
        if(strcmp(input, "up") == 1)
        {
          tmp=numbers[y][x];
          numbers[y][x]=numbers[--y][x];
          numbers[y][x]=tmp;
        } 
        else if(strcmp(input, "down") == 1)
        {
          tmp=numbers[y][x];
          numbers[y][x]=numbers[++y][x];
          numbers[y][x]=tmp;
        }
        else if(strcmp(input, "left") == 1)
        {
          tmp=numbers[y][x];
          numbers[y][x]=numbers[y][--x];
          numbers[y][x]=tmp;
        } 
        else if(strcmp(input, "right") == 1)
        {
          tmp=numbers[y][x];
          numbers[y][x]=numbers[y][++x];
          numbers[y][x]=tmp;
        }
      show(numbers);
      move(input);
      }
    }
    
    
    void show(int num[][4]) /***Display The Layout of the Game***/
    {
      int i, j;
      for(i=0; i <= 3; i++) 
      {
        for(j = 0; j <= 3; j++)
          printf("&#37;5d", num[i][j]);
        printf("\n\n");
      }
    }
    
    
    void move(char input[]) /***inputing what space they want to switch with 0***/
    {
      char c;
      int i;
      printf("Please enter in the space you want to switch with 0 by indicating \"up\", \"down\", \"left\" or \"right\"\n");
      for(i=0; c!='\n'; i++)
        input[i]=(c=getchar());
      input[i]='\0';
    }
    Last edited by PunchOut; 11-29-2008 at 11:13 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    strcmp returns 0 when it matches, not 1
    RTM
    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 PunchOut's Avatar
    Join Date
    Jun 2008
    Location
    norfolk, va
    Posts
    16
    lol, thank you, duh!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM