获得奇怪的价值
Getting strange value
我目前正在学习 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
函数的后续结果,availableAverage
输出为 -1.157e+62
.
我确定问题与我如何将值从一个函数传递到另一个函数有关(我怀疑我做的是否正确)。
我该如何解决这个问题?
提前致谢。
问题不在于函数,而在于变量。
我们以quizPts
为例:
在 main 方法中,您声明了这个变量,但是在将它发送到 currentPoints
之前您不对其进行任何操作。因此当你这样做时它有一个未定义的值(未定义在 C 中通常看起来像随机的)。
您在 quizAverage 中使用的另一个变量 quizPts
具有相同的名称,但对于编译器而言则不同。
在你的主目录中尝试:
quizPts = quizAverage();
您需要像这样存储 currentPoints() 函数的 return 值。
totalPts = currentPoints(quizPts, labPts, teamPts, homeworkAverage, exam1Pts, exam2Pts);
currentAverage(totalPts);
原因是,您在 currentPoints() 中将 "totalPts" 声明为局部变量。
"Local variables has function scope only, it is undefined to main function".
对所有其他人执行此操作
函数(quizAverage、labAverage、teamProject、exam1、exam2、hwAverage、currentAverage)
希望这能解决问题!!!
你问了
How would I go about fixing this issue?
答案是 "Use the debugging tool with "watches" window 在您最喜欢的 IDE 中打开"。
仅仅通过重新阅读代码总是很难找到错误,但在调试器中,您可以在每个时刻看到变量的所有值。具体来说,在这个例子中,你会意识到你的变量从一开始就有垃圾值(没有初始化),而且这个值永远不会改变。
使用这种方法,您可以及时找到自己的原因,而不是写这个 SO 问题所需的时间。我希望这能帮助你节省你未来的时间。
问题是您使用了诸如 quizPts 和 labPts 之类的变量,但没有在其中存储任何值。在您的情况下,您必须在使用函数之前将函数的 return 值存储到相应的变量中。例如,执行与以下语句相同的操作:
quizPts = quizAverage();
我目前正在学习 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
函数的后续结果,availableAverage
输出为 -1.157e+62
.
我确定问题与我如何将值从一个函数传递到另一个函数有关(我怀疑我做的是否正确)。
我该如何解决这个问题?
提前致谢。
问题不在于函数,而在于变量。
我们以quizPts
为例:
在 main 方法中,您声明了这个变量,但是在将它发送到 currentPoints
之前您不对其进行任何操作。因此当你这样做时它有一个未定义的值(未定义在 C 中通常看起来像随机的)。
您在 quizAverage 中使用的另一个变量 quizPts
具有相同的名称,但对于编译器而言则不同。
在你的主目录中尝试:
quizPts = quizAverage();
您需要像这样存储 currentPoints() 函数的 return 值。
totalPts = currentPoints(quizPts, labPts, teamPts, homeworkAverage, exam1Pts, exam2Pts); currentAverage(totalPts);
原因是,您在 currentPoints() 中将 "totalPts" 声明为局部变量。
"Local variables has function scope only, it is undefined to main function".
对所有其他人执行此操作 函数(quizAverage、labAverage、teamProject、exam1、exam2、hwAverage、currentAverage)
希望这能解决问题!!!
你问了
How would I go about fixing this issue?
答案是 "Use the debugging tool with "watches" window 在您最喜欢的 IDE 中打开"。
仅仅通过重新阅读代码总是很难找到错误,但在调试器中,您可以在每个时刻看到变量的所有值。具体来说,在这个例子中,你会意识到你的变量从一开始就有垃圾值(没有初始化),而且这个值永远不会改变。
使用这种方法,您可以及时找到自己的原因,而不是写这个 SO 问题所需的时间。我希望这能帮助你节省你未来的时间。
问题是您使用了诸如 quizPts 和 labPts 之类的变量,但没有在其中存储任何值。在您的情况下,您必须在使用函数之前将函数的 return 值存储到相应的变量中。例如,执行与以下语句相同的操作: quizPts = quizAverage();