Go Back   PCMech Forums > Help & Discussion > Software Discussion & Support

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 10-25-2005, 08:00 PM   #1
Member (9 bit)
 
ironzombie99's Avatar
 
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);
}
ironzombie99 is offline   Reply With Quote
Old 10-25-2005, 10:16 PM   #2
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
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.
Force Flow is offline   Reply With Quote
Old 10-26-2005, 12:23 AM   #3
Member (9 bit)
 
ironzombie99's Avatar
 
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?
ironzombie99 is offline   Reply With Quote
Old 10-26-2005, 05:05 AM   #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
alfie2 is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:57 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2