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. 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. 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. 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. Originally Posted by ceptyoubestbud
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

5. 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. Originally Posted by ceptyoubestbud
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").

7. 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. 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. Make your board array in main and pass it as an argument to the functions that need it.

10. Originally Posted by ceptyoubestbud
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. 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. 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.

13. Originally Posted by ceptyoubestbud
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. 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. 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

Page 1 of 2 12 Last