Dec 7, 2014 at 6:21am UTC
when i run this program it asks me for the first file , when I'm done for input the filename, i get an abort error, I am not sure what I am doing wrong, can someone help me check that? so many thx!
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
using namespace std;
struct game{
string name;
int cost;
double odds;
};
bool mycomp(game* i, game* j){
if(i->odds == 0){
return (i->odds > j->odds);
}
else if(j->odds == 0){
return (i->odds > j->odds);
}
return (i->odds < j->odds);
}
void print_string(string s)
{
for(int i=0; i<s.length()-1; i++){
cout << s[i];
}
}
int main()
{
int lowest_amt;
cout << "Enter the lowest amount of profit:" << endl;
cin >> lowest_amt;
string filename;
cout << "Enter the file name:" << endl;
cin >> filename;
string name,blank;
int cost, N, value, total, claimed;
double odds;
ifstream file;
file.open(filename.c_str());
vector<game*> v;
while(!file.eof())
{
getline(file, name);
name.erase(name.length()-1);
game* g = new game();
g->name = name;
double num = 0, den = 0;
file >> cost >> N >> odds;
g->cost = cost;
for(int i=0; i<N; i++)
{
file >> value >> total >> claimed;
num += claimed;
if(value > lowest_amt + cost){
den += claimed;
}
if(den == 0){
g->odds = 0;
}
else{
g->odds = (num/den)*odds;
}
}
v.push_back(g);
getline(file, blank);
getline(file, blank);
}
sort(v.begin(), v.end(), mycomp);
cout << "Game \t\t\t\t\t" << "Cost \t" << "Odds" << endl;
cout << "--------------------------------------------------------" << endl;
for(vector<game*>::iterator i=v.begin(); i!= v.end(); i++)
{
if((*i)->odds == 0){
cout << setprecision(2) << (*i)->name << "\t\t\t$" << (*i)->cost << " Not Possible " << endl;
}
else cout << fixed << setprecision(2) << (*i)->name << "\t\t\t$" << (*i)->cost << " 1 in " << (*i)->odds << endl;
}
return 0;
}