【发布时间】:2017-04-23 10:03:26
【问题描述】:
我目前正在学习 C++ 中的函数,目前正在做一项家庭作业,其中函数是主要内容。
目前,我正在尝试制作一个成绩计算器,该过程的每个操作都被拆分为自己的函数。
代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
void getHWGrades(int homeworks[], int size)
{
cout << "\nEnter the grades, out of 100 points, for the 9 homeworks you completed." << endl;
cout << "Note that Homework 10 is given to you for free, but is the same grade \nas homework 9.\n" << endl;
for (int i = 0; i < 9; i++)
{
cout << "Homework " << i + 1 << ": ";
cin >> homeworks[i];
while (homeworks[i] > 100 || homeworks[i] < 0)
{
cout << "Invalid grade, input homework grade again: ";
cin >> homeworks[i];
}
}
homeworks[9] = homeworks[8];
cout << "Homework 10: " << homeworks[9];
}
double quizAverage()
{
double quizPts;
cout << "Input your in class quiz average: ";
cin >> quizPts;
return quizPts;
}
double labAverage()
{
double labPts;
cout << "Input your lab average: ";
cin >> labPts;
return labPts;
}
double teamProject()
{
double teamPts;
cout << "Input your team project grade: ";
cin >> teamPts;
return teamPts;
}
int exam1()
{
int exam1Pts;
cout << "Input your exam1 grade: ";
cin >> exam1Pts;
return exam1Pts;
}
int exam2()
{
int exam2Pts;
cout << "Input your exam2 grade: ";
cin >> exam2Pts;
return exam2Pts;
}
double hwAverage(int homeworks[], int size)
{
double total = 0;
double homeworkAverage = 0;
for (int i = 0; i < size; i++)
{
total = total + homeworks[i];
}
homeworkAverage = (total*1.0) / 10;
return homeworkAverage;
}
double currentPoints(double& quizPts, double& labPts, double& teamPts, double& homeworkAverage, int& exam1Pts, int& exam2Pts)
{
double totalPts = ((quizPts / 100.0) * 10) + ((labPts / 100.0) * 10) + ((teamPts / 100.0) * 15) + ((homeworkAverage / 100.0) * 20) + ((exam1Pts / 100.0) * 10) + ((exam2Pts / 100.0) * 15);
cout << "\nYour current points (out of the 80 total available), stand at: " << totalPts;
return totalPts;
}
double currentAverage(double& totalPts)
{
double availableAverage = totalPts*(100.0 / 80);
cout << "\nYour current average is: " << availableAverage;
return availableAverage;
}
int main()
{
// keep the console from closing in visual studio
char charer;
double totalPts;
double quizPts, labPts, teamPts, homeworkAverage;
int exam1Pts, exam2Pts;
const int ARRAY_SIZE = 10;
int hwArray[ARRAY_SIZE];
getHWGrades(hwArray, ARRAY_SIZE);
quizAverage();
labAverage();
teamProject();
exam1();
exam2();
currentPoints(quizPts, labPts, teamPts, homeworkAverage, exam1Pts, exam2Pts);
currentAverage(totalPts);
cin >> charer;
}
我认为我的问题在于函数currentPoints 和currentAverage,当我运行此totalPts 输出为-5.09078e+61 并作为currentAverage 函数@987654327 的后续结果@ 输出为-1.157e+62。
我确定问题与我如何将值从函数传递到函数有关(我怀疑我做对了)。
我将如何解决这个问题?
提前谢谢你。
【问题讨论】:
-
double quizPts, labPts, teamPts, homeworkAverage;当你调用你的currentPoints函数时,这些都是未初始化的。 -
您应该做的第一件事就是将其简化为相关的核心,既要自己理解问题,又要让其他人可以帮助您。我怀疑所有 127 行都导致了这个问题。例如,您可以安全地删除像
quizAverage()这样不会更改全局状态并且忽略返回值的函数。 -
您将未初始化的值传递给函数 currentPoints。您必须像 quizPts=quizAverage(); labPts=labAverage();....等等,totalPts= currentPoints(quizPts, labPts, teamPts, homeworkAverage,exam1Pts,exam2Pts);
-
Heinrich - 是的,我也对此表示怀疑,只是我在调用其他函数的值时遇到问题,所以我认为最好包含相关信息。 user1438832 - 感谢您指出如何解决它!
标签: c++ function parameter-passing pass-by-reference pass-by-value