Runtime Error #3 (reapeating numbers code)

It gives me this error which does not allow me to run my program. Also my program will keep repeating it's code.... what do I do? Please help i need to finish this and I don't understand coding very well. Please add anything to improve or correct my code. Also try it out and see what I'm dealing with here.


#include <iostream>;
#include <cstdlib> //drand function
#include <ctime> // time function
#include <Windows.h> //Sleep function
using namespace std;

int main()

{

int num;
int menuchoice;
char game;
float bet = 's';


// Suspend program for 2 seconds

Sleep(2000);
//seed random number ganerator using the system clock
srand(static_cast<unsigned int> (time(0)));
//generate a random number between 1 and 13
num = rand() % 13 + 1;




// Welcome user to the introduction screen

cout << " BLACKJACK\n\n\n";
cout << " Thank you for playing this game!\n\n";
cout << " Press 'b' to Start the game\n";
cout << " Press 'i' for intructions on the game\n";
cout << " Press 'c' for credits on the game\n";
cin >> menuchoice;

// Show the instructions

if (game == 'i')

{
cout << " The game you will be playing is called 'Blackjack' or 'twenty-one'.\n This is a multiplayer games in which one of the players want to get cards";

}

if (game == 'c')

{

cout << " The writer, producer, developer, and artist of this game";

}

if (game == 'b')
{
do
{

//Tell the user what card they got

cout << " You got" << num;

// Add up all the user"s cards and print

// Ask them if the y want to hit or stay

cout << " Would you like to get hit ('h') or stay ('s')";

//If they want to hit, loop back to generating a random number again otherwise let the computer get a hand

}while (bet == 's');
}
}
cin >> menuchoice; You get this from user

if (game == 'i') but check this. What is value of game at this line?

And in this section:
1
2
3
4
5
do
{
	cout << " You got" << num;
	cout << " Would you like to get hit ('h') or stay ('s')";
}while (bet == 's');

You never allow bet to change, so if the condition is true once, it stays that way forever.
Last edited on
At a cursory glance I noticed two problems. Additionally, you should use the code tags to maintain formatting, making this a lot easier to read.

The first problem is rather minor, however the second problem is the likely culprit of what you are describing. Comments were inserted to show the problem, and were bolded

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
68
69
70
71
72
73
#include <iostream>;   // Dont need a semicolon after the #include
#include <cstdlib> //drand function
#include <ctime> // time function
#include <Windows.h> //Sleep function
using namespace std;

int main()

{

    int num;
    int menuchoice;
    char game;
    float bet = 's';


    // Suspend program for 2 seconds

    Sleep(2000);
    //seed random number ganerator using the system clock
    srand(static_cast<unsigned int> (time(0)));
    //generate a random number between 1 and 13
    num = rand() % 13 + 1;




    // Welcome user to the introduction screen

    cout << " BLACKJACK\n\n\n";
    cout << " Thank you for playing this game!\n\n";
    cout << " Press 'b' to Start the game\n";
    cout << " Press 'i' for intructions on the game\n";
    cout << " Press 'c' for credits on the game\n";
    cin >> menuchoice;

    // Show the instructions

    if (game == 'i')    // the variable game is not initialized. 

    {
        cout << " The game you will be playing is called 'Blackjack' or 'twenty-one'.\n This is a multiplayer games in which one of the players want to get cards";

    }

    if (game == 'c')

    {

        cout << " The writer, producer, developer, and artist of this game";

    }

    if (game == 'b')
    {
        do
        {

            //Tell the user what card they got

            cout << " You got" << num;

            // Add up all the user"s cards and print

            // Ask them if the y want to hit or stay

            cout << " Would you like to get hit ('h') or stay ('s')";

            //If they want to hit, loop back to generating a random number again otherwise let the computer get a hand

        }while (bet == 's');
    }
}



If this fixes the problem please do say so. Ill have a more intimate look later.
Last edited on
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
68
69
70
71
72
73
74
75
76
77
#include <iostream>;
#include <cstdlib> //drand function
#include <ctime> // time function
#include <Windows.h> //Sleep function
using namespace std;

int main()

{

int num;
int menuchoice;
char game;
float bet = 's';  //int or float????


// Suspend program for 2 seconds

Sleep(2000);
//seed random number ganerator using the system clock
srand(static_cast<unsigned int> (time(0)));
//generate a random number between 1 and 13
num = rand() % 13 + 1;




// Welcome user to the introduction screen

cout << " BLACKJACK\n\n\n";
cout << " Thank you for playing this game!\n\n";
cout << " Press 'b' to Start the game\n";
cout << " Press 'i' for intructions on the game\n";
cout << " Press 'c' for credits on the game\n";
cin >> menuchoice; 

// Show the instructions
//NOTE : "game" variable is unused.

if (menuchoice == 'i') // if [game == 'i')

{
cout << " The game you will be playing is called 'Blackjack' or 'twenty-one'.\n This is a multiplayer games in which one of the players want to get cards";

}

if (menuchoice == 'c') // if [game == 'ic)

{

cout << " The writer, producer, developer, and artist of this game";

}

if (menuchoice == 'b') // if [game == 'b')
{
do
{

//Tell the user what card they got
cout << " You got" << num;

// Add up all the user"s cards and print -> What are user cards???
// You'll need to provide more information. "user card" variable?

// Ask them if the y want to hit or stay

cout << " Would you like to get hit ('h') or stay ('s')";
cin >> bet; 

//If they want to hit, loop back to generating a random number again otherwise let the computer get a hand
num = rand() % 13 + 1; //???
//Be more specific. What is the variable you are looking for?

}while (bet == 's');
}
}
Last edited on
No one seems to have noticed this gem:

float bet = 's';
Thanks Guys!!! My problem has been fixed, but now I face the problem of saying what the total amount they have gotten.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	// Let Player 1 go first

			cout << " Player 1's turn!!!";

			// Tell the player what card they got

			cout << " You got" << num;

			// Add up all the user"s cards and print

			

			// Ask them if the y want to hit or stay

			cout << " Would you like to get hit ('h') or stay ('s')";

			//If they want to hit, loop back to generating a random number again otherwise let the computer get a hand

			cin >> choice;

			}while (choice == 'h');

In the "Add up all the user's cards and print" the total is supposed to be 'num', but num has to be used for several different numbers, so how do I add all those 'nums' to make a 'total' ?
Why do we have 3 threads about the same thing? Don't do that - it's annoying because someone might write a long reply in one thread, only to find a whole lot has been written by others in one of the other ones.

but num has to be used for several different numbers, so how do I add all those 'nums' to make a 'total' ?


Think about that for a little while .......
Topic archived. No new replies allowed.