while loop with int variables

This is a discussion on while loop with int variables within the C++ Programming forums, part of the General Programming Boards category; i'm just going to post the while loop portion of the code, because the rest of it above that is ...

  1. #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 10:54 AM.

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

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