[英]Having a divide by zero in my code
Hes my code and I can't figure out where I'm getting the divide by zero problem. 这是我的代码,我不知道我在哪里得到零除问题。
mreviewApp.cpp mreviewApp.cpp
const int SIZE = 80;
const char DELIMIT = '|';
void parseLine(const char line[], string& title, int& rating);
void stringTrim(char st[]);
void printMrList(std::vector <Mreview> mrList);
Mreview searchTitle(std::vector <Mreview> &mrList, string title);
int main()
{
ifstream fin;
fin.open("rating_list.txt");
if (fin.fail()) {
cerr << "Input file opening error.\n";
exit(1);
}
char line[SIZE];
string title;
int rating;
int lineCount = 0;
std::vector <Mreview> mrList;
/* Process one line at a time */
// Read the first line
fin.getline(line, SIZE);
stringTrim(line);
// Process loop
while (strlen(line) != 0) {
parseLine(line, title, rating);
lineCount++;
Mreview review = searchTitle(mrList, title);
review.addScore(rating);
// Read the next line
fin.getline(line, SIZE);
stringTrim(line);
}
cout << "** PROCESS DONE. There were " << mrList.size() << " movie titles. **\n\n";
printMrList(mrList);
// Close the input file before exit.
fin.close();
system("Pause");
return 0;
}
void parseLine(const char line[], string& title, int& rating)
{
char s[SIZE], r[SIZE];
const char *ptr, *temp1;
char *temp2;
ptr = strchr(line, DELIMIT);
if (ptr != NULL) {
// First grab the title string (until '|').
temp1 = line;
temp2 = s;
while (temp1 != ptr)
*temp2++ = *temp1++;
*temp2 = '\0';
stringTrim(s);
title = s;
// Second grab the rating number
temp1 = ptr+1;
temp2 = r;
while (*temp1 != '\0')
*temp2++ = *temp1++;
*temp2 = '\0';
stringTrim(r);
rating = atoi(r);
}
else {
title = "";
rating = 0;
}
}
void stringTrim(char st[])
{
char* ptr;
for (ptr = st; *ptr; ptr++) ;
for (ptr--; *ptr == ' ' && ptr >= st; ptr--) ;
ptr++;
*ptr = '\0';
for (ptr = st; *ptr && *ptr == ' '; ptr++) ;
if (*ptr && ptr != st) {
char* ptr2;
for (ptr2 = st; *ptr; *ptr2++ = *ptr++) ;
*ptr2 = '\0';
}
}
void printMrList(std::vector <Mreview> mrList)
{
std::vector<Mreview>::iterator itr;
for(itr = mrList.begin(); itr != mrList.end(); itr++) {
Mreview review = *(itr);
cout << review.getTitle() << "\t\t" << review.getTotalScore() << "\t\t" << review.aveRating() << endl;
}
}
Mreview searchTitle(std::vector <Mreview> &mrList, string title)
{
Mreview review (title);
std::vector<Mreview>::iterator itr;
for(itr = mrList.begin(); itr != mrList.end(); itr++) {
Mreview r2d2 = *(itr);
if(review.getTitle() == r2d2.getTitle())
return r2d2;
}
mrList.push_back(review);
return review;
}
mreview.cpp mreview.cpp
Mreview::Mreview(string ttl)
: title(ttl), totalScore(0), numRatings(0) {}
Mreview::Mreview(string ttl, int score)
: title(ttl), totalScore(score), numRatings(1) {}
void Mreview::addScore(int score)
{
this->totalScore += score;
this->numRatings += 1;
}
double Mreview::aveRating() const
{
double rating = totalScore/numRatings;
return rating;
}
mreview.h mreview.h
#ifndef MREVIEW_H
#define MREVIEW_H
#include <string>
using namespace std;
class Mreview
{
public:
Mreview(string ttl = "N/A");
Mreview(string ttl, int firstScore);
string getTitle() const { return title; }
int getTotalScore() const { return totalScore; }
int getNumRatings() const { return numRatings; }
void addScore(int score);
double aveRating() const;
private:
string title;
int totalScore;
int numRatings;
};
#endif
My problem is that I am unable to figure out what I have to do to fix the problem. 我的问题是我无法弄清楚要解决的问题。 I have read the comments and I am still confused.
我已经阅读了评论,但仍然感到困惑。
You're allocating (and copying!) a vector of Mreview
objects, all of which are being constructed with the default ctor , so numRatings
defaults to 0, and you are doing nothing to ensure that aveRating()
is never called on unmodified objects or at the very least protects itself from numRatings
being 0. 您正在分配(并复制!)
Mreview
对象的向量, 所有这些对象都使用默认的ctor构造 ,因此numRatings
默认为0,并且您没有做任何事情来确保从未在未修改的对象上调用aveRating()
或至少保护自己不受numRatings
为0的影响。
Edit: Easiest fix: 编辑:最简单的解决方法:
double Mreview::aveRating() const {
if (numRatings == 0)
return 0.;
else
return static_cast<double>(totalScore)/numRatings;
}
A further fix, IMO, would be to store Mreview
pointers (ideally shared_ptr) in the vector and new
ing them when the first score is to be added rather than earlier. 进一步的修正,IMO,将存储
Mreview
指针 (理想的shared_ptr)在矢量和new
荷兰国际集团它们当第一得分要而非前面添加 。
I only see one actual division operation... 我只看到一个实际的除法运算...
double Mreview::aveRating() const {
double rating = totalScore/numRatings;
return rating;
}
...so is it there? ...在那里吗?
When looking for divide by zero problems, look for / and % operators. 寻找除零问题时,请寻找/和%运算符。 I see only one / in your code.
我在您的代码中只看到一个/。
Not the divide by zero error, but... 不是除以零的错误,而是...
double Mreview::aveRating() const
{
double rating = totalScore/numRatings;
return rating;
}
You are taking two integer values and storing it in a double without typecasting... you have to cast either totalScore or numRatings to a double like: 您要获取两个整数值并将其存储在double中,而无需进行类型转换...您必须将totalScore或numRatings强制转换为double,如:
double Mreview::aveRating() const
{
double rating = (double)totalScore/numRatings;
return rating;
}
您可以在调试器中运行它,然后告诉陷阱发生的位置吗?
It looks like if the length of the line is 0 after trimming, then printMrList()
can be called without review.addScore()
being called first. 它看起来像如果线的长度修整后为0,然后
printMrList()
可以在不称为review.addScore()
第一被调用。 In that case, printMrList()
calls review.aveRating()
which tries to divide totalScore
by numRatings
. 在这种情况下,
printMrList()
调用review.aveRating()
,尝试将totalScore
除以numRatings
。 Both of these values are 0, so a divide by zero error occurs. 这两个值均为0,因此会出现被零除的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.