簡體   English   中英

在我的代碼中被零除

[英]Having a divide by zero in my code

這是我的代碼,我不知道我在哪里得到零除問題。

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::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

我的問題是我無法弄清楚要解決的問題。 我已經閱讀了評論,但仍然感到困惑。

您正在分配(並復制!) Mreview對象的向量, 所有這些對象都使用默認的ctor構造 ,因此numRatings默認為0,並且您沒有做任何事情來確保從未在未修改的對象上調用aveRating()或至少保護自己不受numRatings為0的影響。

編輯:最簡單的解決方法:

double Mreview::aveRating() const { 
    if (numRatings == 0)
        return 0.;
    else 
        return static_cast<double>(totalScore)/numRatings;
}

進一步的修正,IMO,將存儲Mreview 指針 (理想的shared_ptr)在矢量和new荷蘭國際集團它們當第一得分要而非前面添加

我只看到一個實際的除法運算...

double Mreview::aveRating() const { 
   double rating = totalScore/numRatings; 
   return rating; 
}

...在那里嗎?

尋找除零問題時,請尋找/和%運算符。 我在您的代碼中只看到一個/。

不是除以零的錯誤,而是...

double Mreview::aveRating() const
{
    double rating = totalScore/numRatings;
    return rating;
}

您要獲取兩個整數值並將其存儲在double中,而無需進行類型轉換...您必須將totalScore或numRatings強制轉換為double,如:

double Mreview::aveRating() const
{
    double rating = (double)totalScore/numRatings;
    return rating;
}

您可以在調試器中運行它,然后告訴陷阱發生的位置嗎?

它看起來像如果線的長度修整后為0,然后printMrList()可以在不稱為review.addScore()第一被調用。 在這種情況下, printMrList()調用review.aveRating() ,嘗試將totalScore除以numRatings 這兩個值均為0,因此會出現被零除的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM