My code works on the web compiler for my homework submission website. In other words, I will get full credit for my assignment. However, on my own computer (Windows running cygwin) the output is different. I want to understand why the code doesn't output correctly on my machine even though it apparently works as intended on other computers. Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <map>
#include <iomanip>
using namespace std;
int main(){
ifstream inFile("movie_ratings.txt");
int N;
float score;
string Nstring;
string scoreString;
string name;
map<string, float> totalRating;
map<string, float> numRating;
map<string, float>::iterator it;
getline(inFile,Nstring);
N = atoi(Nstring.c_str());
for(int i=0; i<N; i++){
getline(inFile,name);
if(getline(inFile,scoreString)) score = atof(scoreString.c_str());
else score = 5;
if(totalRating.count(name)){
totalRating[name] += score;
numRating[name] += 1;
}
else{
totalRating[name] = score;
numRating[name] = 1;
}
}
inFile.close();
cout << setprecision(2);
for(it=totalRating.begin(); it!=totalRating.end(); it++){
cout << it->first << ": " << numRating[it->first]
<< " review(s), average of " << it->second/numRating[it->first] << "/5." << endl;
}
return 0;
}
|
movie_ratings.txt is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
7
Poseidon Adventure
4
Poseidon Adventure
5
Rush
3
Poseidon Adventure
4
Rush
4
District 9
5
Star Wars: Phantom Menace
1
|
When I compile and run the program in cygwin the output is:
1 2 3 4
|
: 1 review(s), average 5/5.
: 3 review(s), average 4.3/5.
: 2 review(s), average 3.5/5.
: 1 review(s), average 1/5.
|
But on the homework submission website my code has this output:
1 2 3 4
|
District 9: 1 review(s), average 5/5.
Poseidon Adventure: 3 review(s), average 4.3/5.
Rush: 2 review(s), average 3.5/5.
Star Wars: Phantom Menace: 1 review(s), average 1/5.
|
This is what I want. But for some reason the title won't print on my computer. it->first doesn't work. In fact while I was trying to figure out what the problem was before I submitted it and realized my code was "working" all along, I found more weird behavior on my machine. Specifically, if I try to output anything in the iterator loop before it->first it will not be displayed.
For example
1 2 3 4
|
for(it=totalRating.begin(); it!=totalRating.end(); it++){
cout << "Hello world" << it->first << ": " << numRating[it->first]
<< " review(s), average of " << it->second/numRating[it->first] << "/5." << endl;
}
|
Not only will it->first content's not output but also "Hello world" will not be displayed.
Anyone know what the problem is? It may not matter for this assignment but in the future I don't want this inconsistency between my machine and other computers to be an issue.
Can anyone test my code on their machine and see if it works for them? Since it worked on the submission website I imagine it's a problem with my computer.
Thank you!
p.s. I know that there are other things about my code that could be improved. Like I'm sure there is a better way of extracting the info from the file than getline and converting with atof/atoi. I would appreciate any other suggestions that you are willing to offer in regards to my code in general. I do know about auto& it offered in c++11 but the submission website doesn't support that compiler so that's why I didn't use auto.