我的代码,我无法弄清楚我在哪里得到除零问题.
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::vectormrList); Mreview searchTitle(std::vector &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 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 mrList) { std::vector ::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 &mrList, string title) { Mreview review (title); std::vector ::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 #includeusing 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(totalScore)/numRatings; }
IMO的另一个修复方法是在向量中存储Mreview
指针(理想情况为shared_ptr),并在添加第一个分数而不是更早时添加new
它们.