结构数组(预期表达式错误)C ++

时间:2014-09-25 18:39:39

标签: c++ arrays struct

我有以下代码。我试图用一副牌填充一个数组,并且我不断遇到相同的错误“预期的表达式错误”,无论我如何编写循环来填充数组。

任何人都可以看到我出错的地方。我觉得它很简单,我是C ++的新手,我只是缺少了。

谢谢!

#include <iostream>

using namespace std;

struct playingCard{
    char suit; // heart (1), club (2), spade (3), diamond (4)
    int value; // 1 to 13 (ace is LOW)

};

void printArray(playingCard playingCardArray[], int size){
    for (int i = 0; i < size; i ++){
        cout << playingCardArray[i].suit << ":\t" << playingCardArray[i].value << endl;
    }
}

int main()
{

    const int ARRAY_SIZE = 52;
    playingCard playingCardArray[ARRAY_SIZE];
    int i = 1;
    int suitLoop = 1;
    while (suitLoop == 1){
        for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
            playingCardArray[i] = {suitLoop, valueLoop},
        }
     }

    printArray(playingCardArray, ARRAY_SIZE);
    return 0;
}

2 个答案:

答案 0 :(得分:1)

要解决您的编译问题,请更改内部for循环,如下所示:

for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
    playingCardArray[i].suit = suitLoop;
    playingCardArray[i].value = valueLoop;
}

除了编译之外,您的代码还有无限循环,要解决此问题,您需要更改main,如下所示:

int main()
{
    const int ARRAY_SIZE = 52;
    playingCard playingCardArray[ARRAY_SIZE];
    int i = 1;
    int suitLoop = 0;
    while (suitLoop < ARRAY_SIZE){
        for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
            playingCardArray[suitLoop].suit = (suitLoop/13 + 1);
            playingCardArray[suitLoop++].value = valueLoop;
        }
     }

    printArray(playingCardArray, ARRAY_SIZE);
    return 0;
}

答案 1 :(得分:0)

playingCardArray[i] = {suitLoop, valueLoop},末尾用分号交换逗号可以解决问题。