C++ 使用数据文件排序进行计算时遇到问题
我是stackoverflow社区的新手,也不熟悉编码,所以在我学习这里张贴的技巧以及编码规则的同时,请提前道歉。我用C++和AM在CS161初学的计算机课上。 我目前正在做一项作业,要求我从存储在计算机上的数据文件中读取数据,并对数据进行排序,以便进行一些计算。在这项作业中,我根据性别和学校类型查找平均考试分数。所有的东西都可以编译,程序也可以运行,但是有一些问题 第一个问题是我的回声C++ 使用数据文件排序进行计算时遇到问题,c++,if-statement,while-loop,counter,ifstream,C++,If Statement,While Loop,Counter,Ifstream,我是stackoverflow社区的新手,也不熟悉编码,所以在我学习这里张贴的技巧以及编码规则的同时,请提前道歉。我用C++和AM在CS161初学的计算机课上。 我目前正在做一项作业,要求我从存储在计算机上的数据文件中读取数据,并对数据进行排序,以便进行一些计算。在这项作业中,我根据性别和学校类型查找平均考试分数。所有的东西都可以编译,程序也可以运行,但是有一些问题 第一个问题是我的回声 // echo the data file while (inData) {
// echo the data file
while (inData)
{
inData >> name >> sex >> school >> score;
cout << name << sex << school << score << endl;
//回显数据文件
while(inData)
{
inData>>姓名>>性别>>学校>>分数;
cout>性别>>学校>>分数;
//提示用户文件位置
cout>文件源;
//打开输出文件并运行程序退出故障保护命令
打开(文件源);
如果(!inData)
{
cout school>>分数;
您的代码有一些问题。让我们一次处理一个问题,好吗
ifstream inData;
ofstream outData;
inData >> name >> sex >> school >> score;
最后一行是做什么的?inData
未打开,但您正在尝试从中读取
maleScore = maleScore +=score;
在这里(以及其他一些地方),您错误地使用了+=
运算符。您应该像这样使用+=
:
maleScore += score;
maleScore = maleScore + score;
或者像这样使用+
:
maleScore += score;
maleScore = maleScore + score;
然后执行以下操作:
if(school == CC)
现在,CC
是代码中的一个std::string
,您尚未初始化它(这意味着它是空的)。因此它永远不会匹配,因此,if
主体将永远不会执行。UN
也会发生同样的情况
再往下,循环中有一行:
maleAvg = maleScore/maleCount;
这样做的目的是每次通过循环重新计算男性的平均值。这不一定是错的(你会得到正确的结果)但是,如果文件中的第一个人是女性,您的程序将崩溃,因为malecont
将为0。如果输入文件中没有女性,或者没有大学或学院的分数,也会发生同样的情况
最后,在循环中使用iostream::eof()
不是一个好主意。您可以在StackOverflow上阅读更多关于这方面的内容:
综上所述,您的错误并不严重,而且是新程序员的典型错误,因此不要泄气。请将此视为学习和理解如何检查代码以发现此类错误的机会。因此,无需进一步讨论,让我们来看看此程序的改进版本:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std; // it's considered bad to do this - but since
// this is homework, we'll let it slide.
int main()
{
//Declare variables to manipulate data
string name;
string fileSource;
int maleScore = 0;
int femScore = 0;
int unScore = 0;
int ccScore = 0;
int femCount = 0;
int maleCount = 0;
int ccCount = 0;
int unCount = 0;
//Declare stream variables
ifstream inData;
ofstream outData;
// Promt user for file location
cout << "Please input file location: ";
cin >> fileSource;
// open output file and run program exit failsafe command
inData.open(fileSource);
if(!inData)
{
cout << "Cannot open input file. "
<< "Program will now terminate." << endl;
return 1;
}
cout << "Reading data from '" << fileSource << "'" << endl;
while(inData >> name)
{ // If we read a name, we can continue. Otherwise, we're done.
char sex;
int score;
string school;
inData >> sex >> school >> score;
// Write the data out
cout << "Processing " << name << " (" << sex << ") attending ";
if(school == "UN")
cout << "University";
else if(school == "CC")
cout << "Community College";
cout << ". Score = " << score << endl;
// Male and female calculations
if(sex=='M')
{
maleScore +=score;
maleCount++;
}
else if(sex =='F')
{
femScore +=score;
femCount++;
}
// Community college and University calculations
if(school == "CC")
{
ccScore +=score;
ccCount++;
}
else if(school == "UN")
{
unScore +=score;
unCount++;
}
}
// We do static_cast<double>(maleScore) / maleCount; to ensure that
// the division is done using floating point and not integer
// arithmetic. We could have multiplied the numerator by 1.0 instead.
if(maleCount != 0)
{
cout << "The average scores for males is: " << setprecision(2)
<< static_cast<double>(maleScore) / maleCount << endl;
}
if(femCount != 0)
{
cout << "The average score for females is: " << setprecision(2)
<< static_cast<double>(femScore) / femCount << endl;
}
if(ccCount != 0)
{
cout << "The average score for Community Colleges is: " << setprecision(2)
<< static_cast<double>(ccScore) / ccCount << endl;
}
if(unCount != 0)
{
cout << unScore << "/" << unCount << endl;
cout << "The average score for Universities is: " << setprecision(2)
<< static_cast<double>(unScore) / unCount << endl;
}
// In this case we will use the multiplication technique instead:
cout << "The 'sum' average is: " << setprecision(2)
<< (1.0 * (maleScore + femScore + ccScore + unScore)) / 12 << endl;
return 0;
}
#包括
#包括
#包括
#包括
使用namespace std;//这样做被认为是不好的,但是因为
//这是作业,我们顺其自然吧。
int main()
{
//声明变量以操作数据
字符串名;
字符串文件源;
int maleScore=0;
积分=0;
int unScore=0;
int ccScore=0;
int femCount=0;
int maleCount=0;
int ccCount=0;
int unCount=0;
//声明流变量
Iftream inData;
数据流输出;
//提示用户文件位置
cout>文件源;
//打开输出文件并运行程序退出故障保护命令
打开(文件源);
如果(!inData)
{
你能给我们看一个应该使用的输入文件的例子吗?这里有一个例子:Bailey M CC 68。第一列是字符串名Bailey,第二列是字符M是性别,第三列是学校字符串CC(另一个选项是大学的UN),最后一列的分数是整数68。文件中有一个列表。另外,我将while(inData)改为while(!inData.eof())。这样更好吗?我想while(inData>>name>>sex>>school>>分数)
会更好。谢谢你。尽管如此,即使做了更改,我仍然有所有这些问题。没有任何更改。它仍然没有进行任何计算,并且一直在运行,直到我手动停止。不过,我主要关心的是如何运行我的计算。有什么想法吗?它看起来很可靠,我不明白它为什么不计算。哇!你在一次毁灭性的逻辑打击中解决了我所有的担忧哈哈。我尽了最大努力想看看我能在那里整合什么,但我在一个基础课程中,所以我们现在才开始讨论数组。我唯一能够提高设置精度的事情是。我在计算任何数组之前放置了这条线平均值:正如我所说,这不是一个坏的努力-你是编程新手,在“进入区域”之前需要一些时间和练习。不要气馁-继续努力。我们都是新程序员。祝你好运!