Dice game help

hello guys i have some errors and questions in my game...
1) The program dont want to close when the player will wrong the answer
1
2
3
4
5
6
7
8
9
10
11
12
int Answers()
{
    if(ans == "13")
    {
               cout << "You are filled in your bank account $50" << endl;
               money += 50;
    }else{
               cout << "[!] Wrong" << endl;
               system("PAUSE");
               return false;
    }
}

2) I want to know how to detect the words in the game like (a,b,c,d,e,f...)
when u will enter word to say "You need to enter number"
3) And also how do i make the program returns after ending ... :D
here is the whole script ...

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
78
79
80
81
82
83
84
85
86
87
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <winable.h>
#include <limits>
#include <ctime>

using namespace std;

string ans;
int money = 0, num, dice;

int Answers()
{
    if(ans == "13")
    {
               cout << "You are filled in your bank account $50" << endl;
               money += 50;
    }else{
               cout << "[!] Wrong" << endl;
               system("PAUSE");
               return 1;
    }
}

int Questions()
{
    cout << "If you answer true u will filled in bank $50" << endl;
    cout << "How much is 7 + 6 ?" << endl;
    getline(cin, ans);
    Answers();
}

int main()
{
    char roll;
    {
         cout<< "Money in bank: $" << money << endl;
         cout<< "Please enter your guess for the next roll. It only costs $1.00 to play, If you are correct I will pay you $100" <<endl;
         cin >> num;
         cin.get(roll);
         srand(time(NULL));
         dice = (rand()%6)+1;
         if(num > 6)
         {
                system("cls");
                cout << "You can't enter hieger number of 6!" << endl;
                cout << "Try again !" << endl;
                system("PAUSE");
                return false;
         }
         if(money <= 0)
         {
                  cout << "You dont have money!" << endl;
                  cout << "You can enter moeny in bank if you answer on some questons." << endl;
                  Questions();
         }
         if (num == dice)
         {
                      cout << "Rolling the dices..." << endl;
                      Sleep(5000);
                      system("cls");
                      cout << "Your number " << num << endl;
                      cout << "Winner! The dice rolled " << dice <<endl;
                      money += 100;
                      cout << "Your bank account is now $" << money << endl;
                      cout << "\n";
                      system("PAUSE");
         }
         else if (num != dice)
         {
                      cout << "Rolling the dices..." << endl;
                      Sleep(5000);
                      system("cls");
                      cout << "Your number " << num << endl;
                      cout << "Sorry the dice rolled " << dice <<endl;
                      money -= 1;
                      cout << "Your bank account is now $" << money << endl;
                      cout << "\n";
                      system("PAUSE");
         }
         return true;
    }
return 0;
}


Thanks :)
Last edited on
please guys i need help :/
closed account (o3hC5Di1)
Hi there,

1) Your function is supposed to return an int, but you return a boolean false. If you want to end the program then you will have to check the Answers() return value in main(), and return0 from main (like you would in any other function). Returning from main ends the program.

2) The easiest way to validate input:

1
2
3
4
5
6
7
8
int some_number;
std::cout << "Please enter a number: ";
std::cin >> some_number;

if (std::cin.fail() == true) //if the input could not be converted to a number
{
    //...
}


3) I'm not sure what you mean by this question - but I assume you mean how to keep repeating the process:

40
41
42
43
while (true)
{
   //lines 40-83
}


Also, remove the "return true" at line 84.

Hope that helps.

All the best,
NwN

Thank you for your advice look what i done...
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <winable.h>
#include <limits>
#include <ctime>

using namespace std;

int money = 10,num,dice;

string ans;

    int Answers()
    {
        if(ans == "13")
        {
               cout << "You are filled in your bank account $50" << endl;
               money += 50;
        }
        if(ans != "13")
        {
               cout << "[!] Wrong" << endl;
               system("pause");               
        }
    }
    int Questions()
    {
        cout << "If you answer true u will filled in bank $50" << endl;
        cout << "How much is 7 + 6 ?" << endl;
        getline(cin, ans);
        Answers();
    }

int main()
{   
    char roll;
    {
         while (true)
         {
               cout<< "Money in bank: $" << money << endl;
               cout<< "Please enter your guess for the next roll. It only costs $1.00 to play, If you are correct I will pay you $100" <<endl;
               cin >> num;
               cin.get(roll);
               srand(time(NULL));
               dice = (rand()%6)+1;
               int some_number;
               std::cin >> some_number;
               if (std::cin.fail() == true)
               {
                      BlockInput(true);
                      cout << "[!] Please enter a number." << endl;
                      system("pause");
                      return false;
               }
               if(num > 6 || num < 1)
               {
                      system("cls");
                      cout << "[!] You can't enter hieger number of 6 or lower than 1" << endl;
                      cout << "Try again !" << endl;
                      system("PAUSE");
                      return false;
               }
               if(money <= 0)
               {
                      cout << "You dont have money!" << endl;
                      cout << "You can enter moeny in bank if you answer on some questons." << endl;
                      Questions();
               }
               if (num == dice)
               {
                      cout << "Rolling the dices..." << endl;
                      Sleep(2500);
                      system("cls");
                      cout << "Your number " << num << endl;
                      cout << "Winner! The dice rolled " << dice <<endl;
                      money += 100;
                      cout << "Your bank account is now $" << money << endl;
                      cout << "\n";
               }
               else if (num != dice)
               {     
                      cout << "Rolling the dices..." << endl;
                      Sleep(2500);
                      system("cls");
                      cout << "Your number " << num << endl;
                      cout << "Sorry the dice rolled " << dice <<endl;
                      money -= 1;
                      cout << "Your bank account is now $" << money << endl;
                      cout << "\n";
               }
         }
    }
return 0;
}


When ill start the program runs good but when ill tupe number i need to tupe another number to start the rolling...
and i also dont get it where to put that "int Answers()" when the answer is not correct.
And also tell me where need to be this code
1
2
3
4
5
6
7
8
9
int some_number;
               std::cin >> some_number;
               if (std::cin.fail() == true)
               {
                      BlockInput(true);
                      cout << "[!] Please enter a number." << endl;
                      system("pause");
                      return false;
               }

Thanks :)
Please try it your self to see what is the problem im not good at english much .... :/
Last edited on
Topic archived. No new replies allowed.