C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-07-2010, 11:51 AM   #1
Registered User
 
Join Date: Feb 2010
Posts: 58
while loop with int variables

i'm just going to post the while loop portion of the code, because the rest of it above that is working fine. so before this loop, a player selected a number of chips to start with, then took out how many he wanted, then we enter the loop. so a is the number that he took, and intial_number_of_chips is how many chips there were to start. both are ints. i'll post the code and describe my problem at the end
Code:
b=0
c=0
while (num_chips>1)
{	   
num_chips=initial_number_of_chips-a-b-c;
	cout<<"\n"<<player_2<<"it is your turn, please select how many chips you would like";
	
	cin>>b;
	
	 
	while (b >(num_chips)/2 || b<1)
	{
	cout<<"\nplease enter a different value";
	cin>>b;
	}
	if (b <=(num_chips)/2 || b>1)

	{
	num_chips=initial_number_of_chips-a-b-c;
	cout<<"\nThere are"<<num_chips<<"left";
	
	cout<<"\n"<<player_1<<"it is your turn. please select how many chips you would like";
	cin>>b;
	}
	while (b>(num_chips)/2 || b<1)
	{
	cout<<"\nPlease enter a different value";
	cin>>b;
	}
	if (b<=(num_chips)/2|| b>=1)
	{
	num_chips=initial_number_of_chips-a-b-c;
	cout<< "there are"<<num_chips<<"chips left";
	}
	


}
return 0;
}
i set b=0 and c=0 outside the loop because when i didn't, odd and unpleasant things happened. anyways, the game is that players select chips, they can't take more than half the available amount, and can't take less than 1. the loop is working fine down to the bottom of it, but when it goes back to the top and the user inputs a new value for b, it's returning a value of initial_number_of_chips-a-b (but the new b, and it's throwing out the old one). i need to it to keep the old b, because it needs to keep subtracting from the available chips again and again until it's down to 1. but it's just resetting each time. any ideas on how to alter this code? i'm not worried about the neatness of it right now, just want to get the loop working first.

Last edited by joeman; 02-07-2010 at 11:54 AM.
joeman is offline   Reply With Quote
Old 02-07-2010, 01:33 PM   #2
Registered User
 
NeonBlack's Avatar
 
Join Date: Nov 2007
Posts: 352
If I'm understanding you right, I think you want to do something like
Code:
num_chips=num_chips-a-b-c;
initial_number_of_chips is never modified. That's why it keeps "resetting".

There are a number of other issues here, (what are a and c used for? some logic issues as well) but let's just focus on this first.
__________________
I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo
NeonBlack is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
NEED HELP READING FILE and PRINTING geoffr0 C Programming 4 04-16-2009 05:26 PM
Drawing Program Max_Payne C++ Programming 21 12-21-2007 05:34 PM
30something GSOH seeks help with the basics of a minor programme promsan C Programming 3 05-13-2007 08:55 AM
Game Won't Compile jothesmo C++ Programming 2 04-01-2006 04:24 PM
My graphics library stupid_mutt C Programming 3 11-26-2001 06:05 PM


All times are GMT -6. The time now is 12:09 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22