0

Final project for programming class due tomorrow, any help appreciated, program crashes in this module, after accepting file name. By crash I mean it outputs "This application has requested runtime to terminate it in an unusual way" and then the usual windows "CotD.exe has stopped working":

void load(vector<Fish>& stock)
{
    char c;
    do {
        cout << "Welcome to Catch of The Day, enter (f) to choose a file to load from, otherwise enter anything else to load from 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 the directory location of CotD.exe and 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 to store name of save file. 
//It is global so that load() and 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 you for using Catch of the Day|\n"
        <<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    system("Pause");
    return 0;

}

I recently wrote this program which seems very similar to me, and ran perfectly I can't see the 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 to list\n";
}
byake
  • 321
  • 4
  • 15
  • 2
    You should test your stream input works (e.g. if (std::cin >> file) ... else ...), and not test for eof like that - eof is only set after a failed operation. There are hundreds of stackoverflow Q&A about that - e.g. http://stackoverflow.com/questions/21647/reading-from-text-file-until-eof-repeats-last-line Also, learn to step with the debugger so you know which line fails and what the variable contents are beforehand. – Tony Delroy Apr 06 '14 at 23:13
  • Where is the rest of your code? What is `file`? What does `Fish` look like? – user657267 Apr 06 '14 at 23:54

1 Answers1

0

I've edited your code, this is what I got:

    void load(vector<Fish>& stock)
  {
char c;
do {
    cout << "Welcome to Catch of The Day, enter (f) to choose a file to load from, otherwise enter anything else to load from 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 the directory location of CotD.exe and 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, this is without knowing what is in the file and what the heck Fish is, so I do not know if this is what you are looking for.

EDIT:If you could include the file, or just a section of one "fish" as I assume that is what the contents of the file are, it would be alot easier to help.

Duyve
  • 100
  • 8