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.
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.
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.
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.
#include "stdafx.h"
#include <iostream>
#include <string>
usingnamespace 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?
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;
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?
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.