generating linear equation with one unknown (c++) - c++

Im sitting here for two hours and my mind is blowing. Im trying to code a generator for linear equation with one unknown. It has 3 combinations:
ax+b=c (ex. 2x-3=1)
a+bx=c (ex. 3-2x=1)
a+b=cx (ex. 4-6=2x)
So i did some simple math, made an algorithm (x=(c-b)/a), excluded zero from denominator etc. The whole point is that to make the result as integer. That's why there is this code:
while (((c - b) % (a) != 0))
{
do
{
a = rand() % 24 - 12;
} while (a == 0);
b = rand() % 24 - 12;
c = rand() % 24 - 12;
}
First two combinations are working perfectly, but third one isn't. The example of an output from the third one is:
5-7=6x AND the calculated solution is x = 2. It would be x=2 if there was a + between 5 and 7. And it always goes this way. This one digit is wrong. The problem situated not in printing in the console, but in calculations.
If you have any idea, please help me to solve this.
Here is the code:
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int level_1()
{
/* LEVEL 1 - ROWNANIE Z 1 NIEWIADOMA*/
int a = 5, b = 123, c = 32;
double x;
double answer;
double licznik, mianownik;
cout << "level 1" << endl;
// losujemy kombinacje (1 z 3) dla roznorodnosci
int losowanie = 3;//= rand() % 3 + 1;
if (losowanie == 1) {
cout << "Rolled: 1" << endl << endl; // x jest przy wspolczynniku a
a = 5;
while (((c - b) % (a) != 0))
{
do
{
a = rand() % 24 - 12;
} while (a == 0);
b = rand() % 24 - 12;
c = rand() % 24 - 12;
}
if (a == 1) cout << "x";
else if (a == -1) cout << "-x";
else cout << a << "x";
if (b > 0) cout << "+" << b;
else if (b == 0) cout << "";
else cout << b;
cout << "=";
if (c >= 0) cout << c;
else cout << c;
x = (c - b) / a;
cout << endl << "Type x = ";
cin >> answer;
if (answer == x)
{
cout << endl << "good answer" << endl;
return 1;
}
cout << endl << "bad answer." << endl;
return 0;
}
else if (losowanie == 2){
cout << "Rolled: 2" << endl << endl; // x jest przy wspolczynniku b
a = 5;
while (((c - b) % (a) != 0))
{
do
{
a = rand() % 24 - 12;
} while (a == 0);
b = rand() % 24 - 12;
c = rand() % 24 - 12;
}
if (b!=0)
cout << b;
if (a < 0 || a == 0) cout << "";
else if (a>0) cout << "+";
if (a == 1) cout << "x";
else if (a == -1) cout << "-x";
else cout << a << "x";
cout << "=" << c;
x = (c - b) / a;
cout << endl << "Type x = ";
cin >> answer;
if (answer == x)
{
cout << endl << "good answer" << endl;
return 1;
}
cout << endl << "bad answer." << endl;
return 0;
}
else
{
cout << "You rolled: 3" << endl << endl; // x jest przy wspolczynniku c
a = 5;
while (((c - b) % (a) != 0))
{
do
{
a = rand() % 24 - 12;
} while (a == 0);
b = rand() % 24 - 12;
c = rand() % 24 - 12;
}
cout << a << endl << b << endl << c << endl;
if (c != 0) cout << c;
if (b != 0)
{
if (b > 0) cout << "+";
else cout << "";
cout << b;
}
cout << "=";
if (a == 1) cout << "x";
else if (a == -1) cout << "-x";
else cout << a << "x";
x = ((c - b) / a);
cout << endl<< "zzz" << x << endl;
cout << endl << "type x = ";
cin >> answer;
if (answer == x)
{
cout << endl << "good answer" << endl;
return 1;
}
cout << endl << "bad answer." << endl;
return 0;
}
return 1;
}
int main()
{
//cout << y << endl;
srand(time(NULL));
while (1){
level_1();
}
}

x=(c-b)/a only holds true for equations of the first form. The other ones would be x = (c-a)/b and x = (a+b)/c. You should change your while loops accordingly.

Related

Recursive functions. Calculate the definite integral using the Newton-Leibniz formula. C++

