I'm just starting out with C hoping to move on to C++ in the future. When I learn languages I normally make up my own examples to help me learn, and as its my first day with C the one I am working on is quite ambitious, but i'm determined to complete it. However all the different blocks of code are confusing my compiler (gcc). It will be easier for me to explain my problem and what I want to do by showing you code, so here goes: Code: #include <stdio.h> int main(); { int points; char decision; char deal; char play; printf ( "Welcome to Ben's blackjack game!\n" ); printf ( "Would you like to play (y or n)\n" ); scanf ( "% ", &play ); if ( play == "y" ) { printf ( "Press d to deal, or press h for help\n" ); scanf ( "% ", &deal ); if ( deal == "h" ) { printf ( "In blackjack, the player aims to get 21 points each deal. They do this by getting cards of different values\n but if the player gets over 21 then they are bust, which means they get 0\n" ); } else { printf ( "Invalid Command\n" ); } } else if ( play == n ) { printf ( "Ok then, bye!\n" ); } else { printf ( "Invalid Command\n" ); } } Obviously there is loads of blocks there and gcc is confused, and I can probably make it more clear and fix it with a bit of fiddling around (it is actually a bit more clear inside kate, some of my spacing was lost in the copy pase :/). But as it is to become more complex I was wondering whether I could change to something like this: Code: #include <stdio.h> int main(); { char deal; char play; printf ( "Welcome to Ben's blackjack game!\n" ); printf ( "Would you like to play (y or n)\n" ); scanf ( "% ", &play ); if ( play == "y" ) { /* a command to run ./game would appear here, if there is such a thing */ } else if ( play == n ) { printf ( "Ok then, bye!\n" ); } else { printf ( "Invalid Command\n" ); } } So is that possible? And if so, how? Thanks, Ben
Try this Code: #include<stdio.h> int main() { int points; char decision; char deal; char play; printf ( "Welcome to Ben's blackjack game!\n" ); printf ( "Would you like to play (y or n)\n" ); scanf ( "%c", &play ); if ( play == 'y' ) { printf ( "Press d to deal, or press h for help\n" ); fflush (stdin); scanf ( "%c", &deal ); if ( deal == 'h' ) { printf ( "In blackjack, the player aims to get 21 points each deal.They do this by getting cards of different values\n but if the player gets over 21 then they are bust \n, which means they get 0\n" ); } else { printf ( "Invalid Command\n" ); } } else if ( play == 'n' ) { printf ( "Ok then, bye!\n" ); } else { printf ( "Invalid Command\n" ); } } For any further doubt just post your doubt or something else?
> gcc is confused Hmm. Try using indentation properly if the problem is that "the compiler" is confused about code blocks. You'll probably find the problem becomes immediately apparent. Code: #include <stdio.h> int main(); { int points; char decision; char deal; char play; printf ( "Welcome to Ben's blackjack game!\n" ); printf ( "Would you like to play (y or n)\n" ); scanf ( "% ", &play ); if ( play == "y" ) { printf ( "Press d to deal, or press h for help\n" ); scanf ( "% ", &deal ); if ( deal == "h" ) { printf ( "In blackjack, the player aims to get 21 points each deal. They do this by getting cards of different values\n but if the player gets over 21 then they are bust, which means they get 0\n" ); } else { printf ( "Invalid Command\n" ); } } else if ( play == n ) { printf ( "Ok then, bye!\n" ); } else { printf ( "Invalid Command\n" ); } } > Obviously there is loads of blocks there No, not really. I counted six. The compiler can handle waaaaaaaaay more than that before it starts to get confused. > /* a command to run ./game would appear here, if there is such a thing */ > So is that possible? And if so, how? Yes, but that won't solve your problem. Looking a little closer: Code: char play; if ( play == "y" ) else if ( play == n ) Several things wrong here. (1) In C and C++, string (character array) comparison is not done with ==; you have to use strcmp. (2) play isn't a string anyway so it won't compare with "y" even with strcmp. play is a single character so it will compare with 'y' and for this you can use ==. (3) n is not defined so play==n will throw an error; do you mean play=='n'? Code: scanf ( "% ", &play ); I haven't seen percent-space before. What does that do? Did you mean %c?
/me begs for mercy. I had more indentation in kate, but when I copied here it went away... . I probably did mean %c because I wasn't sure what went after the % in this scenario so I left it blank until I found out. Anyway, thanks for your replies, I think I sorted it now =]. Ben