if problem

This is a discussion on if problem within the C Programming forums, part of the General Programming Boards category; me and a friend have been trying to find a solution to this for the past day and still have ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    10

    if problem

    me and a friend have been trying to find a solution to this for the past day and still have got no where

    Code:
      if(grid[x][y]) /* if mine */ 
       
    	printf("Sorry You Hit a Mine Game Over\n"); /*Text Displayed*/
        
         else 
    
        display();				/* display grid */
         printf("Well done You now have 10 points Try again\n");
          printf("\n");
    Thats the code.

    Basically i'm looking for the code or help with the code so that the program will end if you hit a mine. I've tried exit and end if but the program won't compile. Just wondering if anyone can help.

    We just need to it to end if it hits a mine or continue if it does not.

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    you need to include stdlib.h to use exit(), but you can also use return;
    Code:
    if (mine)
    {
      printf("Mine\n");
      return;
    }
    else
    printf("No Mine\n");

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    you can always use: exit(0);
    this will end the program immediatly

    Code:
    if(grid[x][y]) {        /* if mine */ 
        printf("Sorry You Hit a Mine Game Over\n"); 
        exit(0);
    } else {
        display();		 display grid*/
         printf("Well done You now have 10 points Try again\n");
         printf("\n");
    }
    edit: yep, yo can use return also..

    and since you probably use a while loop, you can use: "break;" too..

    edit: forgot the braces as braincell pointed out..
    Last edited by zuiplap; 05-05-2004 at 04:08 AM.

  4. #4
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    will you post the errors and the code too?? it would make things way easier and we might even find more errors or bad practices.

    i see two errors here :
    Code:
     if(grid[x][y])
    i can't see an expression here nor a statement that would yield a TRUE\FALSE (aka : 1\0) result. (someone correct me if im wrong)


    the other one :
    Code:
    else{
    
        display();				/* display grid */
         printf("Well done You now have 10 points Try again\n");
          printf("\n");
          }
    you forgot to add braces here(shown in red) to tell the compiler that those three lines of code are for that "else".

    and btw... you can use two new lines characters '\n' in one 'printf()' as :
    Code:
    printf("Well done You now have 10 points Try again\n\n");
    no need to use another 'printf()' to print a new line only.

    and for exiting the program , you could use a 'return' this way :
    Code:
    if(grid[x][y]){ /* if mine */ 
       
    	printf("Sorry You Hit a Mine Game Over\n"); /*Text Displayed*/
            return 0;
             }
    and again...you need the braces..


    hope this helped..
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    if (grid[x][y]) is a condition equivilent to if (grid[x][y] != 0), depending on the type at least...

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    if (grid[x][y]) is a condition equivilent to if (grid[x][y] != 0), depending on the type at least...
    thanks for the correction..
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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