gpt4 book ai didi

C++ Card 相关程序将无法编译。 (运行时错误)

转载 作者:行者123 更新时间:2023-11-28 06:31:34 24 4
gpt4 key购买 nike

有谁知道为什么我的程序会出现运行时错误并崩溃?我似乎无法弄清楚。似乎没有调试选项。它只是在启动时崩溃。请帮助我完整地回答我的问题并善待它。希望大家能帮帮我。

#include <cstdlib>
#include <iostream>
#include <ctime>
#include <math.h>
using namespace std;

class Card {
private:
string suit;
string face;
int number;

public:
Card (string cardsuit , int cardnumber){
suit = cardsuit;
number = cardnumber;
}

Card () {
suit = "";
number = 0;
}

void setcard (string cardsuit){
suit = cardsuit;
}

void setnumber( int cardnumber){
number = cardnumber;
}

void display() {
if(number == 0) {
cout << "" ;
}
else
(cout << number << " of " << suit << endl) ;
}
};

int main(int argc, char *argv[])
{
Card acard[52];

for (int i = 0; i <= 13; i++){
acard[i] = Card("Hearts " , i+1);
}

for (int i = 0; i <= 13; i++) {
acard[i+13] = Card("Diamonds " , i+1);
}

for (int i = 0; i <= 13; i++) {
acard[i+26] = Card("Clubs " , i+1);
}

for (int i = 0; i <= 13; i++) {
acard[i+39] = Card("Spades " , i+1);
}

for (int i = 0; i < 52; i++) {
acard[i].display();
}

cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}

最佳答案

逻辑错误...

您在功能上用“方 block 1”覆盖了“红心 14”。您也可以对其余花色执行此操作。

acard[i] = Card("Hearts " , i+1);      // i is 13
acard[i+13] = Card("Diamonds " , i+1); // i is 0

写入超出数组末尾...

在最后一个 for 循环中,最后一次迭代在数组末尾之外写入:

acard[i+39] = Card("Spades " , i+1);   // i is 13

还有...

Card::display() 中将 cout 表达式放在圆括号中是很奇怪的。

关于C++ Card 相关程序将无法编译。 (运行时错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27436780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com