上週在我的計算機科學類,我們編寫了載有進口的「卡」,從文件中像這樣的功能的程序:如何使用指針與結構成員
struct card
{
char rank[10];
char suit[10];
char color;
bool dealt;
char location[12];
};
。
void importCard(card deck[])
{
ifstream fin;
fin.open("deck.txt");
int index;
for(index=0;index<52;index++)
{
fin >> deck[index].rank;
fin >> deck[index].suit;
if(deck[index].suit[0]==('d')||deck[index].suit[0]==('h'))
{
deck[index].color='r';
}
else
{
deck[index].color='b';
}
deck[index].dealt=false;
}
}
基本上,我HEAVILY在這個函數中使用點運算符。
現在,我的任務是修改這個使用指針代替所有方括號。但是,如果我使用類似
fin >> *deckPointer.suit;
因爲指針不能與點運算符一起使用,所以它不起作用。那麼,我該如何編寫這個循環遍歷所有的卡?我知道如何將它寫入循環一張卡片,但我不知道如何通過所有卡片循環。另外,我不能使用任何其他更高級的工具,如矢量,所以不要這麼做。此外,我只允許用++和 - 增加指針,而不是任何其他數量。
正確使用括號,可以使用*和。在一起,但使用 - >更好。 – 2013-02-14 19:12:43
如果這是一個C++課程,請讓你的教師允許你使用適當的C++類型作爲'std :: string',而不是結構中的字符數組。 – 2013-02-14 19:12:54