Given: two real numbers a, b (a < b) and a positive integer
n. Calculate the definite integral using the Newton-Leibniz formula:
a(top) ∫ b(bottom) (f(x,n)dx = F(b,n)-F(a,n)
where F(x,n) is the first-order (antiderivative, inverse derivative, primitive function, primitive integral) function f(x,n):
F(x, n) = integral(f(x,n)dx)
Implement the calculation of F(x,n) in two ways:
as a recursive.
as a nonrecursive function.
That's what needs to solve. Click on the link to open the photo.
F(x,n) = ∫(ctg^n*xdx)
I solved this problem using the pow function. My teacher asks me to solve this problem without using the pow function. Help me with the solution without using the pow function.
Recursion solution
// Recursion
double F(double x, int n)
{
if (n == 0)
return x;
else if (n == 1)
return log(fabs(sin(x)));
else
return -pow(cos(x) / sin(x), n - 1) / (n - 1) - F(x, n - 2);
}
Solving with a loop
// For loop
double F_for(double x, int n)
{
if (n == 0)
return x;
else if (n == 1)
return log(fabs(sin(x)));
else
{
double F1 = x;
double F2 = log(fabs(sin(x)));
double res = 0.0;
for (int i = 2; i <= n; i++)
{
res = -pow(cos(x) / sin(x), i - 1) / (i - 1) - F1;
F1 = F2;
F2 = res;
}
return res;
}
}
main function
int main()
{
double a, b;
cout << "Enter a: ";
cin >> a;
cout << "Enter b: ";
cin >> b;
int n;
cout << "Enter n: ";
cin >> n;
cout << "Recursion: " << F(b, n) - F(a, n) << std::endl;
cout << "A loop: " << F_for(b, n) - F_for(a, n);
return 0;
}
P.S.
Help me solve it without using the pow function, please.
I already wrote my function "my_power".
My instructor said: "You can't use a power function, neither the standard one (pow) nor your own (power). The reason is that any use of such functions leads to a loop, and your loop, or the one in the standard function, are details. A recursive function is itself a loop; using a variable-increment results in a loop - that's not efficient."
double power(double x, int y)
{
double temp;
if (y == 0)
return 1;
temp = power(x, y / 2);
if ((y % 2) == 0) {
return temp * temp;
}
else {
if (y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
I also tested this function
void test_my_power()
{
cout << "pow" << pow(2, -3) << "0" << endl;
cout << "power" << power(2, -3) << "0" << endl;
cout << endl;
cout << "pow" << pow(0, -3) << "0" << endl;
cout << "power" << power(0, -3) << "0" << endl;
cout << endl;
cout << "pow" << pow(0, 1) << "0" << endl;
cout << "power" << power(0, 1) << "0" << endl;
cout << endl;
cout << "pow" << pow(0, -1) << "0" << endl;
cout << "power" << power(0, -1) << "0" << endl;
cout << endl;
cout << "pow" << pow(8, 4) << "0" << endl;
cout << "power" << power(8, 4) << "0" << endl;
cout << endl;
cout << "pow" << pow(11, 3) << "0" << endl;
cout << "power" << power(11, 3) << "0" << endl;
cout << endl;
cout << "pow" << pow(5, 6) << "0" << endl;
cout << "power" << power(5, 6) << "0" << endl;
cout << endl;
}
Your teacher probably wants you to compute pow on the fly as you iterate the loop.
This code:
const auto t = cos(x) / sin(x);
for(int i = 2; i <= n; ++i) {
const auto val = pow(t, i - 1);
...
}
is equivalent to:
auto power = t;
for(int i = 2; i <= n; ++i) {
const auto val = power;
...
power *= t;
}
and doesn't use pow.

c++: variable 'f' not initialized

I'm creating a text-based Risk game on C++. Been trying to implement the dice / battle function but it's been giving me the error "f is used without being initialized!" Which is weird because I initialized it earlier. Here's my code
void battle(int attack, int defend)
{
int a, b, c, d, e, f;
if (attack >= 3)
{
a = rollDice();
b = rollDice();
c = rollDice();
}
else if (attack == 2)
{
a = rollDice();
b = rollDice();
c = 0;
}
else if (attack == 1)
{
a = rollDice();
b = 0;
c = 0;
}
else if (defend >= 2)
{
d = rollDice();
e = rollDice();
f = 0;
}
else if (defend == 1)
{
d = rollDice();
e = 0;
f = 0;
}
sortValues(a, b, c);
sortValues(d, e, f);
cout << endl << "The attacking country rolled the following dices: " << a
<< " " << b << " " << c << ".";
cout << endl << "The defending country rolled the following dices: " << d
<< " " << e << " " << f << ".";
if (a == d)
{
attack = attack - 1;
}
else if (a != d)
{
if (a < d)
{
attack = attack - 1;
}
else if (a > d)
{
defend = defend - 1;
}
}
else if (b == e)
{
attack = attack - 1;
}
else if (b != e)
{
if (b < e)
{
attack = attack - 1;
}
else if (b > e)
{
defend = defend - 1;
}
}
// since the int f is never used, this function is critical to implement. so that we can avoid compile time error
_unused(f);
}
int main()
{
int attack;
int defend;
int choice;
// this asks for the user choice of what he would like to do.
cout << "How large is the attacking army?" << endl;
cin >> attack;
cout << "How large is the defending army?" << endl;
cin >> defend;
while (attack >= 1 && defend >= 1)
{
cout
<< "Based on the numbers you entered, you have a couple of choices: "
<< endl << endl;
cout << "1) Battle once" << endl;
cout << "2) Battle until the end" << endl;
cout << "3) Withdraw from the battle" << endl;
cout << "Please enter your game choice (just enter the number): "
<< endl;
cin >> choice;
if (choice == 1)
{
battle(attack, defend);
}
if (choice == 2)
{
do
{
battle(attack, defend);
} while (attack != 0 && defend != 0);
cout << "Battle ended. The attacking forces are: " << attack
<< " , while the defending forces are: " << defend << endl;
}
if (choice == 3)
{
break;
}
}
if (attack < 1)
{
cout
<< "The attacking forces have lost. Defending forces keep their territory"
<< endl;
}
else if (defend < 1)
{
cout << "The attacking forces have won over the territory." << endl;
}
system("pause");
return 0;
}
This isn't all of the code, but it's the critical section which includes and uses f.
In the battle function you are calling sortValues(d,e,f), but in the first 3 if-cases f does not get a value (thus is not initialized).
Actually you should get a similar error (is it really an error or a warning?) for d and in some of the cases you do not give a value to a,b and c.

Is there a variable, that can hold letters and decimals c++?

I am making a program, that can calculate a triangle if given some information but if I try to use "letters" or "," then, the program glitches and the program needs to be restarted. I am currently using double to hold the numbers and decimals but one of the rules with double is that you need to use "." for decimals, but i've hit the "," so much so I want to know if there is a way to fix it. I also want to know if the person can choose to write both numbers and letters in the same box.
#include <iostream>
#include <windows.h>
#include <math.h>
#define PI 3.14159265359
using namespace std;
void SetColor(int ForgC);
int failure = 0;
int wait;
double a, b, c, A, B, C = 90;
int main()
{
SetColor(10);
{
info:
while (GetAsyncKeyState(VK_RETURN))
{
}
system("cls");
cout << "\n\t\t\t\tFind triangle info\n" << endl;
SetColor(11);
cout << "\t\t\t\tNote: Use 0 if you don't have the number!" << endl;
SetColor(10);
cout << "What information do you have?";
SetColor(11);
cout << "\tIf you use decimal numbers then use \".\" not \",\"!";
SetColor(10);
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "c = ";
cin >> c;
cout << "A = ";
cin >> A;
cout << "B = ";
cin >> B;
while (GetAsyncKeyState(VK_RETURN))
{
}
}
{
calculate:
if (a == 0 && b == 0 && c == 0 && A == 0 && B == 0)
{
cout << "There need to be at least two numbers!\n" << endl;
system("pause");
failure = 1;
} // Failsafe
else if (c != 0)
{
if (a >= c || b >= c)
{
cout << "a or b cannot be greater than c!\n" << endl;
system("pause");
failure = 1;
}
} // Failsafe 2
else if (A >= 90 || B >= 90)
{
cout << "A or B cannot be equal to, or greater than 90!\n" << endl;
system("pause");
failure = 1;
;
} // Failsafe 3
if (a != 0 && c != 0)
{
b = sqrt(c * c - a * a);
A = asin(a / c) * 180 / PI;
B = 90 - A;
}
else if (b != 0 && c != 0)
{
a = sqrt(c * c - b * b);
A = acos(b / c) * 180 / PI;
B = 90 - A;
}
else if (a != 0 && b != 0)
{
c = sqrt(a * a + b * b);
A = atan(a / b) * 180 / PI;
B = 90 - A;
}
else if (c != 0 && A != 0)
{
a = c * sin(A);
b = c * cos(A);
B = 90 - A;
}
else if (c != 0 && B != 0)
{
A = 90 - B;
a = c * sin(A);
b = c * cos(A);
}
else if (a != 0 && B != 0)
{
b = a * tan(B);
c = sqrt(a * a + b * b);
A = 90 - B;
}
else if (b != 0 && A != 0)
{
a = b * tan(A);
c = sqrt(a * a + b * b);
B = 90 - A;
}
else if (a != 0 && A != 0)
{
b = a / tan(A);
c = sqrt(a * a + b * b);
B = 90 - A;
}
else if (b != 0 && B != 0)
{
a = b / tan(B);
c = sqrt(a * a + b * b);
A = 90 - B;
}
if (failure == 1)
{
goto failsafe;
}
goto answer;
}
{
failsafe:
failure = 0;
system("cls");
cout << "\n\t\t\t\tFind triangle info\n" << endl;
SetColor(11);
cout << "\t\t\t\tNote: Use 0 if you don't have the number!" << endl;
SetColor(10);
cout << "What information do you have?";
SetColor(11);
cout << "\tIf you use decimal numbers then use \".\" not \",\"!";
SetColor(10);
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "c = ";
cin >> c;
cout << "A = ";
cin >> A;
cout << "B = ";
cin >> B;
while (GetAsyncKeyState(VK_RETURN))
{
}
goto calculate;
}
answer:
while (1)
{
system("cls");
wait = 1;
cout << "\n\t\t\t\tFind triangle info\n" << endl;
SetColor(11);
cout << "\t\t\t\tNote: Use 0 if you don't have the number!" << endl;
SetColor(10);
cout << "What information do you have?";
SetColor(11);
cout << "\tIf you use decimal numbers then use \".\" not \",\"!";
SetColor(10);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << "A = " << A << endl;
cout << "B = " << B << endl;
cout << "C = " << C << endl;
cout << "\nDo you want to write new information? (";
SetColor(9);
cout << "Y";
SetColor(10);
cout << "/N)";
while (wait == 1)
{
if (GetAsyncKeyState(VK_RIGHT))
{
wait = 0;
}
else if (GetAsyncKeyState(VK_RETURN))
{
goto info;
}
}
system("cls");
wait = 1;
cout << "\n\t\t\t\tFind triangle info\n" << endl;
SetColor(11);
cout << "\t\t\t\tNote: Use 0 if you don't have the number!" << endl;
SetColor(10);
cout << "What information do you have?";
SetColor(11);
cout << "\tIf you use decimal numbers then use \".\" not \",\"!";
SetColor(10);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << "A = " << A << endl;
cout << "B = " << B << endl;
cout << "C = " << C << endl;
cout << "\nDo you want to write new information? (Y/";
SetColor(9);
cout << "N";
SetColor(10);
cout << ")";
while (wait == 1)
{
if (GetAsyncKeyState(VK_LEFT))
{
wait = 0;
}
else if (GetAsyncKeyState(VK_RETURN))
{
return 0;
}
}
}
}
void SetColor(int ForgC)
{
WORD wColor;
// We will need this handle to get the current background attribute
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
// We use csbi for the wAttributes word.
if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
// Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}
You need to add error handling.
If input fails, you need to decide what to do. E.g. you can print an error message, clear the stream state and start over:
std::cin.exceptions(std::ios::failbit);
try
{
std::cout << "a = ";
std::cin >> a;
std::cout << "b = ";
std::cin >> b;
std::cout << "c = ";
std::cin >> c;
std::cout << "A = ";
std::cin >> A;
std::cout << "B = ";
std::cin >> B;
} catch(std::exception const& e)
{
std::cerr << "Error in input.\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max());
goto info;
}
May I heartily suggest not using goto for program flow (use loops). And you may also use non-exception error checking:
std::cout << "a b c A B = ";
if (!(std::cin >> a >> b >> c >> A >> B))
{
if (std::cin.eof())
{
std::cerr << "Goodbye\n";
exit(1);
}
std::cerr << "Error in input.\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max());
goto info;
}
PS. For the numeric_limits traits, include
#include <limits>
Apart from the usual tricky error handling with text input, in particular on the console, and the funny gotos: In order to change number formats (and others as well) you probably need to "imbue a locale" on cin (and, for consistency, on cout). I have never done it but this site has examples which look reasonable and simple: http://www.linuxtopia.org/online_books/programming_books/c++_practical_programming/c++_practical_programming_101.html

Program randomly adds to variable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
my program seems to be running fine but at certain points the variable healthone seems to be added on too instead of being subtracted from like its supposed to be although there isnt a line in this code to adding to healtone.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
float healthone, healthtwo, ending;
int attack, move, random, chance, comprandom, attackp, attackcp, rando;
char c;
string comattackname;
cout << "Do you want to be Naruto or Goku? [n or g]: ";
cin >> c;
cout << " n";
cout << "Naruto and Goku enter the ring!" << endl;
cout << " n";
healthone = 23;
healthtwo = 23;
ending = 10;
while (ending >= 0) {
if (c == 'n') {
cout << "What move do you want to use?" << endl;
cout << "1 - Tail Beast Bomb" << endl;
cout << "2 - Rasengan Shurikan Barrage" << endl;
cout << "3 - Giant Rassengan Barrage" << endl;
cout << "4 - Rassengan Barrage" << endl;
cout << "5 - Rassengan Shuriken" << endl;
cout << "6 - Rasengan" << endl;
cout << "7 - Direct Punch" << endl;
cout << "8 - Shuriken" << endl;
cout << "9 - Enter Sage Mode" << endl;
cout << "10 - Enter Limited Tail Beast Mode" << endl;
cout << "11 - Enter 9 Tails Mode" << endl;
cout << "-";
cin >> move;
}
if (c == 'g') {
cout << "What move do you want to use?" << endl;
cout << "1 - Universal Spirit Bomb" << endl;
cout << "2 - Super Spirit Bomb" << endl;
cout << "3 - Continuos Kamehameha" << endl;
cout << "4 - Super Kamehameha" << endl;
cout << "5 - Kamehameha" << endl;
cout << "6 - Destructo Disk" << endl;
cout << "7 - Direct Punch" << endl;
cout << "8 - Ki Blast" << endl;
cout << "9 - Enter Sage Mode" << endl;
cout << "10 - Enter Limited Tail Beast Mode" << endl;
cout << "11 - Enter 9 Tails Mode" << endl;
cout << "-";
cin >> move;
}
if (move == 8) {
attackp = 1;
rando = 23;
}
if (move == 7) {
attackp = 2;
rando = 17;
}
if (move == 6) {
attackp = 3;
rando = 12;
}
if (move == 5) {
attackp = 5;
rando = 8;
}
if (move == 4) {
attackp = 8;
rando = 5;
}
if (move == 3) {
attackp = 12;
rando = 3;
}
if (move == 2) {
attackp = 17;
rando = 2;
}
if (move == 1) {
attackp = 23;
rando = 1;
}
chance = 1 + rand() % 23;
if (chance <= rando) {
cout << " " << endl;
cout << "Hit! Dealing " << attackp << " Damage!" << endl;
healthtwo = healthtwo - attackp;
} else {
cout << " " << endl;
cout << "Miss!" << endl;
}
comprandom = 1 + rand() % 8;
if (comprandom == 8) {
attackcp = 1;
rando = 23;
}
if (comprandom == 7) {
attackcp = 2;
rando = 17;
}
if (comprandom == 6) {
attackcp = 3;
rando = 12;
}
if (comprandom == 5) {
attackcp = 5;
rando = 8;
}
if (comprandom == 4) {
attackcp = 8;
rando = 5;
}
if (comprandom == 3) {
attackcp = 12;
rando = 3;
}
if (comprandom == 2) {
attackcp = 17;
rando = 2;
}
if (comprandom == 1) {
attackcp = 23;
rando = 1;
}
if (c = 'n') {
if (comprandom == 1) {
comattackname = "Tail Beast Bomb";
}
if (comprandom == 2) {
comattackname = "Rasengan Shurikan Barrage";
}
if (comprandom == 3) {
comattackname = "Giant Rassengan Barrage";
}
if (comprandom == 4) {
comattackname = "Rassengan Barrage";
}
if (comprandom == 5) {
comattackname = "Rassengan Shuriken";
}
if (comprandom == 6) {
comattackname = "Rasengan";
}
if (comprandom == 7) {
comattackname = "Direct Punch";
}
if (comprandom == 8) {
comattackname = "Shuriken";
}
} else if (comprandom == 1) {
comattackname = "Universal Spirit Bomb";
}
if (comprandom == 2) {
comattackname = "Super Spirit Bomb";
}
if (comprandom == 3) {
comattackname = "Continuos Kamehameha";
}
if (comprandom == 4) {
comattackname = "Super Kamehameha";
}
if (comprandom == 5) {
comattackname = "Kamehameha";
}
if (comprandom == 6) {
comattackname = "Destructo Disk";
}
if (comprandom == 7) {
comattackname = "Direct Punch";
}
if (comprandom == 8) {
comattackname = "Ki Blast";
}
chance = 1 + rand() % 23;
if (chance <= rando) {
cout << "Comp Used " << comattackname << "!" << " Hit!" << " Dealing " << attackcp << " Damage" << endl;
cout << " " << endl;
healthone = healthone - attackcp;
} else {
cout << "Comp Used " << comattackname << "!" << " Miss!" << endl;
cout << " " << endl;
}
cout << "Your health: " << healthone << endl;
cout << "Comp health: " << healthtwo << endl;
cout << " " << endl;
if (healthone > healthtwo) {
ending = healthone;
}
if (healthone < healthtwo) {
ending = healthtwo;
}
if (healthone = healthtwo) {
ending = healthone;
}
if (healthone <= 0) {
cout << "You loose." << endl;
cout << " " << endl;
}
if (healthtwo <= 0) {
cout << "You win." << endl;
cout << " " << endl;
}
}
}
This line is your problem:
if (healthone=healthtwo)
{
ending=healthone;
}
Change it to:
if (healthone==healthtwo)
{
ending=healthone;
}
I don't know about your IDE, but many of them will flag something like this as a warning. Check your warnings...

Why won't the numbers stay random?

I have been coding a blackjack game that is almost done it's first stage of development. I am almost done, the only problem is that the random numbers that are generated in this part:
if(x.hit == 1) {
if (x.userShowCardThree == 0) {
x.userShowCardThree = 1 + (rand()%11);
int dealerHit1 = x.userShowCardThree;
x.userTotal += dealerHit1;
x.cardCount++;
}
else {
if (x.userShowCardFour == 0) {
x.userShowCardFour = 1 + (rand()%11);
int dealerHit2 = x.userShowCardFour;
x.userTotal += dealerHit2;
x.cardCount++;
}
else {
if (x.userShowCardFive ==0) {
x.userShowCardFive = 1 + (rand()%11);
int dealerHit3 = x.userShowCardFive;
x.userTotal += dealerHit3;
x.cardCount++;
}
}
}
is not the same as the numbers generated in the final part:
cout << "You had a total of: " << x.userTotal << endl;
as i keep getting different numbers. I will paste my entire code below.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstring>
using namespace std;
void setRand() {
srand(time(0));
}
class Game {
public:
int userCards[11] = {1,2,3,4,5,6,7,8,9,10,11};
int dealerCards[11] = {1,2,3,4,5,6,7,8,9,10,11};
int userShowCardOne = userCards[1 + (rand()%11)];
int userShowCardTwo = userCards[1 + (rand()%11)];
int dealerShowCard = dealerCards[1 + (rand()%11)];
int dealerHiddenCard = dealerCards[1 + (rand()%11)];
int userShowCards[5] = {userShowCardOne, userShowCardTwo, userShowCardThree, userShowCardFour, userShowCardFive};
int userShowCardThree = 0;
int userShowCardFour = 0;
int userShowCardFive = 0;
int fresh = 1;
int beginningInput;
int hit = 1;
int dealerTotal = dealerShowCard + dealerHiddenCard;
int userTotal = userShowCardOne + userShowCardTwo;
int cardCount = 2;
int runGame = 1;
private:
};
// int a = 1 + rand()%11;
/*int b = 1 + rand()%11;
int c = 1 + rand()%11;
int d = 1 + rand()%11;
int e = 1 + rand()%11;
int f = 1 + rand()%11;
int g = 1 + rand()%11;
int h = 1 + rand()%11;
*/
int startGame();
int main() {
srand(time(0));
Game x;
cout << "Welcome to BlackJack 1.0.0" << endl;
cout << "Press: " <<endl;
cout << "1 ----- New Game" << endl;
cout << "2 ----- Help" << endl;
cin >> x.beginningInput;
if(x.beginningInput == 1){
startGame();
cout << "The dealer had: " << endl;
cout << x.dealerHiddenCard << endl;
cout << x.dealerShowCard << endl;
while(x.dealerTotal <= 16) {
cout << "The dealer has decided to hit" << endl;
int dealerHit = 1 + (rand()%11);
cout << "The dealer has gotten " << dealerHit << endl;
x.dealerTotal += dealerHit;
}
cout << "The dealer has decided to stay" << endl;
cout << "The dealer had a total of " << x.dealerTotal << endl;
cout << "You had a total of: " << x.userTotal << endl;
if (x.dealerTotal > 21 && x.userTotal <= 21) {
cout << "The dealer busted. You won!" << endl;
}
else if (x.userTotal > 21 && x.dealerTotal <= 21) {
cout << "You busted. The Dealer Won" << endl;
}
else if (x.userTotal > 21 && x.dealerTotal > 21) {
cout << "You and the dealer both busted. Tie" << endl;
}
else {
if (x.dealerTotal > x.userTotal) {
cout << "Sorry, you lost to the dealer" << endl;
}
else if (x.dealerTotal < x.userTotal) {
cout << "Congrats, you won!" << endl;
}
else {
cout << "You and the dealer tied. " << endl;
}
}
cout << "Would you like to play again? 1 to play again, 0 to quit" << endl;
cin >> x.beginningInput;
if (x.beginningInput == 1){
startGame();
}
else if (x.beginningInput == 0){
return 0;
}
}
else if (x.beginningInput == 0) {
cout << "Thanks for playing!" << endl;
}
else {
cout << "Here's the help section" << endl;
}
}
int startGame() {
Game x;
srand(time(0));
if (x.fresh != 1){
cout << "NEW GAME\n \n " << endl;
}
while (x.runGame == 1) {
cout << "Dealer: \n" << endl;
cout << "X" << endl;
cout << x.dealerShowCard << endl;
cout << "You: \n" << endl;
cout << x.userShowCardOne << endl;
cout << x.userShowCardTwo << endl;
x.userTotal = x.userShowCardOne + x.userShowCardTwo;
if (x.userShowCardThree != 0) {
cout << x.userShowCardThree << endl;
}
if (x.userShowCardFour != 0) {
cout << x.userShowCardFour << endl;
cout << "You can only hit one more time!" << endl;
}
if (x.userShowCardFive != 0) {
cout << x.userShowCardFive << endl;
}
if(x.cardCount > 5) {
cout << "sorry, there is a 5 card limit.";
}
cout << "Would you like to hit or stay? (1 for hit or 2 for stay)" << endl;
cin >> x.hit;
x.fresh = 2;
if(x.hit == 1) {
if (x.userShowCardThree == 0) {
x.userShowCardThree = 1 + (rand()%11);
int dealerHit1 = x.userShowCardThree;
x.userTotal += dealerHit1;
x.cardCount++;
}
else {
if (x.userShowCardFour == 0) {
x.userShowCardFour = 1 + (rand()%11);
int dealerHit2 = x.userShowCardFour;
x.userTotal += dealerHit2;
x.cardCount++;
}
else {
if (x.userShowCardFive ==0) {
x.userShowCardFive = 1 + (rand()%11);
int dealerHit3 = x.userShowCardFive;
x.userTotal += dealerHit3;
x.cardCount++;
}
}
}
}
if (x.hit == 2) {
x.runGame = 2;
}
}
return 0;
}
Looks like you have 2 instances of the Game object and both are named x
int main() {
srand(time(0));
Game x;
...
cout << "You had a total of: " << x.userTotal << endl;
then in the startGame function
int startGame() {
Game x;
...
if(x.hit == 1) {
if (x.userShowCardThree == 0) {
x.userShowCardThree = 1 + (rand()%11);
int dealerHit1 = x.userShowCardThree;
x.userTotal += dealerHit1;
x.cardCount++;
The x in main() is not the same as x in startGame() as both are separate Game objects.