crash - C++ Program crashes while reading from file using fstream fin -


final project programming class due tomorrow, appreciated, program crashes in module, after accepting file name. crash mean outputs "this application has requested runtime terminate in unusual way" , usual windows "cotd.exe has stopped working":

void load(vector<fish>& stock) {     char c;     {         cout << "welcome catch of day, enter (f) choose file load from, otherwise enter else load default file.\n";         cin >> c;         if (c == 'f' || c == 'f')         {             cout << "enter file name\n";             cin >> file;         }         ifstream fin(file.c_str());         if (fin.fail())         {             cout << "could not open " << file << " check directory location of cotd.exe , try again\n";         }         else         {             while (!fin.eof())             {                 fish f;                 string blank;                 fin >> f.amt;                 fin >> f.prc;                 fin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');                 getline(fin, blank);                 stock.push_back(f);             }             fin.close();             break;         }     } while (true); } 

edit other relevant code:

#include <iostream> #include <fstream> #include <vector> #include <iomanip> using namespace std; // string file = "default.txt"; //global variable used store name of save file.  //it global load() , save() can both access it. struct fish {     string type;     double amt;     double prc;     double val; }; void addtype(vector<fish>&); void editstock(vector<fish>&);  void sortby(vector<fish>&); void sortasc(vector<fish>&,char); void sortdesc(vector<fish>&,char); void display(vector<fish>&); int search(vector<fish>&); void save(vector<fish>&); void load(vector<fish>&); string gettype(); int disptype(string,vector<fish>&); int find(string,vector<fish>&); double getamt(); void deltype(string,vector<fish>&); void menu(vector<fish>&); double getprc();  int main(){     std::vector<fish> stock;     load(stock);     menu(stock);     save(stock);     cout<<endl<<endl         <<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"         <<"|thank using catch of day|\n"         <<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";     system("pause");     return 0;  } 

i wrote program seems similar me, , ran can't see difference:

void load(vector<string>& names) {     string file, name, bad;     while (true)     {         cout << "input file name\n";         getline(cin, file);         ifstream fin(file.c_str());         if (fin.fail())         {             cout << "could not open " << file << ", try again.\n";         }         else break;     }     ifstream fin(file.c_str());     while (!fin.eof())     {         fin >> bad;         fin >> name;         cout << "\"" << name << "\"" << endl;      }     system("pause");     fin.close();      ifstream fin(file.c_str());     while (!fin.eof())     {         getline(fin, name);         names.push_back(name);     }     system("pause");     fin.close();     cout << "names added list\n"; } 

i've edited code, got:

    void load(vector<fish>& stock)   { char c; {     cout << "welcome catch of day, enter (f) choose file load from, otherwise enter else load default file.\n";     cin >> c;     if (c == 'f' || c == 'f')     {         cout << "enter file name\n";         cin >> file;     }     ifstream fin(file.c_str());     if (fin.fail())     {         cout << "could not open " << file << " check directory location of cotd.exe , try again\n";     }     else     {         fish f;         string blank;         if (fin>>f.amt)         {            if (fin>>f.prc)            {             getline(fin,blank);             stock.pushback(f);            }         }          fin.close();         break;     } } while (true);     } 

of course, without knowing in file , heck fish is, not know if looking for.

edit:if include file, or section of 1 "fish" assume contents of file are, alot easier help.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -