|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (9 bit)
Join Date: Jul 2001
Posts: 355
|
need some help with a C program from class
I have a loop with IF else statements nested inside of it. If the IF vlaue is false it goes to the Else and says wrong number try again, how do i get it to go back to top of the loop and redo the number entry. Everything works in it until it gets to the inner loop where it does the number entries but that isnt that important right now...The part im wondering about i have bolded. If the wrong number is entered then i need it to jump back to the beginning of the while statement.
By the way this is a pickup 1 type game where there are 21 stones and 2 players picks 1-3 stones, the first to 0 wins the game... (one player is pc controled) here is the coding so far, it is incomplete as of now #include #include #include int main() { char a; int stones = 21; int num_stones = 0; int player = 1; srandom(time(NULL)); printf("Welcome to 21 pickup. We begin with a pile of 21 stones.\n"); printf("Each turn you may choose to remove 1 to 3 stones.\n"); printf("The goal is to be the player to remove the last stone.\n"); printf("Good Luck!\n"); for (; ![]() { printf("Do you want to play a game of Twenty one pickup (y/n) ?> \n"); scanf("%c", &a); if(a == 'Y' || a == 'y') { int player = 1; int num_stones = 0; int stones = 21; printf("Begin Game -- There are %d stones remaining\n", stones); while( stones != 0) { printf(" Player %d, Enter the number of stones you want to remove>\n", player); scanf("%d", &num_stones); if(num_stones == 1 || num_stones == 2 || num_stones == 3) { printf("You selected %d stones\n", num_stones); stones = num_stones - stones; printf("There are %d stones left\n", stones); player = 1 + player; } else printf("You selected an invlaid number, please try again\n"); printf("Player %d, Enter the number of stones you want to remove>\n", player); num_stones = (random() % 3) + 1; if(num_stones == 1 || num_stones == 2 || num_stones == 3) { printf("You selected %d stones\n", num_stones); stones = num_stones - stones; printf("There are %d stones left\n", stones); player = 1 - player; } } } else if(a == 'N' || a == 'n') { printf("Thanks for playing\n"); break; } else { printf("You entered a invalid choice\n"); break; } } return (0); } |
|
|
|
|
|
#2 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,384
|
Put the if-else statement inside of a do-while loop with a boolean variable as the condition. Default as false, and have the else statement set it to true when needed.
__________________
There are two secrets to staying young, being happy, and achieving success. You have to laugh and find humor every day, and you have to have a dream.
|
|
|
|
|
|
#3 |
|
Member (9 bit)
Join Date: Jul 2001
Posts: 355
|
"Put the if-else statement inside of a do-while loop with a boolean variable as the condition. Default as false, and have the else statement set it to true when needed."
Ok...I know the basic structure of the do while loop do { } while (boolean expression); but im not clearly getting what you are saying. Are you talking about adding another loop inside of the while loop like this? while( stones != 0) { printf(" Player %d, Enter the number of stones you want to remove>\n", player); scanf("%d", &num_stones); do { if(num_stones == 1 || num_stones == 2 || num_stones == 3) { printf("You selected %d stones\n", num_stones); stones = num_stones - stones; printf("There are %d stones left\n", stones); player = 1 + player; } else printf("You selected an invlaid number, please try again\n"); } while(...); if this is what you are talking about, what do i need to make the boolean expression and what do i need to do to make the else statement make it true or should i you a else if function? |
|
|
|
|
|
#4 |
|
Member (9 bit)
Join Date: Feb 2005
Posts: 392
|
put all the codes after the "else" inside the "if" condion {}".
ie: ========================= -----edit cut---- .... printf("There are %d stones left\n", stones); player = 1 + player; printf("Player %d, Enter the number of stones you want to remove>\n", player); num_stones = (random() % 3) + 1; if(num_stones == 1 || num_stones == 2 || num_stones == 3) { printf("You selected %d stones\n", num_stones); stones = num_stones - stones; printf("There are %d stones left\n", stones); player = 1 - player; } } } else if(a == 'N' || a == 'n') { printf("Thanks for playing\n"); break; } else { printf("You entered a invalid choice\n"); break; } }// ........if else printf("You selected an invlaid number, please try again\n"); }//.......while return (0); }
__________________
words to live by: others don't know, I know. others know, I know more. others know more, I excel. one shouldnt read this far; above, is meant as an encouragement, translated from a Chinese Proverb. "He who angers you conquers you." : Elizabeth Kenny |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|