I recently wrote a Blackjack card game console application in Dev C++, for the most part it works fine, however sometimes i get this glitch where it says i lost even though my number was higher than the cpu's number, i believe that the code i have written is correct and this should not happen and this also doesnt happen every time , Please note that I'm very new to programming, as I just started almost a week ago, and I'm still in the process of watching tutorials online, so I know that I have a long way to go. Also, I'm not very experienced with the while loop. Another glitch ive seen is where it will tell me that i went over 21 so i lost, but I wasn't over 21, Ill hit and my total will be like 20 and it will tell me that I went over.
Heres the code for my game.
#include <iostream>
#include <cstdlib>
#include <time.h>
usingnamespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */\
int value2 = 0;
int goal;
int total;
int value;
int choice2;
int choice3;
int bet1;
int iscore = 500;
int number;
int number2;
int cpu;
int main(void) {
srand(time(NULL));
number=(rand()%21+2);
number2=(rand()%10+2);
cpu=(rand()%21+2);
cout << "Welcome to Blackjack!" << endl;
cout << "Enter your goal (higher than 500): " << endl;
cin >> goal;
cout << "Enter your bet:" << endl;
cin >> bet1;
while (bet1 >= goal)
{
cout << "It's not wise to bet your goal or to bet over it!\nEnter your bet:" << endl;
cin >> bet1;
}
cout << "Your total is: " << number << endl;
cout << "Earnings: " << iscore << " Goal: " << goal << "\n(1)Hit\n(2)Stay" << endl;
cin >> choice2;
if (choice2 == 1)
{
(total = number + number2);
cout << "Your new total is: " << total << endl;
cout << "Your opponent's total is: " << cpu << endl;
if (total > 21)
{
value2 = 1;
}
if (total == cpu)
{
cout << "It's a tie!" << endl;
cout << "Your earnings are still: " << iscore << " Your goal is: " << goal << endl;
}
if (number > cpu)
{
cout << "You win!" << endl;
cout << "Your earnings are now: " << (iscore =bet1 + iscore) << " Your goal is: " << goal << endl;
if (iscore >= goal)
{
cout << "Congratulations! You have hit it big in Vegas and have reached your goal!" << endl;
return 0;
}
}
if (value2=1 and number > cpu)
{
cout << "JUST KIDDING! You went over 21. You lose." << endl;
cout << "Your earnings are now: " << (iscore=iscore - bet1) << " Your goal is: " << goal << endl;
}
if (number < cpu)
{
cout << "You lose." << endl;
cout << "Your earnings are now: " << (iscore=iscore - bet1) << " Your goal is: " << goal << endl;
if (iscore < 0)
{
cout << "Your dreams of hitting it big in Vegas are shattered. Game Over." << endl;
return 0;
}
}
cout << "Would you like to play again?\n(1)Yes\n(2)No" << endl;
cin >> choice3;
if (choice3 == 2)
{
cout << "Thanks for playing!" << endl;
return 0;
}
if (choice3 == 1)
{
int value=1;
}
}
if (choice2 == 2)
{
cout << "Your total is still: " << number << endl;
cout << "Your opponent's total is: " << cpu << endl;
if (number == cpu)
{
cout << "Its a tie!" << endl;
cout << "Your earnings are still: " << iscore << " Your goal is: " << goal << endl;
if (number > cpu)
{
cout << "You win!" << endl;
cout << "Your earnings are now: " << (iscore=bet1 + iscore) << " Your goal is: " << goal << endl;
if (iscore >= goal)
{
cout << "Congratulations! You have hit it big in Vegas and have reached your goal!" << endl;
return 0;
}
}
if (number < cpu)
{
cout << "You lose." << endl;
cout << "Your earnings are now: " << (iscore=iscore - bet1) << " Your goal is: " << goal << endl;
if (iscore < 0)
{
cout << "Your dreams of hitting it big in Vegas are shattered. Game Over." << endl;
return 0;
}
}
cout << "Would you like to play again?\n(1)Yes\n(2)No" << endl;
cin >> choice3;
if (choice3 == 2)
{
cout << "Thanks for playing!" << endl;
return 0;
}
if (choice3 == 1)
{
int value=1;
}
}
}
while (value=1)
{
srand(time(NULL));
number=(rand()%21+1);
number2=(rand()%10+2);
cpu=(rand()%21+1);
cout << "Enter your bet:" << endl;
cin >> bet1;
while (bet1 >= goal)
{
cout << "It's not wise to bet your goal or to bet over it!\nEnter your bet:" << endl;
cin >> bet1;
}
cout << "Your total is: " << number << endl;
cout << "Earnings: " << iscore << " Goal: " << goal << "\n(1)Hit\n(2)Stay" << endl;
cin >> choice2;
if (choice2 == 1)
{
(total = number + number2);
cout << "Your new total is: " << total << endl;
cout << "Your opponent's total is: " << cpu << endl;
if (total > 21)
{
value2 = 1;
}
if (total == cpu)
{
cout << "It's a tie!" << endl;
cout << "Your earnings are still: " << iscore << " Your goal is: " << goal << endl;
}
if (number > cpu)
{
cout << "You win!" << endl;
cout << "Your earnings are now: " << (iscore =bet1 + iscore) << " Your goal is: " << goal << endl;
if (iscore >= goal)
{
cout << "Congratulations! You have hit it big in Vegas and have reached your goal!" << endl;
return 0;
}
}
if (value2=1 and number > cpu)
{
cout << "JUST KIDDING! You went over 21. You lose." << endl;
cout << "Your earnings are now: " << (iscore=iscore - bet1) << " Your goal is: " << goal << endl;
}
if (number < cpu)
{
cout << "You lose." << endl;
cout << "Your earnings are now: " << (iscore=iscore - bet1) << " Your goal is: " << goal << endl;
if (iscore < 0)
{
cout << "Your dreams of hitting it big in Vegas are shattered. Game Over." << endl;
return 0;
}
}
cout << "Would you like to play again?\n(1)Yes\n(2)No" << endl;
cin >> choice3;
if (choice3 == 2)
{
cout << "Thanks for playing!" << endl;
return 0;
}
if (choice3 == 1)
{
int value=1;
}
}
if (choice2 == 2)
{
cout << "Your total is still: " << number << endl;
cout << "Your opponent's total is: " << cpu << endl;
if (number == cpu)
{
cout << "Its a tie!" << endl;
cout << "Your earnings are still: " << iscore << " Your goal is: " << goal << endl;
}
if (number > cpu)
{
cout << "You win!" << endl;
cout << "Your earnings are now: " << (iscore=bet1 + iscore) << " Your goal is: " << goal << endl;
if (iscore >= goal)
{
cout << "Congratulations! You have hit it big in Vegas and have reached your goal!" << endl;
return 0;
}
}
if (number < cpu)
{
cout << "You lose." << endl;
cout << "Your earnings are now: " << (iscore=iscore - bet1) << " Your goal is: " << goal << endl;
if (iscore < 0)
{
cout << "Your dreams of hitting it big in Vegas are shattered. Game Over." << endl;
return 0;
}
}
cout << "Would you like to play again?\n(1)Yes\n(2)No" << endl;
cin >> choice3;
if (choice3 == 2)
{
cout << "Thanks for playing!" << endl;
return 0;
}
if (choice3 == 1)
{
int value=1;
}
}
}
return 0;
}