Like Tree1Likes

Program isnt entering the If statements.

This is a discussion on Program isnt entering the If statements. within the C Programming forums, part of the General Programming Boards category; The program isnt entering the If statements, it doesnt even get the input of (choice). Whats the problem? Code: void ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    8

    Program isnt entering the If statements.

    The program isnt entering the If statements, it doesnt even get the input of (choice). Whats the problem?
    Code:
    void dispbord() /*This displays the game board*/
    {
         char Blk1 = 'A', Blk2 = 'B', Blk3 = 'C', Blk4 = 'D', Blk5 = 'E', Blk6 = 'F', Blk7 = 'G', Blk8 = 'H', Blk9 = 'I', Blk10 = 'J', Blk11 = 'K', Blk12 = 'L', Blk13 = 'M', Blk14 = 'w', Blk15 = 'N', Blk16 = 'O', Blk17 = 'P', Blk18 = 'Q', Blk19 = 'R', Blk20 = 'S', Blk21 = 'T', Blk22 = 'U', Blk23 = 'V', Blk24 = 'W', Blk25 = 'X', Blk26 = 'Y', Blk27 = 'Z';
         char choice;
         
                             
             printf(" %c | %c | %c      %c | %c | %c      %c | %c | %c\n", Blk1, Blk2, Blk3, Blk10, Blk11, Blk12, Blk19, Blk20, Blk21);
             printf("--+---+--+     --+--+---+     --+--+---+\n");
             printf(" %c | %c | %c      %c | %c | %c      %c | %c | %c\n", Blk4, Blk5, Blk6, Blk13, Blk14, Blk15, Blk22, Blk23, Blk24);
             printf("--+---+--+     --+--+---+     --+--+---+\n");
             printf(" %c | %c | %c      %c | %c | %c      %c | %c | %c\n", Blk7, Blk8, Blk9, Blk16, Blk17, Blk18, Blk25, Blk26, Blk27);
             
         
         printf("Where do you want to place your marker?: \n");
         scanf("%c", &choice);
         
         system("PAUSE");
         
         if(choice == 'A' || choice == 'a')
             Blk1 = 'X';   
         else if(choice == 'B' || choice == 'b')
             Blk2 = 'X';
      
         system("PAUSE");  
    }

    After asking "Where do you want to place the marker?", it just says "Please enter any key to continue." and it didnt even get the input of (choice).

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That's because right after you enter your choice it hits system("PAUSE")...
    And then it's going to hit it again right after misplacing your X.

    Also your 27 BLKxx variables... A) contain a corrupted alphabet and B) would be better replaced by an array.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    So I made it like this:
    Code:
    void dispbord() /*This displays the game board*/
    {
    char alpa[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','w','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
         char choice;
         
              
        init_alpa();
          printf(" %c | %c | %c      %c | %c | %c      %c | %c | %c\n", alpa[0], alpa[1], alpa[2], alpa[9], alpa[10], alpa[11], alpa[18], alpa[19],alpa[20]);
             printf("--+---+--+     --+--+---+     --+--+---+\n");
             printf(" %c | %c | %c      %c | %c | %c      %c | %c | %c\n", alpa[3], alpa[4], alpa[5], alpa[12], alpa[13], alpa[14], alpa[21], alpa[22], alpa[23]);
             printf("--+---+--+     --+--+---+     --+--+---+\n");
             printf(" %c | %c | %c      %c | %c | %c      %c | %c | %c\n", alpa[6], alpa[7], alpa[8], alpa[15], alpa[16], alpa[17], alpa[24], alpa[25], alpa[26]);
             printf("\n");
                             
            
             
         printf("Where do you want to place your marker: \n");
         scanf("%s", &choice);
         
         
         if(choice == 'A' || choice == 'a')
         {
            alpa[0] == 'X';
         }   
      
         system("PAUSE");  
    }
    I was wondering, is the:
    Code:
     "if(choice == 'A' || choice == 'a')
         {
            alpa[0] == 'X';
         }   
      "
    acceptable in changing the board?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ceptyoubestbud View Post
    I was wondering, is the:
    Code:
     "if(choice == 'A' || choice == 'a')
         {
            alpa[0] == 'X';
         }   
      "
    acceptable in changing the board?
    If you want to assign to alpa[0], use "=" not "==" (and otherwise that statement is meaningless).

    If you are going to implement a bunch of possible choices here, you might want to use tolower() on "choice" and then a switch() block:

    Code:
    choice = tolower(choice);
    switch (choice) {
        case ('a'):
            alpa[0] = 'X';
            break;
        case ('b'):
            // and so on....
    }
    Clearer and easier
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    So I replaced it with:
    Code:
    choice = tolower(choice);
         switch (choice)
         {
                case 'A': case 'a': 
                     alpa[0] = 'X';
                     break;
                case 'B': case 'b':
                     alpa[1] = 'X';
                     break;
                default: 
                         printf("rawr");
         }
    But how come the board isnt changing??

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ceptyoubestbud View Post
    But how come the board isnt changing??
    Because you have to print it out again. What's on the screen is not mapped to your array; it's a copy that was made when you printf()'d it. Subsequent changes to the original source will not affect the copy.

    There is no way around that using basic I/O, but you can redraw the board and optionally clear the screen first (that requires use of an OS specific system() command such as "clear").
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You would probably be better off putting your user choice in main and allowing your dispboard() function do only the one task of actually displaying the playing board. A good function does only one thing, but does it well...

    Also you can simplify your case statement by using...
    Code:
    switch (tolower(choice))
      { case...
    ... which eliminates the need of the intermediate step.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    Ok, so heres my current code:

    Code:
    void dispbord() /*This displays the game board*/
    {
    char alpa[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','w','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    char choice;
          
              
             printf(" %c | %c | %c      %c | %c | %c      %c | %c | %c\n", alpa[0], alpa[1], alpa[2], alpa[9], alpa[10], alpa[11], alpa[18], alpa[19],alpa[20]);
             printf("--+---+--+     --+--+---+     --+--+---+\n");
             printf(" %c | %c | %c      %c | %c | %c      %c | %c | %c\n", alpa[3], alpa[4], alpa[5], alpa[12], alpa[13], alpa[14], alpa[21], alpa[22], alpa[23]);
             printf("--+---+--+     --+--+---+     --+--+---+\n");
             printf(" %c | %c | %c      %c | %c | %c      %c | %c | %c\n", alpa[6], alpa[7], alpa[8], alpa[15], alpa[16], alpa[17], alpa[24], alpa[25], alpa[26]);
             printf("\n");
                             
            
             
         printf("Where do you want to place your marker: \n");
         scanf("%s", &choice);
         
         playerchoice(choice);
         computerchoice();
           
      
         system("PAUSE");  
    }
    with a function playerchoice() which gets the choice of the player and changes the board into X.

    But now I'm wondering how do I link the board in function playerchoice() with the board in function dispbord() and computerchoice()?

    I was thinking of having an array board that keeps on updating and an initial board, but have no idea how.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Make your board array in main and pass it as an argument to the functions that need it.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ceptyoubestbud View Post
    But now I'm wondering how do I link the board in function playerchoice() with the board in function dispbord() and computerchoice()? I was thinking of having an array board that keeps on updating and an initial board, but have no idea how.
    Ok... here's what you need to do... and I hope you will take this advice seriously...

    You need to sit down and analyse what you're trying to do (tic tac toe) and work out exactly what is needed to play that game (analyse it).
    Then you need to take that analysis and decide what elements your program needs, where and when to make that happen (plan it)
    Next you take your plan --written out on paper-- and transform it into C source code that you can compile (write it)
    Finally, take your beautifully planned piece of software and run it, fixing every little problem you can find (test it).

    I guarantee that if you sit down and work those 4 steps for every program, no matter how small or how ginormous, you'll produce a lot better code with a lot less fuss and muss...

    What you are doing now amounts to guessing... and how's that working out for you?

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    Ur right, thanks for the tip. I have already done an algorithm, but what made my plan/algorithm long is where I ask the user whether he likes to play first, then I would let him play as X. and vice versa, so I separated the two into two separate LONG algorithms.

    And having problems with linking functions, like passing the array to a function that gets the player's choice, and getting that updated array to my main.

    Thanks for the tip, I did take that seriously. . . .
    Is programming really that hard? Cuz I think I'm gonna fail. . . .

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    What tutorials/books are you using? Did you try the ones we have here for a quick overview. Programming is about abstraction and problem solving more than anything else. That is why coding isn't the first step, as Tater likes to point out.

    For instance, it really doesn't matter what player goes first, or if the game is played by 1 vs. comp or comp vs. comp or player vs. player. The comp AI and player code will be the same with only minor differences.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ceptyoubestbud View Post
    Ur right, thanks for the tip. I have already done an algorithm, but what made my plan/algorithm long is where I ask the user whether he likes to play first, then I would let him play as X. and vice versa, so I separated the two into two separate LONG algorithms.

    And having problems with linking functions, like passing the array to a function that gets the player's choice, and getting that updated array to my main.

    Thanks for the tip, I did take that seriously. . . .
    Is programming really that hard? Cuz I think I'm gonna fail. . . .
    Yes programming is hard. If it was easy everybody would be doing it...

    It takes a special mindset --one with minute attention to detail and a logical approach to problem solving-- to be a programmer. Compilers are totally unforgiving and code is equally stupid... you tell it what to do, it will do it... even if it's the wrong thing. You need to think in very tiny blobs and work out details more precisely than most people ever think possible.

    No offense is intended... but if you're having problems and extra study in textbooks and tutorials isn't helping... maybe this isn't your best career choice.

  14. #14
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    Yeah, but its a challenge I'm willing to take. I'll just start from scratch again I guess and slowly make it to the top. I won't quit being a programmer. If you guys could do it then I bet I could do it too.

    I use any book I find in our library, and eBooks. Do you know any books that are good for begginners?

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Did you look at the link I posted for you? Start there. Then you may want to check out:

    Teach Yourself C 21 Days
    Steve Summit Programming Notes
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-23-2010, 05:15 AM
  2. printf statements effecting program
    By Brain_Child in forum C Programming
    Replies: 3
    Last Post: 11-06-2009, 10:57 AM
  3. Program for entering the Ordinal of a Number
    By jugs in forum C Programming
    Replies: 1
    Last Post: 10-13-2009, 04:47 AM
  4. Program Issue, if and else if statements
    By kordric in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2008, 06:46 PM
  5. Simple C Program with Flow Statements
    By rory-uk in forum C Programming
    Replies: 16
    Last Post: 02-06-2008, 01:12 PM

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