0

My code compiles and everything, but it currently just crashes and doesn't return to the original function which contains the user menu for this project. This is very similar code to what I had used on my previous project to accomplish a similar task of reading a file with a variable name input. I can only use IO Stream and fstream to do all of this. Any help at all would be appreciated. Thank you!

void readCars(Cars carsArray[]) {
    int index1;
    char inputFile[100];
    cout << "Input file name:" << endl;
    cin >> inputFile;
    ifstream input(inputFile);
    if (input) {
        while (!input.eof()) {
            for (index1 = 0; index1 < 5; index1++) {
                input >> carsArray[index1].year >> carsArray[index1].make
                      >> carsArray[index1].model >> carsArray[index1].model
                      >> carsArray[index1].price >> carsArray[index1].available;
            }
        }
    } else {
        cerr << "Input file cannot be opened" << endl;
        return;
    }
    return;
}
Nopileos
  • 1,188
  • 3
  • 14
  • 1
    It needs to be in the current working directory, which isn't necessarily the same thing as the directory where the executable is. – user207421 Feb 06 '20 at 00:38
  • 3
    Typo? `carsArray[index1].model >> carsArray[index1].model` – tangy Feb 06 '20 at 00:44
  • 4
    Probably not the bug you're hunting, but here's some recommended reading: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Feb 06 '20 at 00:49
  • 6
    The programmer's secret weapon is the debugger. With the debugger you can run the program on your terms and slow things down so you can watch what happens as it happens. Run the program in the debugger that came with your development tools and step through the program line by line. Keep an eye out for the program doing something you didn't expect like taking the wrong branch or storing the wrong value in a variable. The unexpected is usually a bug. When it isn't it means your expectations are wrong and that's just as bad. – user4581301 Feb 06 '20 at 00:53
  • 2
    Does the array passed to `readCars` contain room for 5 elements? – TheUndeadFish Feb 06 '20 at 00:54
  • user4581301 figured it out, thanks man. – Noah Howren Feb 06 '20 at 00:58
  • 1
    @user4581301 That would be of exactly zero help in the case of a file not found. – user207421 Feb 06 '20 at 03:10
  • 1
    I've rolled back your edit. It is not appropriate to add **SOLVED** to the question title or edit a solution into the question. If you'd like to share the solution, do so by writing an answer in the space provided below for that purpose - see [Can I answer my own question?](http://stackoverflow.com/help/self-answer) for more information. If you don't want to do so, you can either wait for someone else to answer or you can delete the question entirely using the link below the tags. – Ken White Feb 06 '20 at 03:35
  • 2
    As an addition to @user4581301's comment, it's helpful to understand that *it compiles* only means that the syntax is correct. It does not mean that the logic or code is correct, only that the syntax passes the compiler's rules. The debugger helps you figure out whether the logic works or not. – Ken White Feb 06 '20 at 03:43

0 Answers0