Hi,
so I'm trying to pick up C++ after I gave up a few years ago,
I was just messing around with some code and can't seem to figure out how to fix this problem I''m having, I'll explain below this wall of text:
(My code, I know, it's dumb):
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
string PlayerName;
const int GOLD = 900;
int People;
People = 7;
cout << "please enter your name:"<< endl;
cin >> PlayerName;
cout << "Please enter a number, don't make it too big!" << endl;
cout << "(This determines the size of group you will be sharing your loot with!)"<< endl;
int GroupSize;
cin >> GroupSize;
cout << "Please enter another number, but be sure to make it smaller than the initial number!" << endl;
int Dead;
cin >> Dead;
while (GroupSize <= Dead)
{
cout << "\nHilarious" << endl;
cout << "Just hilarious" << endl;
cout << "Let's try again from scratch, shall we?" << endl;
cout << "\nPlease enter a number, don't make it too big, as it's the group you will be sharing your loot with!"<< endl;
int GroupSize;
cin >> GroupSize;
cout << "Please enter another number, but be sure to make it smaller than the initial number!" << endl;
int Dead;
cin >> Dead;
}
cout << "the group finally found the treasure!" << endl;
cout << "there were 900 shiny pieces of gold to take" << endl;
cout << "however, there were " << People << " in the group"<< endl;
cout << "they had all agreed to split it equally, so therefore"<< endl;
cout << "they all received " << floor (GOLD/People) << " Pieces of gold!"<< endl;
cout << "Well," << ((People + 1) - People) << " Person did not, but who?" << endl;
cout << PlayerName << " craftily took the remaining " << GOLD%People << " gold pieces for himself!"<< endl;
return 0;
}
Now my problem is in the while loop and no, it's not the sarcastic overtone of it.
It's that, lets say I enter 2 for GroupSize, then 3 for Dead, it will enter the while loop, so when I enter 3 for GroupSize and 2 for dead I was expecting it to exit, instead it prints out the same sarcastic quote etc.
It's like it's just reading the values of the initial values first time round, and not reading the newly entered ones in the while loop (hopefully you understand, if not just copy the code, paste and you'll see what I mean).
How could I go about fixing this, I could nest tons of if statements but,
a) no
b) Seriously, no
Could I use loops?
although I haven't taken a look at loops yet and can't remember much.
Does anyone know how to fix this?
*edit*
If you could explain why whatever I have done is causing the issue that would be great, I feel that understanding silly errors is really important, but heh, what do I know.
#include <iostream>
#include <math.h>
usingnamespace std;
int main()
{
string PlayerName;
constint GOLD = 900;
int People;
People = 7;
cout << "please enter your name:"<< endl;
cin >> PlayerName;
cout << "Please enter a number, don't make it too big!" << endl;
cout << "(This determines the size of group you will be sharing your loot with!)"<< endl;
int GroupSize;
cin >> GroupSize;
cout << "Please enter another number, but be sure to make it smaller than the initial number!" << endl;
int Dead;
cin >> Dead;
while (GroupSize <= Dead)
{
cout << "\nHilarious" << endl;
cout << "Just hilarious" << endl;
cout << "Let's try again from scratch, shall we?" << endl;
cout << "\nPlease enter a number, don't make it too big, as it's the group you will be sharing your loot with!"<< endl;
cin >> GroupSize;
cout << "Please enter another number, but be sure to make it smaller than the initial number!" << endl;
cin >> Dead;
}
cout << "the group finally found the treasure!" << endl;
cout << "there were 900 shiny pieces of gold to take" << endl;
cout << "however, there were " << People << " in the group"<< endl;
cout << "they had all agreed to split it equally, so therefore"<< endl;
cout << "they all received " << floor (GOLD/People) << " Pieces of gold!"<< endl;
cout << "Well," << ((People + 1) - People) << " Person did not, but who?" << endl;
cout << PlayerName << " craftily took the remaining " << GOLD%People << " gold pieces for himself!"<< endl;
return 0;
}
The problem was you had written int GroupSize and int Dead twice. Write only once int [integer name].
Hmm, yeah, not quite sure why I did that, so how coke that caused the problem? Was it because the condition for the while loop was looking at the initial values, and by putting the int in again in the loop meant I couldn't actually change those initial values and thus couldn't change the loop?
P.s
im having a bit of trouble with some char stuff, if you could help it'd be great.
I am offering the player a yes or no decision and have coded it like this
Char Decision;
Char Positive ;
Char Negative
Char Both;
Char Neither;
Positive = 'y'
Negative = 'n'
Both = 'y' or 'n';
Neither != Both;
Cin>> Decision
If (Decision = 'y')
{Do this}
Else if ( Decision = 'n')
{Do this}
Else while ( Decision != 'y' or 'n')
{Cout<<"type either y or n" << endl;}
I did try using Negative instead of n, Positive instead of y, etc. But that wouldn't work at all so I reverted to this, I can get it to work if y is hit, if n is hit, but if for example j is hit, rather than going onto the loop of unless you hit y or n you're not going anywhere, it instead does the (Decision = y) bit.
Am I going about this completely incorrectly?