[英]Why do I keep getting this error: exception Unhandled : Unhandled exception thrown: read access violation. this was 0x4
I am currently working on the BlackJack project, but there is an error showing "exception Unhandled: Unhandled exception thrown: read access violation. this was 0x4.".我目前正在处理 BlackJack 项目,但出现错误显示“异常未处理:抛出未处理异常:读取访问冲突。这是 0x4。”。 I am not quite sure which part I did wrong, and the program sometimes runs normally sometimes shows that exception.
我不太确定我做错了哪一部分,程序有时运行正常有时会显示该异常。 In draw_card function, it returns a value of a random number.
在draw_card function中,它返回一个随机数的值。 For example: if we get 13, the value will be 10. It also returns the name of the card and the type of the card such as 13 corresponds to king.
例如:如果我们得到 13,则值为 10。它还返回卡的名称,卡的类型如 13 对应于国王。
int main()
{
srand(time(0));
unsigned bet;
int player = 0 , dealer = 0;
string card , type;
cout << "You have $100. Enter bet: ";
cin >> bet;
cout << "Your cards are:" << endl;
player += draw_card(card, type, player);
cout << " "+card + " of " + type << endl;
player += draw_card(card, type, player);
cout << " " + card + " of " + type << endl << endl << endl;
}
int draw_card(string& card, string& type, int drawer_points) {
int randomNumber; //between 1 and 13
int suite; //between 1 and 4 to determine the suite of the card.
randomNumber = rand() % 13 + 1;
suite = rand() % 4 + 1;
card = getRank(randomNumber);
type = getSuit(suite);
if (randomNumber == 13 || randomNumber == 12 || randomNumber == 11) {
return 10;
}else if (randomNumber == 1) {
int ace1 = 21 - (drawer_points + 1);
int ace2 = 21 - (drawer_points + 11);
return ace1 < ace2 ? 1 : 11;
}
else
{
return randomNumber;
}
}
string getSuit(int suit) {
switch (suit)
{
case 0:
return "spades";
break;
case 1:
return "clubs";
break;
case 2:
return "diamonds";
break;
case 3:
return "hearts";
break;
default:
break;
}
}
string getRank(int rank) {
switch (rank)
{
case 13:
return "King";
break;
case 12:
return "Queen";
break;
case 11:
return "Jack";
break;
case 1:
return "Ace";
break;
case 2:
return "Two";
break;
case 3:
return "Three";
break;
case 4:
return "Four";
break;
case 5:
return "Five";
break;
case 6:
return "Six";
break;
case 7:
return "Seven";
break;
case 8:
return "Eight";
break;
case 9:
return "Nine";
break;
case 10:
return "Ten";
break;
default:
break;
}
You generate你生成
suite = rand() % 4 + 1;
This is a random number between 1 and 4 inclusive.这是一个介于 1 和 4 之间的随机数。
You then call然后你打电话
getSuit(suite);
But getSuit
only has switch branches for values between 0 and 3 inclusive:但
getSuit
仅对 0 到 3 之间的值有开关分支:
switch (suit)
{
case 0:
return "spades";
break;
case 1:
return "clubs";
break;
case 2:
return "diamonds";
break;
case 3:
return "hearts";
break;
default:
break;
}
Not returning a value from a function that is declared to return a value is undefined behaviour.不从声明为返回值的 function 中返回值是未定义的行为。
A few functions like getSuit
and getRank
in your code don't return a value if only the default case of their switch
statement is executed.如果仅执行
switch
语句的默认情况,则代码中的一些函数(如getSuit
和getRank
)不会返回值。
You can return an empty string in the default cases:在默认情况下,您可以返回一个空字符串:
default:
return ""; // empty string
And in the call site, check to see if the returned value is empty using the empty
function.并在调用站点中,使用
empty
function 检查返回值是否为空。
Another way is to use std::optional<T>
like below:另一种方法是使用
std::optional<T>
,如下所示:
std::optional<string> getSuit( const int suit )
{
switch (suit)
{
case 0:
return "spades";
case 1:
return "clubs";
case 2:
return "diamonds";
case 3:
return "hearts";
default:
return { }; // empty optional
}
}
And in the call site:在呼叫站点中:
std::optinal<std::string> type { getSuit(suite) };
if ( type ) // if optional has value
{
// extract and use the value inside of optional
type.value( );
}
Keep in mind that if the optional does not have a value, using value()
will throw.请记住,如果可选项没有值,则使用
value()
将抛出。 You can use value_or()
instead which does not throw.您可以使用
value_or()
代替它不会抛出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.