user637965
user637965

Reputation:

Assertion `in' failed - c++

The following is part of client program for a fraction class. I wrote the class and now am testing it with the given client program. When I try to run it, I get this error:

Assertion 'in' failed.

Code:

bool eof(ifstream& in);

cout << "\n----- Now reading Fractions from file\n";
ifstream in("fraction.data");
assert(in);
while (!eof(in)) {
    Fraction f;
    if (in.peek() == '#') {
        in.ignore(128, '\n');                    //skip this line, it's a comment
    } else {
        in >> f;
        cout << "Read fraction = " << f << endl;
    }

As a relative beginner to C++, I don't really understand what this part of the code is supposed to be doing:

ifstream in("fraction.data");
assert(in);

And when I try to debug and I get to that point, it says:

No source available for "__kernel_vsyscall() at 0x12e416"

So yeah, in conclusion I'm pretty clueless about why this happening :P

EDIT: Here are the include statements

#include <iostream>
#include "fraction.h"
#include <fstream>
#include <cassert>
using namespace std;

Upvotes: 0

Views: 3742

Answers (3)

user637965
user637965

Reputation:

So I finally got it. I just need to move my "fraction.data" file to main project directory. Before, I had it in the source folder within the directory.

Upvotes: 0

Tom Kerr
Tom Kerr

Reputation: 10720

assert is a runtime check that verifies its argument is true. In this case, your file is not valid.

Upvotes: 0

Harper Shelby
Harper Shelby

Reputation: 16583

The assert() fails if the expression evaluates to false.

assert(in)

fails because in (the input file) evaluates to false. Your code is unable to open a file called "fraction.data". If in were a valid input file stream, assert(in) would pass, and you'd go on about your business.

Short answer -> "File not found" or "Can't create a file here".

Upvotes: 2

Related Questions