Hi help with homework

Hi guys! I'm very new at programming (but I'm loving it). I probably logged 20 hours of actual programming.

I have questions regarding about my homework. So I'm trying to work on the problem with 21 stone pick up.

The problem I have is that the program won't run to the next while statement. It just runs the first while statement, ignoring the if, and looping again.

How am I suppose to have the program break out of a loop? I've tried return 0, and break after the if statement, but no dice.

Is the return 0, break needed?

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
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int stone, subt, total1, total2, total3;
	string player1, player2;
	stone = 21;
	
	cout << "Please enter player 1's name. ";
	cin >> player1;
	cout << "Please enter player 2's name. ";
	cin >> player2;

	while (stone = 21)
	{ 
		cout << "Please take (1-4) stones." << endl;
		cin >> subt;
		if (subt >= 5 && subt <=0)
			{
				cout << "Invalid choice.";
				break;
			}	
		else 
		total1 = stone - subt;
			{
				cout << "There are " << total1 << " stones remaining.";
			}	
	}
	
	while (total1)
	{
		cout << "Player 2, please take (1-4) stones." << endl;
		cin >> subt;
		if (subt >= 5 && subt <= 0)
			{
				cout << "Invalid choice.";
				return 0;
			}
		else
		total2 = total1 - subt;
		{	
			cout << "There are " << total1 << " stones remaining.";
		}
	}
	
	while (total2)
	{
		cout << "Player 1, please take (1-4) stones." << endl;
		cin >> subt;
		if (subt >= 5 && subt <= 0)
			{
				cout << "Invalid choice.";
				return 0;
			}
		else
		total3 = total2 - subt;
		{	
			cout << "There are " << total1 << " stones remaining.";
		}
	}
	
	system ("pause");
	return 0;
}


Thanks alot guys!!!
The problem is that you never update the value of "stone" and so it never becomes something other than 21, the loop condition is always true, and the loop doesn't terminate and go to the next one.

If you update the value of "stone," rather than just the value of total1, it will end the loop after input.
Hi amaranthboar,

There is quite a bit wrong with your program here. Brenn pointed out why your look is running forever however your while loop statement will never be false.

while (stone = 21)
this statement here is assigning the number 21 to your variable stone.

while (stone == 21)
this statement here is checking to see if stone is equal to the number 21. So it says while stone is equal to 21 I want you to run all this code in my curly braces. Once it equals anything but 21 stop running this while loop.

also inside your while loop you have:

if (subt >= 5 && subt <=0)\

this line of code will never run. It is a fairly common mistake. However you are asking for a number that is equal to or bigger than 5 and a number that is less than or equal to 0. There is no such number so it will never run.

I think what you were trying to say is:

if (subt >= 5 || subt <=0)\

This say or. If subt is bigger than 5 or less than 0.

Hope this helps if you have more questions feel free to ask. Also, explaining what your program is supposed to do will help me in particular i have never written 21 stone.

Cheers!
MRangel- Nice catch with the "stone =" and not "==" Can't believe I missed that. :|

Amaranthboar-There's another issue in the second and third while loops. The "while(total1)" will only end once player2 has taken all of the stones, which I assume is not intended behavior. In addition, your program only allows for 3 turns--player1 in the first loop, player2 in the second, and player1 in the third.
I suggest having one "while(stone > 0)" for the whole function, and then do the player1 turn and player2 turn inside that.
Thank you very much guys for making it easy for me to understand!!! I'm updating the program as I'm typing.

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
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int stone, subt, total1, total2, total3;
	string player1, player2;
	stone = 21;
	
	cout << "Please enter player 1's name. ";
	cin >> player1;
	cout << "Please enter player 2's name. ";
	cin >> player2;

	while (stone == 21)
	{ 
		cout << "Please take (1-4) stones." << endl;
		cin >> subt;
		if (subt >= 5 || subt <=0)
			{
				cout << "Invalid choice.";
			}	
		else 
		total1 = stone - subt;
			{
				cout << "There are " << total1 << " stones remaining.";
			}	
	}
	
	while (total1)
	{
		cout << "Player 2, please take (1-4) stones." << endl;
		cin >> subt;
		if (subt >= 5 || subt <= 0)
			{
				cout << "Invalid choice.";
			}
		else
		total2 = total1 - subt;
		{	
			cout << "There are " << total1 << " stones remaining.";
		}
	}
	
	while (total2)
	{
		cout << "Player 1, please take (1-4) stones." << endl;
		cin >> subt;
		if (subt >= 5 || subt <= 0)
			{
				cout << "Invalid choice.";
			}
		else
		total3 = total2 - subt;
		{	
			cout << "There are " << total1 << " stones remaining.";
		}
	}
	
	system ("pause");
	return 0;
}


