Loops

I'm having an issue with my program. I would appreciate all the help I can get.

I wrote out the program. When I run it ... It will propmted for 2 names, ask how many chips I want, and then display congrats player2 name, you win.
I'm very lost at the moment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 #include <iostream>
 #include <iomanip>

 using namespace std;

 int main()
 {
 
 int remainingChips, chipPile, currentChips, Chips = 0;
  
 string player1, player2, currentPlayer;



 // Rules of game
 
 cout << "Rules: The game starts with a pile of chips. Each player may only take at\n"; 
 cout << "most half of the chips. The player that gets the last chip wins. Good Luck!\n";


		  
 // ask player 1 for name
 
 cout << "\nPlayer 1 please enter your name: ";
 cin >> player1;
 

 // ask player 2 for name
 
 cout << "Player 2 please enter your name: ";
 cin >> player2;  
  


 //set the current player to player 1
 
 currentPlayer = player1;

 if( currentPlayer == player1)
   {
   currentPlayer = player2;
   }
   else
     {
     currentPlayer = player1;
	 }

 // ask players how many chips they want to start with
 // ask player 1 how many chips they want from the remaining 200 max 100
 // ask player 2 how many chips they want from the remaining chips max half of remaining chips
 
 cout << "\nHow many chips would you like to start with? ";
 cin >> chipPile; 
 

 
  while (Chips > 1)
    {
	  if ( chipPile % 2 == 1 )
         {
		 cout << endl << currentPlayer << " how many of the remaining " << chipPile << " chip(s) would you like to take (" << remainingChips << " max)?";
         cin >> currentChips;
		 }
	  else 
	     {
		 cout << endl << currentPlayer << " how many of the remaining " << chipPile << " chip(s) would you like to take (" << remainingChips << " max)?";
         cin >> currentChips;
		 }
	
      if( currentChips >= remainingChips / 2 )
        {
        Chips = Chips - currentChips;
		chipPile ++;
        }
    }

 if (chipPile % 2 != 0)
   {
   cout << "Congratulations " << player1 << "! You won!";
   }
 else
   {
   cout << "Congratulations " << player2 << "! You won!";
   }
       	     	 
 return 0;
 }
Couple of major things I'll point out, though I think there are some other issues too:

1) You never give Chips a value other than 0. You need to get user input for that or just hardcode it yourself.

2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
currentPlayer = player1;

 if( currentPlayer == player1)
   {
   currentPlayer = player2;
   }
   else
     {
     currentPlayer = player1;
	 }

 // ask players how many chips they want to start with
 
 cout << "\nHow many chips would you like to start with? ";
 cin >> chipPile; 


This is massively overcomplicated and doesn't do what you want. You can just ask them without using currentPlayer or any if statements. You could even just ask them when you ask them for their name.

Topic archived. No new replies allowed.