Не корректная работа цикла при вызове функции добавления пользователя
19.12.2016, 22:28. Показов 566. Ответов 1
Всем Доброго времени суток.
Такая проблема у меня есть класс в который мы вводим имя пользователя и деньги,в классе BlackJack ,я поставил проверку , что если у нас наши деньги равны 0 , то заново просим добавить пользователя. Но когда я это сделал , просто выводит оба поля запроса и преступает к другой ветке меню. Попробовал поставить cin.getline(), смог вводит имя,но как только нажимал enter,то запускался бесконечный цикл. В общем,прошу помощи, помогите исправить ошибки в коде. Очень прошу.
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| #pragma once
class User
{
private:
char name[100];
int money;
int choice;
public:
User()
{
this->money = 0;
strcpy_s(this->name, "");
};
~User();
void fillAccaunt();
int getMoney() { return money; }
char *getName() { return name; }
}; |
|
C++ | 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
| #include "stdafx.h"
#include "User.h"
User::~User()
{
}
void User::fillAccaunt()
{
cout << "Please,enter your name: ";
cin.getline(this->name, 100);
cout << "\nYour name: " << this->name << endl;
cout << "\nPlease enter the amount of your money: ";
cin >> this->money;
while (money < 10)
{
cout << "\nSorry,you cannot have less then 10 $,try next time! Have a nice day!" << endl;
cout << "Try again ? 1 - Yes,0 - exit" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "\nPlease enter the amount of your money: ";
cin >> this->money;
break;
case 0:
cout << "Goodbye!" << endl;
default:
break;
}
}
} |
|
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| #pragma once
class Menu
{
private:
int choice = 0;
bool exit = false;
User user;
public:
Menu()
{
this->choice = 0;
this->exit = false;
};
~Menu();
void menu();
int getMoney() { return user.getMoney(); }
char *getName() {return user.getName();}
}; |
|
C++ | 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 "stdafx.h"
#include "Menu.h"
Menu::~Menu()
{
}
void Menu::menu()
{
Roulette r;
Craps c;
BlackJack b;
user.fillAccaunt();
cout << "Welcome to casino Eldorado!" << endl;
{
system("cls");
cout << "Your name: " << getName() << endl;
cout << "Your money: " << getMoney() << endl;
cout << "\nPlease choose one of the games: \n";
cout << "1-Roulette " << endl;
cout << "2-BlackJack " << endl;
cout << "3-Craps" << endl;
cout << "0-exit" << endl;
cout << "Please enter your choice here: ";
cin >> choice;
system("cls");
while (choice < 0 || choice > 4)
{
cout << "ERROR:Wrong value!" << endl;
cout << "\nPlease choose one of the games: \n";
cout << "1-Roulette " << endl;
cout << "2-BlackJack " << endl;
cout << "3-Craps" << endl;
cout << "0-exit" << endl;
cout << "Please enter your choice here: ";
cin >> choice;
}
if (!cin)
{
cout << "ERROR:Wrong value!" << endl;
cout << "\nPlease choose one of the games: \n";
cout << "1-Roulette " << endl;
cout << "2-BlackJack " << endl;
cout << "3-Craps" << endl;
cout << "0-exit" << endl;
cout << "Please enter your choice here: ";
cin >> choice;
}
switch (choice)
{
case 1:
r.playRoulette();
break;
case 2:
b.getAccountMoney(user.getMoney());
b.getAccountName(user.getName());
b.playGame();
break;
case 3:
c.playCraps();
break;
case 0:
exit = true;
cout << "Press any key to exit!" << endl;
break;
default:
break;
}
cout << "Press any button to come back to menu" << endl;
system("pause");
system("cls");
} while (choice != 0);
} |
|
C++ | 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
| #pragma once
class BlackJack
{
private:
vector<User*>users;
bool isGame;
const int maxPoint = 21;
int card, cardsSumPC, cardsSumPlayer;
array<int, 11> deck = { 1,2,3,4,5,6,7,8,9,10,11 };
int makeBet, minBet, maxBet, oldBet, newBet;
int oneToOneOdd;
int insertNumber, exit;
int pcMoney, money,accountMoney;
char * accountName;
User u;
Menu m;
public:
BlackJack()
{
this->isGame = false;
this->cardsSumPC = 0;
this->cardsSumPlayer = 0;
this->card = 0;
this->makeBet = 0;
this->minBet = 10;
this->maxBet = 500;
this->exit = 0;
this->oneToOneOdd = 1;
};
~BlackJack();
int playerGame();
int pcGame();
int getCard() { return rand() % 10 + 1; }
void playGame();
int win(int bet, int odds, int maxBet);
int lose(int money, int maxBet);
char* getAccountName(char * name);
int getAccountMoney(int money);
}; |
|
C++ | 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
171
| #include "stdafx.h"
#include "BlackJack.h"
BlackJack::~BlackJack()
{
}
int BlackJack::playerGame()
{
Card takeCard;
cout << "Welcome to the Blackjack table! \n1-Play game\n0-go to main menu" << endl;
cin >> insertNumber;
while (insertNumber < 0 || insertNumber > 1)
{
cout << "Wrong value! '1','0' only!" << endl;
cout << "Welcome to the Blackjack table! \n1-Play game\n0-go to main menu" << endl;
cin >> insertNumber;
}
//ПРОБЛЕМА ЗДЕСЬ
switch (insertNumber)
{
case 1:
if (accountMoney == 0)
{
cout << "You lost all your money! '1' - Go to the main manu,'0' - Exit " << endl;
cin >> isGame;
switch (isGame)
{
case 1:
system("cls");
u.fillAccaunt();
m.menu();
break;
case 0:
isGame = false;
default:
break;
}
}
system("cls");
cout << "Accaunt data: " <<"\nmoney: " <<accountMoney<<"\nname:"<<accountName;
cout << "\nPlease make bet: ";
cin >> makeBet;
while (makeBet < minBet || makeBet>accountMoney)
{
cout << "Your bet cannot be less then 10 $ and bigger then " << accountMoney << " $ " << endl;
cout << "Please make bet: ";
cout << "\nInput: ";
cin >> makeBet;
}
money = accountMoney - makeBet;
cout << "Your money: " << money << " $ " << endl;
do {
card = 0;
card = getCard();
cout << "\nYour card: " << endl;
takeCard.assignCard(deck[card]);
cardsSumPlayer += deck[card];
cout << "Your points: " << cardsSumPlayer << endl;
if (cardsSumPlayer > maxPoint)
{
accountMoney = lose(makeBet, accountMoney);
cout << "You lost! Your fund contains:" << accountMoney << endl;
cout << "You lost :" << makeBet << endl;
break;
}
cout << "Get card or pass step ? 1 - take card, 0 - pass step" << endl;
cin >> isGame;
} while (isGame != false);
break;
case 0:
system("cls");
m.menu();
break;
}
return cardsSumPlayer;
}
int BlackJack::pcGame()
{
srand(time(NULL));
Card takeCard;
while (cardsSumPC < 17)
{
card = getCard();
cardsSumPC += deck[card];
}
return cardsSumPC;
}
void BlackJack::playGame()
{
Menu m;
isGame = false;
do {
cardsSumPC = 0; cardsSumPlayer = 0;
playerGame();
if (cardsSumPlayer <= maxPoint) {
pcGame();
if (cardsSumPlayer == cardsSumPC)
{
cout << " Draw" << endl;
}
cout << "PC points cards are equal to: " << cardsSumPC << endl;
if (cardsSumPlayer > cardsSumPC)
{
accountMoney = win(makeBet, oneToOneOdd, accountMoney);
cout << "You winner! Your fund contains: " << accountMoney << endl;
}
if (cardsSumPlayer < cardsSumPC && cardsSumPC <= maxPoint)
{
accountMoney = lose(makeBet, accountMoney);
cout << "PC winner! You lost: " << makeBet << endl;
}
if (cardsSumPlayer < cardsSumPC && cardsSumPC>maxPoint)
{
accountMoney = win(makeBet, oneToOneOdd, accountMoney);
cout << "You winner! Your fund contains: " << accountMoney << endl;
}
}
cout << "Play again ?: 1 - continue, 0 - leave: " << endl;
cin >> isGame;
system("cls");
} while (isGame != false);
if (isGame == false)
{
system("cls");
m.menu();
}
}
int BlackJack::win(int bet, int odds, int maxBet)
{
oldBet = maxBet;
newBet = oldBet + (bet * odds);
return newBet;
}
int BlackJack::lose(int money, int maxBet)
{
oldBet = maxBet;
newBet = oldBet - money;
return newBet;;
}
char* BlackJack::getAccountName(char * name)
{
return accountName = name;
}
int BlackJack::getAccountMoney(int money)
{
return accountMoney = money;
} |
|
0
|