So I've updated my program. I've taken out the return 0 and break (as I would need a quick briefing on how it works).

The program still runs on the first loop. I am just wondering, what are some suggestions that I can implement?

Another question ~ I'm looking at the void functions now. I'm not so sure the usages of the void function. It says in the book it's mainly for modular programming, and it seems like it's substitution. My question is; how does this work?

Is my example correct?

#include <iostream>
using namespace std;

void blahblah()
{
cout << "Hello world" << endl;
}

int main()
{
cout << "Please say: " << blahblah() << endl;
}

Thanks!!!







Last edited on
Hi! Im new to these forums but I think I can help you.
When you have your all of your else statements, not everything is inside the curly brace so not all of it is going to run. Everything needs to be inside the curly brace after an else if you want all of it to run.

void functions are basically a way to write a section of code in one place and then use it when ever you want to in int main(). For example:

void ILikeTrains()
{
cout <<" I Like Trains";
}

int main()
{
ILikeTrains();
ILikeTrains();
ILikeTrains();
ILikeTrains();
}

this would run the code within the curley braces of the void function ILikeTrains and so it would cout "I like trains" 4 times
Sorry for such late reply. Thanks tigernillia1111!

My creative brain juice is in the can right now. I FEEL like this can run perfectly fine. Can someone enlighten me on how to make a loop? I'm quite confused with the return 0; what can be substitute into 0?

thanks guys!

[code}
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
int stone, subt1;
string player1, player2;
stone = 21;
player1 = 0;
player2 = 0;

cout << "Please enter player 1's name. ";
cin >> player1;
cout << "Please enter player 2's name. ";
cin >> player2;

while (player1 != 21 && player2 != 21)
{
{
cout << "Game Start!." << endl;
{
//player 1
cout << player1 << ", please take your stones (1-4). ";
cin >> subt1;

if (subt1 >=5 || subt1 <=0)
{
cout << "Invalid choice. Turn skipped";
}
else
{
total1 = subt1 + player1;

if (total1 == 21)
{
cout << "You lose.";
}
else
{
cout << "There are " << stone - subt1 << " stones left." << endl;
}
}

//player 2
cout << player2 << ", please take your stones (1-4). ";
cin >> subt1;
if (subt1 >=5 || subt1 <=0)
{
cout << "Invalid choice. Turn skipped.";
}
else
{
total1 = subt1 + player2;

if (total1 == 21)
{
cout << "You lose.";
}
else
{
cout << "There are " << stone - subt1 << " stones left." << endl;
}
}
}
}
if (player1 == 21)
{
cout << player1 << ", you lose!" << endl;
}
if (player2 == 21)
{
cout << player2 << ", you lose!" << endl;
}
}
system ("pause");
return 0;
}
[/code]
I'm fairly new at this as well and was reading over the comments and the original problem. Does it matter that you/he initializes stone = 21; at the beginning of the program? Wont this make the while condition always true from the get go?
I was just about to question this.

I'm thinking about taking out stone = 21. Or can I place stone = 21 into a void function.

The question I have is how to make the whole program loop back into the start, WHILE continue to subtract from the pile.
well you would have to put the whole thing in a for loop with stone -- to subtract 1 from the pile... if you are trying to subtract x amount from the pile you would have to write that condition in the loop... stone - (user input). if i am wrong... somebody please tell me.
I'm going to try that tomorrow. I'll post up the results.
Topic archived. No new replies allowed.