How to deduce 100 to zero using nested for loop c ++

advertisements

Basically my program is to deduct 100 by two person until it is reach zero. I'm using nested for loop. 1 loop for each person as the person need to enter an integer. That integer will be deducted with 100.

But the main problem is I can't repeat the 1st loop without losing current value of 100. I'm a beginner.

for( int x = 1; x<=100;)
{
  cout <<"\n\n"<< nP1 <<" how many of the remaining " << chips<<" chip(s) would you like?"<<endl;
  cin >> P1c;
  result = chips-P1c;
  cout <<result;

    for( int y = 1; y <=100;)
    {
    cout <<"\n\n"<< nP2 <<" how many of the remaining " << result<<" chip(s) would you like?"<<endl;
    cin >> P2c;
    result = result-P2c;
    cout <<result;
    break;
    }

Screenshot


For-loops aren't suited for this – "repeat until something is zero", as you're describing the problem, is very different from "repeat one hundred times", as you tried to solve it.

If you were playing this in real life, you would probably do something like this:

while there are any chips left
   let person 1 pick some chips
   if there are still any chips left
       let person 2 pick some chips

Translating to code left as an exercise.