【发布时间】:2020-04-20 18:56:33
【问题描述】:
您好再次堆栈溢出用户!我有一个新程序,但又遇到了一个我无法弄清楚的问题!我写了一个程序,数学导师程序,实际上已经完成了,只是有些东西我想不通。在程序中,有一个 void 函数可以检查答案(如果用户输入正确或不正确,将显示)但我似乎无法让它工作。当我在我的 doOneSet void 函数中有它时(确实是一组或问题),即使答案是正确的,它似乎也只显示“不正确”?我似乎无法弄清楚我做错了什么或缺少了什么。感谢任何类型的帮助/提示/参考。谢谢!
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void getProbsPerSet (int& numProbs);
void printHeader (/*in*/ char problemType);
void getMaxNum(/* out */int& maxNum);
void generateOperands(int& num1, int& num2, int maxNum);
void checkAnswer (/* in */int num1,/* in */ int num2, /*out*/ int& answer);
void calcCorrectAnswer(/* in */ char problemType,/* in */ int num1,/* in */int num2, /*inout*/ int& answer);
void doOneProblem (char problemType, int maxNum);
void doOneSet (char problemType, int probsPerSet, int&);
void printReport (/* in */ int probsPerSet, int& set1Correct, /* in */int& set2Correct, /* in */int& set3Correct);
int main ()
{
int set1Correct, set2Correct, set3Correct, probsPerSet, maxNum;
srand(time(0));
getProbsPerSet (probsPerSet);
cout << endl;
doOneSet ('+', probsPerSet, set1Correct);
cout << endl;
doOneSet ('-', probsPerSet, set2Correct);
cout << endl;
doOneSet ('*', probsPerSet, set3Correct);
cout << endl;
return 0;
}
void getProbsPerSet (int& numProbs)
{
cout << "Enter problems per set: ";
cin >> numProbs;
cout << endl;
while (numProbs < 3 || numProbs > 10)
{
cout << endl;
cout << "Please stay between 3 and 10. Thank you!";
cout << endl;
cout << endl;
cout << "Enter problems per set: ";
cin >> numProbs;
cout << endl;
}
}
void printHeader (/*in*/ char problemType)
{
switch(problemType)
{
case '+': cout << endl;
cout << "Set # 1" << endl;
cout << "----------" << endl;
break;
case '-': cout << endl;
cout << "Set # 2" << endl;
cout << "----------" << endl;
break;
case '*': cout << endl;
cout << "Set # 3" << endl;
cout << " ----------" << endl;
break;
}
}
void doOneProblem (char problemType, int maxNum)
{
int num1,num2,answer;
generateOperands(num1, num2, maxNum);
switch (problemType)
{
case '+' : cout << num1 << problemType << num2 << " = ";
cin >> answer;
break;
case '-' : cout << num1 << problemType << num2 << " = ";
cin >> answer;
break;
case '*' : cout << num1 << problemType << num2 << " = ";
cin >> answer;
break;
}
}
void doOneSet (char problemType, int probsPerSet, int& answer)
{
int num1, num2, numProbs, maxNum;
bool isCorrect;
printHeader(problemType);
getMaxNum(maxNum);
for (int count = 0; count < probsPerSet; count++)
{
generateOperands(num1, num2, maxNum);
doOneProblem (problemType, maxNum);
calcCorrectAnswer(problemType, num1, num2, answer);
checkAnswer (num1, num2, answer);
}
}
void generateOperands(int& num1, int& num2, int maxNum)
{
num1 = 1 + rand() % maxNum;
num2 = 1 + rand() % maxNum;
}
void getMaxNum(/*out*/ int& maxNum)
{
cout << "What is the maximum number for this set?: ";
cin >> maxNum;
}
void checkAnswer (int num1, int num2, /*out*/ int& answer)
{
bool isCorrect;
if (answer == isCorrect)
{
cout << endl;
cout << "Correct!" << endl;
cout << endl;
}
else
{
cout << endl;
cout << "Incorrect!" << endl;
cout << endl;
}
}
void calcCorrectAnswer(/* in */ char problemType,/* in */ int num1,/* in */int num2, /*inout*/ int& answer)
{
bool isCorrect;
switch (problemType)
{
case '+' : isCorrect = num1 + num2;
break;
case '-' : isCorrect = num1 - num2;
break;
case '*' : isCorrect = num1 * num2;
break;
}
}
void printReport (/* in */ int probsPerSet, int& set1Correct, /*in*/ int& set2Correct, /*in*/ int& set3Correct)
{
int set1Percent = 0, set2Percent = 0, set3Percent = 0;
int total, complete;
int numcorrect = 1;
total = set1Correct + set2Correct + set3Correct;
complete = probsPerSet + probsPerSet + probsPerSet;
set1Percent = (100 * set1Correct) / probsPerSet;
set2Percent = (100 * set2Correct) / probsPerSet;
set3Percent = (100 * set3Correct) / probsPerSet;
cout << endl;
cout << "Set #1 : You got " << set1Correct << " correct out of " << probsPerSet << " for " << set1Percent << "%" << endl;
cout << "Set #2 : You got " << set2Correct << " correct out of " << probsPerSet << " for " << set2Percent << "%" << endl;
cout << "Set #3 : You got " << set3Correct << " correct out of " << probsPerSet << " for " << set3Percent << "%" << endl;
cout << endl;
cout << "Overall you got " << total << " out of " << complete << endl;;
}
【问题讨论】:
-
您正在将
int的引用与checkAnswer函数中的单元化bool进行比较。 -
我建议减少输出参数的数量,而是尽可能从函数中返回值。它会让你的代码更容易理解。
标签: c++