Validating input

I have this blackjack 40 assignment for my c++ class. How would i validate the input so that the user cannot type any other number or letter.

[code]
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;


int draw_card() {
return rand() % 13 + 1;
}


int main() {
const int BET = 10;
cout << "Please choose a random seed, or 0 to use the current time:\n";
int seed = 0;
cin >> seed;
if (seed == 0) srand(time(0));
else srand(seed);

cout << "Welcome to Blackjack-40!\n\n";

int money = 100;
int choice = 0, total = 0, dealer_total = 0, card;/


while (true) {
if (choice != 1) {
cout << "You currently have $" << money << " and are betting $" << BET << endl;
}

if (choice == 1) {

card = draw_card();
if (card >= 10) {
card = 10;
}
if (card == 1 || card == 11) {
if (total + 11 <= 40) card = 11;
else card = 1;
}
cout << "You drew a " << card << endl;
total += card;
} else {
for (int i = 0; i < 4; i++) {
card = draw_card();
if (card >= 10) {
card = 10;
}
if (card == 1 || card == 11) {
if (total + 11 <= 40) card = 11;
else card = 1;
}
cout << "You drew a " << card << endl;
total += card;
}
}
int playedHands = 0;
cout << "The total value of your cards is: " << total << endl;
if (total == 40 && playedHands == 1) {
cout << "BLACKJACK 40!" << endl;
cout << "Player wins 20 dollars" << endl;
money += 20;
if (money >= 200) {
cout << "A WINNER IS YOU! GAME OVER!\n";
return 0;
}
choice = 0;
total = 0;
continue;
} else if (total > 40)
cout << "BUSTED!" << endl;
if (total <= 40) {
cout << "Do you wish to 1) Hit or 2) Stay or 3) Quit?\n";
cin >> choice;
} else choice = 2;
if (choice == 1)
continue;
if (choice == 3)
return 0;
while (dealer_total <= 35) {
card = draw_card();
if (card >= 10) card = 10;
if (card == 1) card = 11;
cout << "Dealer drew a " << card << endl;
dealer_total += card;
}
cout << "The total value of the dealer's cards is: " << dealer_total << endl;
if (dealer_total > 40) { // && total <= 40) {
// cout << "DEALER BUSTED!" << endl;
cout << "Player wins 10 dollars" << endl;
money += 10;
}
if (dealer_total <= 40 && total > 40) {
cout << "Dealer wins 10 dollars" << endl;
money -= 10;
} else if (total <= 40 && dealer_total <= 40) {
if ((40 - dealer_total) < (40 - total)) {
cout << "Dealer wins 10 dollars" << endl;
money -= 10;
} else {
cout << "Player wins 10 dollars" << endl;
money += 10;
}
} else if (dealer_total > 40) { //or && total > 40
cout << "DEALER BUSTED!" << endl;
}
total = 0;
dealer_total = 0;
choice = 0;

if (money == 0) {
cout << "YOU LOSE! GAME OVER!\n";
return 0;
} else continue;

if (money == 200) {
cout << "A WINNER IS YOU! GAME OVER!\n";
return 0;
} else continue;
}
}
Well you half-managed the code tags...
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
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;


int draw_card()
{
  return rand() % 13 + 1;
}


int main()
{
  const int BET = 10;
  cout << "Please choose a random seed, or 0 to use the current time:\n";
  int seed = 0;
  cin >> seed;
  if (seed == 0)
    srand(time(0));
  else
    srand(seed);

  cout << "Welcome to Blackjack-40!\n\n";

  int money = 100;
  int choice = 0, total = 0, dealer_total = 0, card;
  /while (true) {
    if (choice != 1) {
      cout << "You currently have $" << money << " and are betting $" << BET << endl;
    }

    if (choice == 1) {

      card = draw_card();
      if (card >= 10) {
        card = 10;
      }
      if (card == 1 || card == 11) {
        if (total + 11 <= 40)
          card = 11;
        else
          card = 1;
      }
      cout << "You drew a " << card << endl;
      total += card;
    } else {
      for (int i = 0; i < 4; i++) {
        card = draw_card();
        if (card >= 10) {
          card = 10;
        }
        if (card == 1 || card == 11) {
          if (total + 11 <= 40)
            card = 11;
          else
            card = 1;
        }
        cout << "You drew a " << card << endl;
        total += card;
      }
    }
    int playedHands = 0;
    cout << "The total value of your cards is: " << total << endl;
    if (total == 40 && playedHands == 1) {
      cout << "BLACKJACK 40!" << endl;
      cout << "Player wins 20 dollars" << endl;
      money += 20;
      if (money >= 200) {
        cout << "A WINNER IS YOU! GAME OVER!\n";
        return 0;
      }
      choice = 0;
      total = 0;
      continue;
    } else if (total > 40)
      cout << "BUSTED!" << endl;
    if (total <= 40) {
      cout << "Do you wish to 1) Hit or 2) Stay or 3) Quit?\n";
      cin >> choice;
    } else
      choice = 2;
    if (choice == 1)
      continue;
    if (choice == 3)
      return 0;
    while (dealer_total <= 35) {
      card = draw_card();
      if (card >= 10)
        card = 10;
      if (card == 1)
        card = 11;
      cout << "Dealer drew a " << card << endl;
      dealer_total += card;
    }
    cout << "The total value of the dealer's cards is: " << dealer_total << endl;
    if (dealer_total > 40) {    // && total <= 40) {
// cout << "DEALER BUSTED!" << endl;
      cout << "Player wins 10 dollars" << endl;
      money += 10;
    }
    if (dealer_total <= 40 && total > 40) {
      cout << "Dealer wins 10 dollars" << endl;
      money -= 10;
    } else if (total <= 40 && dealer_total <= 40) {
      if ((40 - dealer_total) < (40 - total)) {
        cout << "Dealer wins 10 dollars" << endl;
        money -= 10;
      } else {
        cout << "Player wins 10 dollars" << endl;
        money += 10;
      }
    } else if (dealer_total > 40) { //or && total > 40
      cout << "DEALER BUSTED!" << endl;
    }
    total = 0;
    dealer_total = 0;
    choice = 0;

    if (money == 0) {
      cout << "YOU LOSE! GAME OVER!\n";
      return 0;
    } else
      continue;

    if (money == 200) {
      cout << "A WINNER IS YOU! GAME OVER!\n";
      return 0;
    } else
      continue;
  }
}


Before you start adding any more code, you really need to start making more functions out of this 100+ line monster main you have.
We are not supposed to be using functions at this point in the class.
Last edited on
Last edited on
> We are not supposed to be using functions at this point in the class.
You have an idiot for a teacher then.

But wait, what's this?
1
2
3
4
int draw_card()
{
  return rand() % 13 + 1;
}


Are you completely sure about the no functions rule?
That was already there in his template, but other than that we are not supposed to use functions.
Topic archived. No new replies allowed.