gpt4 book ai didi

c++ - 使用数据文件排序计算时遇到问题

转载 作者:行者123 更新时间:2023-11-28 07:46:05 25 4
gpt4 key购买 nike

我是 stackoverflow 社区的新手,也是编码的新手,所以提前道歉,因为我学习了在这里发帖的技巧以及编码规则。我正在使用 C++,并且正在参加 CS161 初级计算机科学类(class)。

我目前正在做一项作业,要求我从存储在计算机上的数据文件中读取并对数据进行排序以便进行一些计算,在这项作业中,计算是根据性别和类型找出平均考试分数学校的。一切都编译并且程序运行但是有一些问题。

第一个问题是我的回声。

    // echo the data file
while (inData)
{
inData >> name >> sex >> school >> score;
cout << name << sex << school << score << endl;

该程序确实回显了数据,但由于某种原因它最终回显了列表中的姓氏两次。此外,(我不知道这是否重要)当它回显时,它不会跳过姓名、性别、学校和分数之间的空格。

第二个问题是它没有执行计算,我认为这是因为我缺少某种与“计数”相关的指令,但我无法做到这一点。

这是我的代码,让我知道你的想法:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//Declare variables to manipulate data
char sex;
string name;
string school;
string fileSource;
string CC;
string UN;
int maleScore = 0;
int femScore = 0;
int unScore = 0;
int ccScore = 0;
double maleAvg;
double femAvg;
double unAvg;
double ccAvg;
double sumAvg = 0;
int femCount = 0;
int maleCount = 0;
int ccCount = 0;
int unCount = 0;
int score;
int sum;
//Declare stream variables
ifstream inData;
ofstream outData;
inData >> name >> sex >> school >> score;
// 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;
}
outData << fixed << showpoint << setprecision(2);

// echo the data file
while (inData)
{
inData >> name >> sex >> school >> score;
cout << name << sex << school << score << endl;

// while reading incoming data from file, execute the conditions

// Male and female calculations
if(sex=='M')
{
maleScore = maleScore +=score;
++maleCount;
}
else if(sex =='F')
{
femScore = femScore +=score;
++femCount;
}

// Community college and University calculations
if(school == CC)
{
ccScore = ccScore +=score;
++ccCount;
}
else if(school == UN)
{
unScore = unScore +=score;
++unCount;
}
maleAvg = maleScore/maleCount;
}

// Male average output
cout << maleAvg;

femAvg = femScore/femCount;

// Female average output
cout << femAvg;

ccAvg = ccScore/ccCount;

// Community College average output
cout << ccAvg;

unAvg = unScore/unCount;

// University average output
cout << unAvg;
sum = maleScore + femScore + ccScore + unScore;
sumAvg = sum/12;
cout << sumAvg;
return 0;
}

此外,我的编译器一直在运行程序,并没有停止。我拍了一张编译器窗口的照片,但不知道如何发布。

最佳答案

您的代码存在一些问题。让我们一次一个地处理事情,好吗?

ifstream inData;
ofstream outData;
inData >> name >> sex >> school >> score;

最后一行是做什么的? inData 未打开,但您正在尝试从中读取数据。

maleScore = maleScore +=score;

此处(以及其他一些地方)您错误地使用了 += 运算符。您应该像这样使用 +=:

maleScore += score;

或者像这样使用+:

maleScore = maleScore + score;

然后你这样做:

if(school == CC)

现在,CC 是您代码中的一个 std::string,您尚未对其进行初始化(这意味着它是空的)。所以它永远不会匹配,因此 if 主体永远不会执行。 UN 也会发生同样的事情。

再往下,循环中有这一行:

maleAvg = maleScore/maleCount;

这样做是在每次循环中重新计算男性的平均值。这不一定是错误的(你会得到正确的结果)但如果文件中的第一个人是女性,你的程序将因被零除而崩溃,因为 maleCount 将为 0。同样的事情如果输入文件中没有女性,或者没有大学或学院的分数,就会发生这种情况。

最后,在循环中使用 iostream::eof() 不是一个好主意。您可以在 StackOverflow 上阅读更多相关信息:Why is iostream::eof inside a loop condition considered wrong?

尽管如此,您的错误并不严重,而且是新程序员的典型错误,所以不要气馁。将此视为学习和了解如何检查代码以发现此类错误的机会。事不宜迟,让我们看看这个程序的改进版本:

#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;
}

关于c++ - 使用数据文件排序计算时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14863525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com