(c++)无法让 void 函数工作,主.cpp内部的几行也有问题

(c++) Cannot get void functions to work, having trouble with a few lines inside of the main.cpp as well

本文关键字:内部 几行 有问题 cpp c++ void 工作 函数      更新时间:2023-10-16

我在下面输入的代码遇到了一些问题。void 函数似乎很好,但它们不起作用。我不确定我做错了什么,所以任何帮助都值得赞赏。另外,我不知道如何使用 if 语句从两个玩家的文件中提出问题,如下所示。拜托,我必须在 11 月 12 日晚上 3 点上交,任何帮助将不胜感激。我已经工作了 5 个小时了

如果语句在此处向玩家 1 显示奇数个问题,向玩家 2 显示偶数个问题,请确保通过调用函数 upDateScoreDouble 为相应玩家添加正确的分数

void getHistory (playerHistory pHist[], int loopy, ifstream& inQuest)
{
int i = 0 ;
loopy = 22 ;
while( i < loopy, i++)
{
inQuest >> pHist[i].alias1 ;
inQuest >> pHist[i].score ;
inQuest >> pHist[i].difficulty ;
inQuest >> pHist[i].date ;
}
return ;
}

void bubblesort (playerHistory pHist[], int length)
{
bool swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < length - j; i++) {
if (pHist[i].score > pHist[i + 1].score) {
tmp = pHist[i].score;
pHist[i].score = pHist[i + 1].score;
pHist[i + 1].score = tmp;
swapped = true;
}
}
}
return ;
}
///3.3.6 create a loop to call the getQuestion function for each question
for(int loopy=1; loopy<=numQuestions; loopy++)
{
///3.3.6.2 getQuestion function call
getQuestion(gameQ, numQuestions, inQuest) ;
///3.3.6.1 if statement here to display odd number of questions to player1 and even number of questions to player2
///make sure to add the correct score for respective player by calling the function upDateScoreDouble
if(loopy < numQuestions)
{
inQuest >> gameQ[loopy].question;
cout << gameQ[loopy].question ;
cout << gameQ[loopy].answer ;
cout << gameQ[loopy].ans1 ;
cout << gameQ[loopy].ans2 ;
cout << gameQ[loopy].ans3 ;
cout << gameQ[loopy].ans4 ;
}

}   ///end of for loop of number of questions

getHistory函数中,看起来你想要一个for循环。

因为表达式i < loopy, i++计算i < loopy然后丢弃结果

相反,循环的条件是i++,并且由于它在第一次迭代中0,结果是假的,循环永远不会发生。

阅读有关逗号运算符的更多信息,例如此参考。

你可能想要类似的东西

for (int i = 0; i < 22; ++i) { ... }