Join 132,227 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,052 people online right now. Registration is fast and FREE... Join Now!
This is my first C program. I am trying to implement a NIM game. I am pretty sure I am not using "scanf" right. Any help is appreciated.
CODE
* This program implements a NIM game with normal * win rule. The game starts with three piles * of sticks each containing between 1 and 50 sticks * which are represented with a *. The number of * sticks in each pile is specified by the players. * The two players take turns removing any positive * number of sticks from a pile that is not empty. * The player that takes the last stick wins. The * program refreshes the number of sticks in each pile * after every turn and prints out graphically the number * of sticks in each pile. The program checks for several * different errors throughout. * */
if (pile == 1) { if (pile1 != 0) { pile1 = pile1 A; if (pile1 <= 0) pile1 = 0; }
else printMsg (MSG_8, 1);
}
else if (pile == 2) { if (pile2 != 0) { pile2 = pile2 A; if (pile1 <= 0) pile2 = 0; }
else printMsg (MSG_8, 1);
}
else if (pile == 3) { if (pile3 != 0) { pile3 = pile3 A; if (pile3 <= 0) pile3 = 0; }
else printMsg (MSG_8, 1);
}
else printMsg (MSG_7, 1); }
else { if (player = 1) player = player + 1; else player = player - 1;
printMsg (MSG_1, 1); exit(1); }
return 0; }
/* Function: printPiles() * This function visually prints out the three piles with asterisks * Inputs: the number of sticks in pile1, pile2, pile3 * Output: none */ void printPiles (int pile1, int pile2, int pile3) { /* declare variables */
/* Printing messages: * This function prints out a message corresponding to a message code. * Input: the message code, the player ID for the outputs for a particular player. * Output: 0 if no error, 1 if error. */ int printMsg (int msgCode, int player) {
if ( msgCode < 0 || msgCode > 9) return 1; if ( msgCode == 1 || msgCode == 4 ) if ( player < 1 || player > 2 ) return 1;
switch (msgCode) { case 0: printf("Enter pile sizes (separated by spaces): "); break; case 1: printf("Player %d's turn:\n", player); break; case 2: printf("Pick a pile #: "); break; case 3: printf("Remove how many sticks? "); break; case 4: printf("Congratulations! Player %d is the winner!\n", player); break; case 5: printf("Invalid input, end of game!\n"); break; case 6: printf("Invalid pile sizes. Try again.\n"); break; case 7: printf("Invalid pile. Try again.\n"); break; case 8: printf("That pile is empty. Try again.\n"); break; case 9: printf("You have to take at least one stick!\n"); break; default: break; } return 0; }
I'm not sure. I am getting a lot of errors when i try to compile it. It seems that all of the errors are around where I use scanf.
I fixed the quotes throughout. Still getting a lot of errors. haha
mp3.c:55:9: warning: character constant too long for its type mp3.c: In function main: mp3.c:55: warning: passing argument 1 of scanf makes pointer from integer without a cast mp3.c:57: error: invalid lvalue in assignment mp3.c:64: warning: implicit declaration of function end mp3.c:100:9: warning: multi-character character constant mp3.c:100: warning: passing argument 1 of scanf makes pointer from integer without a cast mp3.c:107:9: warning: multi-character character constant mp3.c:107: warning: passing argument 1 of scanf makes pointer from integer without a cast mp3.c:119: error: stray \342 in program mp3.c:119: error: stray \200 in program mp3.c:119: error: stray \223 in program mp3.c:119: error: expected ; before A mp3.c:134: error: stray \342 in program mp3.c:134: error: stray \200 in program mp3.c:134: error: stray \223 in program mp3.c:134: error: expected ; before A mp3.c:148: error: stray \342 in program mp3.c:148: error: stray \200 in program mp3.c:148: error: stray \223 in program mp3.c:148: error: expected ; before A mp3.c:164: warning: suggest parentheses around assignment used as truth value mp3.c:194:10: warning: character constant too long for its type mp3.c: In function printPiles: mp3.c:194: warning: passing argument 1 of printf makes pointer from integer without a cast mp3.c:197: error: expected expression before ) token mp3.c:202:10: warning: character constant too long for its type mp3.c:202: warning: passing argument 1 of printf makes pointer from integer without a cast mp3.c:205: error: expected expression before ) token mp3.c:210:10: warning: character constant too long for its type mp3.c:210: warning: passing argument 1 of printf makes pointer from integer without a cast mp3.c:213: error: expected expression before ) token
Well, the errors are gone, but important is that you should always code inside a program like notepad or any other simple text editor; do not ever copy your code to and from a word-processor like WORD or Open Office Writer.
The formatting in these programs can create a beginner's nightmare.
And when you want to assign a value to a variable say:
int counter;
counter = pile1; and not pile1 = counter; the latter statement will assign the value of counter to pile1 and not what you actually want to happen.
cpp
/* This program implements a NIM game with normal * win rule. The game starts with three piles * of sticks each containing between 1 and 50 sticks * which are represented with a *. The number of * sticks in each pile is specified by the players. * The two players take turns removing any positive * number of sticks from a pile that is not empty. * The player that takes the last stick wins. The * program refreshes the number of sticks in each pile * after every turn and prints out graphically the number * of sticks in each pile. The program checks for several * different errors throughout. * */
if (pile == 1) { if (pile1 != 0) { pile1 = pile1 - A; if (pile1 <= 0) pile1 = 0; }
else printMsg (MSG_8, 1);
}
else if (pile == 2) { if (pile2 != 0) { pile2 = pile2 - A; if (pile1 <= 0) pile2 = 0; }
else printMsg (MSG_8, 1);
}
else if (pile == 3) { if (pile3 != 0) { pile3 = pile3 - A; if (pile3 <= 0) pile3 = 0; }
else printMsg (MSG_8, 1);
}
else printMsg (MSG_7, 1); }
else { if (player = 1) player = player + 1; else player = player - 1;
printMsg (MSG_1, 1); exit(1); }
return 0; }
/* Function: printPiles() * This function visually prints out the three piles with asterisks * Inputs: the number of sticks in pile1, pile2, pile3 * Output: none */ void printPiles (int pile1, int pile2, int pile3) { /* declare variables */
/* Printing messages: * This function prints out a message corresponding to a message code. * Input: the message code, the player ID for the outputs for a particular player. * Output: 0 if no error, 1 if error. */ int printMsg (int msgCode, int player) {
if ( msgCode < 0 || msgCode > 9) return 1; if ( msgCode == 1 || msgCode == 4 ) if ( player < 1 || player > 2 ) return 1;
switch (msgCode) { case 0: printf("Enter pile sizes (separated by spaces): "); break; case 1: printf("Player %d's turn:\n", player); break; case 2: printf("Pick a pile #: "); break; case 3: printf("Remove how many sticks? "); break; case 4: printf("Congratulations! Player %d is the winner!\n", player); break; case 5: printf("Invalid input, end of game!\n"); break; case 6: printf("Invalid pile sizes. Try again.\n"); break; case 7: printf("Invalid pile. Try again.\n"); break; case 8: printf("That pile is empty. Try again.\n"); break; case 9: printf("You have to take at least one stick!\n"); break; default: break; } return 0; }
The program does not run after the inputs, but that is for some other post.
I am having difficulties using the debugger that was recomended for this assignment. I am pretty sure there is somthing wrong with the program after the first input.
CODE
* This program implements a NIM game with normal * win rule. The game starts with three piles * of sticks each containing between 1 and 50 sticks * which are represented with a *. The number of * sticks in each pile is specified by the players. * The two players take turns removing any positive * number of sticks from a pile that is not empty. * The player that takes the last stick wins. The * program refreshes the number of sticks in each pile * after every turn and prints out graphically the number * of sticks in each pile. The program checks for several * different errors throughout. * */
if (pile == 1) { if (pile1 != 0) { pile1 = pile1 - A; if (pile1 <= 0) pile1 = 0; }
else printMsg (MSG_8, 1);
}
else if (pile == 2) { if (pile2 != 0) { pile2 = pile2 - A; if (pile1 <= 0) pile2 = 0; }
else printMsg (MSG_8, 1);
}
else if (pile == 3) { if (pile3 != 0) { pile3 = pile3 - A; if (pile3 <= 0) pile3 = 0; }
else printMsg (MSG_8, 1);
}
else printMsg (MSG_7, 1); }
else { if (player == 1) player = player + 1; else player = player - 1;
printMsg (MSG_1, 1); exit(1); }
return 0; }
/* Function: printPiles() * This function visually prints out the three piles with asterisks * Inputs: the number of sticks in pile1, pile2, pile3 * Output: none */ void printPiles (int pile1, int pile2, int pile3) { /* declare variables */
/* Printing messages: * This function prints out a message corresponding to a message code. * Input: the message code, the player ID for the outputs for a particular player. * Output: 0 if no error, 1 if error. */ int printMsg (int msgCode, int player) {
if ( msgCode < 0 || msgCode > 9) return 1; if ( msgCode == 1 || msgCode == 4 ) if ( player < 1 || player > 2 ) return 1;
switch (msgCode) { case 0: printf("Enter pile sizes (separated by spaces): "); break; case 1: printf("Player %d's turn:\n", player); break; case 2: printf("Pick a pile #: "); break; case 3: printf("Remove how many sticks? "); break; case 4: printf("Congratulations! Player %d is the winner!\n", player); break; case 5: printf("Invalid input, end of game!\n"); break; case 6: printf("Invalid pile sizes. Try again.\n"); break; case 7: printf("Invalid pile. Try again.\n"); break; case 8: printf("That pile is empty. Try again.\n"); break; case 9: printf("You have to take at least one stick!\n"); break; default: break; } return 0; }
Now, the program exits after it asks for how many sticks to remove. There is obviously a mistake in one of "if" statements. Again, any help is always appreciated
Here is the updated code:
CODE
* This program implements a NIM game with normal * win rule. The game starts with three piles * of sticks each containing between 1 and 50 sticks * which are represented with a *. The number of * sticks in each pile is specified by the players. * The two players take turns removing any positive * number of sticks from a pile that is not empty. * The player that takes the last stick wins. The * program refreshes the number of sticks in each pile * after every turn and prints out graphically the number * of sticks in each pile. The program checks for several * different errors throughout. * */