C++ 在我的代码中有一个被零除的数
他是我的代码,我不知道从哪里得到零除问题 mreviewApp.cppC++ 在我的代码中有一个被零除的数,c++,divide-by-zero,C++,Divide By Zero,他是我的代码,我不知道从哪里得到零除问题 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 searc
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::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
#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
\ifndef MREVIEW\u H
#定义MREVIEW_H
#包括
使用名称空间std;
类Mreview
{
公众:
Mreview(string ttl=“不适用”);
Mreview(字符串ttl,int firstScore);
字符串getTitle()常量{return title;}
int getTotalScore()常量{return totalScore;}
int getNumRatings()常量{return numRatings;}
无效添加分数(整数分数);
双平均()常数;
私人:
字符串标题;
积分总分;
国际数字;
};
#恩迪夫
我的问题是我无法弄清楚我必须做什么来解决这个问题。我读了这些评论,仍然感到困惑。我只看到一个实际的除法运算
double Mreview::aveRating() const {
double rating = totalScore/numRatings;
return rating;
}
…是吗?我只看到一个实际的除法运算
double Mreview::aveRating() const {
double rating = totalScore/numRatings;
return rating;
}
…就是这样吗?在查找被零除的问题时,请查找/和%运算符。我在代码中只看到一个/。在查找被零除的问题时,请查找/和%运算符。我在你的代码中只看到一个/。你能在调试器中运行它并让它告诉你陷阱发生在哪里吗?你能在调试器中运行它并让它告诉你陷阱发生在哪里吗?不是被零除的错误,而是
double Mreview::aveRating() const
{
double rating = totalScore/numRatings;
return rating;
}
您正在获取两个整数值并将其存储在一个double中,而无需进行类型转换。。。您必须将totalScore或numRatings转换为双精度,如:
double Mreview::aveRating() const
{
double rating = (double)totalScore/numRatings;
return rating;
}
不是被零除的错误,而是
double Mreview::aveRating() const
{
double rating = totalScore/numRatings;
return rating;
}
您正在获取两个整数值并将其存储在一个double中,而无需进行类型转换。。。您必须将totalScore或numRatings转换为双精度,如:
double Mreview::aveRating() const
{
double rating = (double)totalScore/numRatings;
return rating;
}
您正在分配(并复制!)一个Mreview
对象的向量,所有这些对象都是使用默认的构造函数构建的,因此numRatings
默认为0,并且您没有做任何事情来确保平均()
从不在未修改的对象上调用,或者至少保护自己不受numRatings
为0的影响
编辑:最简单的修复:
double Mreview::aveRating() const {
if (numRatings == 0)
return 0.;
else
return static_cast<double>(totalScore)/numRatings;
}
double Mreview::aveRating()常量{
如果(数值==0)
返回0。;
其他的
返回静态施法(总分)/数值;
}
IMO,进一步的修复方法是将Mreview
指针(理想情况下是共享的)存储在向量中,并在添加第一个分数时而不是更早地添加它们。您正在分配(并复制!)一个Mreview
对象的向量,所有这些对象都是用默认的ctor构建的,因此,numRatings
默认为0,并且您没有做任何事情来确保aveRating()
从不在未修改的对象上调用,或者至少保护自己不受numRatings
为0的影响
编辑:最简单的修复:
double Mreview::aveRating() const {
if (numRatings == 0)
return 0.;
else
return static_cast<double>(totalScore)/numRatings;
}
double Mreview::aveRating()常量{
如果(数值==0)
返回0。;
其他的
返回静态施法(总分)/数值;
}
另一个解决办法是,IMO将
Mreview
指针(理想情况下是共享的)存储在向量中,并new
在添加第一个分数时而不是更早地添加它们。看起来,如果修剪后的行长度为0,则可以调用printMrList()
,而无需review.addScore()
首先被调用。在这种情况下,printMrList()
调用review.aveRating()
,该函数试图将totalScore
除以numRatings
。这两个值都是0,因此会发生被零除的错误。看起来,如果修剪后的行长度为0,则可以调用printMrList()
,而无需先调用review.addScore()
。在这种情况下,printMrList()
调用review.aveRating()
,该函数试图将totalScore
除以numRatings
。这两个值都是0,因此会出现被零除的错误。可能需要修复问题的语法。问问Jon SKeet或Scott Hanselman,他们对此没有问题嘿!您正在使用Dev-C++。。。猜猜我怎么说?可能想修正你的问题的语法。问问Jon SKeet或Scott Hanselman他们没有问题嘿!您正在使用Dev-C++。。。猜猜我怎么知道?我只是不知道如何解决这个问题。按照保罗的建议去做。这是最简单的解决办法。就我个人而言,我不会在没有指针的向量中存储此类类:我会使默认的ctor显式,并且只有在我有第一个分数时才向向量中添加类的新实例。呃,更确切地说,Paul的建议是测试numRatings而不是totalScore。对不起,我只是不知道该如何解决这个问题。听保罗的建议。这是最简单的解决办法。就我个人而言,我不会在没有指针的向量中存储此类类:我会使默认的ctor显式,并且只有在我有第一个分数时才向向量中添加类的新实例。呃,更确切地说,Paul的建议是测试numRatings而不是totalScore。很抱歉