Needs help with validation

Hi guys, I need help with this programming assignment!!

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <stdlib.h>
#include <limits>
#include <ctime>
#include <stdio.h>

using namespace std;

int main()
{

    srand(time(0)); //seeds random generator

    char name[10]; //variable for name.
    int balance = 500; //starting balance
    int rolltime= 1;
    int NEWbalance; //credit calculation
    int NEWbet; //placing new bet
    char repeat = 'y'; //TRUE/FALSE check
    char repeat1 = 'y'; //TRUE/FALSE check
    char repeat2 = 'n'; //TRUE/FALSE check
    int dice1, dice2 = 0; //player roll
    int playbet; //bet place
    int winbet; //winning bet
    int addbet; //increase with new bet
    int rolltimer;

    system("cls"); //clear screen


    cout << "Kindly please enter your name.\n"; //enter player name
    cin >> name;
    cout << "Hi " << name << "! You have an available balance of 500 coins. \n";
    system("Pause");

    while (repeat == 'n' || repeat == 'N')
{
    cout << "Goodbye" << endl;
}

    while (repeat == 'y' || repeat == 'Y')


    {
        //std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //recommend this instead of system("PAUSE") you will need '#include <limits>"
                                                                            //you can find why this is better in a long thread somewhere in these forums

        cout << "Kindly please enter your bet amount\n"; //player to enter bet amount

        cin >> playbet; //enter bet
        int dice1, dice2 = 0; //player roll
        int roll;

        dice1 = rand() % 6 + 1;
        dice2 = rand() % 6 + 1;
        roll = dice1 + dice2;
        rolltimer = rolltime;
        cout << "Your roll was: " << dice1 << " + " << dice2 << " = " << roll << endl;


        if ((rolltimer == 1) && (roll == 7 || roll == 11))
        {
            winbet = playbet * 2;
            NEWbalance = balance + winbet;
            balance = NEWbalance;
            std::cout << "You lose!" << balance << " Do you wish to play again? [Y/N]:" << std::endl;
            cin >> repeat;
            rolltimer = rolltime - 1;
        }
         if  ((rolltimer == 1) && (roll == 2 || roll == 3 || roll == 12))
        {
            NEWbalance = balance - playbet;
            balance = NEWbalance;
            std::cout << "You lose!" << balance << " Do you wish to play again? [Y/N]:" << std::endl;
            std::cin >> repeat;
            rolltimer = rolltime - 1;
            system("CLS");
        }

        if ((rolltimer == 1) && (roll == 4 || roll == 5 || roll == 6 || roll == 8 || roll == 9 || roll == 10))

        {
            dice1 = rand() % 6 + 1;
            dice2 = rand() % 6 + 1;
            roll = dice1 + dice2;

            cout << "Continue" << endl;
            cin >> repeat2;
            if (repeat2== 'n' || repeat2 == 'N')
            {
                cout << "Quit midway" << endl;
                break;
            }
                {
            cout << "Do you want to increase your betting amount?" << endl;
            cin >> repeat1;
            if (repeat1 == 'y' || repeat1 == 'Y')
            {
            cout << "What is the amount you wish to increase?" << endl;
            cin >> addbet;
            NEWbet = playbet + addbet;
            playbet = NEWbet;

            }}
            rolltimer = rolltime + 1;


        if ((rolltimer == 2) && (roll == 7 || roll == 11))

            {
            NEWbalance = balance - playbet;
            balance = NEWbalance;
            cout << "Your roll was: " << dice1 << " + " << dice2 << " = " << roll << endl;
            std::cout << "You lose!" << winbet << " Your current credit balance is " << balance << " Do you wish to play again? [Y/N]:" << std::endl;
            std::cin >> repeat;
            rolltimer = rolltime - 1;
            }
        if ((rolltimer == 2) && (roll == 4 || roll == 10 ))
        {
            winbet = 2 * playbet + playbet;
            NEWbalance = balance + winbet;
            balance = NEWbalance;
            cout << "Your roll was: " << dice1 << " + " << dice2 << " = " << roll << endl;
            std::cout << "You win!" << winbet << " Your current credit balance is " << balance << " Do you wish to play again? [Y/N]:" << std::endl;
            std::cin >> repeat;
            rolltimer = rolltime - 1;
        }
        if ((rolltimer == 2) && (roll == 5 || roll == 9))
        {
            winbet = playbet * 1.5 + playbet;
            NEWbalance = balance + winbet;
            balance = NEWbalance;
            cout << "Your roll was: " << dice1 << " + " << dice2 << " = " << roll << endl;
            std::cout << "You win!" << winbet << " Your current credit balance is " << balance << " Do you wish to play again? [Y/N]:" << std::endl;
            std::cin >> repeat;
            rolltimer = rolltime - 1;

        }
        if ((rolltimer == 2) && (roll == 6 || roll == 8))
        {
            winbet = playbet * 1.2 + playbet;
            NEWbalance = balance + winbet;
            balance = NEWbalance;
            cout << "Your roll was: " << dice1 << " + " << dice2 << " = " << roll << endl;
            std::cout << "You win!" << winbet << " Your current credit balance is " << balance << " Do you wish to play again? [Y/N]:" << std::endl;
            std::cin >> repeat;
            rolltimer = rolltime - 1;


        }

        if ((rolltimer == 2) && (roll == 2 || roll == 3 || roll == 12))
        {
            NEWbalance = balance + playbet;
            balance = NEWbalance;
            cout << "Your roll was: " << dice1 << " + " << dice2 << " = " << roll << endl;
            std::cout << "No Winner. All Bets are returned. Do you wish to play again? [Y/N]:" << std::endl;
            std::cin >> repeat;
            rolltimer = rolltime - 1;

        }



        }

    }
    return 0;
}

========================================================

How do I validate the name to be a character, the bet to be integer and validating user input for Y for yes and N for no. Please help anyone?
Last edited on
What do you mean?
ctype::scan_is() can test if an array of characters is alphabetic.
http://www.cplusplus.com/reference/locale/ctype/scan_is/

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
I would like to do a validation on characters for entering player's name. But I do not know how to go round about doing it..
bump*
I provided you with a link on how to do it.
That page includes an example.

If you don't want to use scan_is, you can always use isalpha().
http://www.cplusplus.com/reference/cctype/isalpha/
Topic archived. No new replies allowed.