loading from a file syntax question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • snafu2480
    New Member
    • Nov 2007
    • 2

    loading from a file syntax question

    I'm trying to load from a file i was just wondering if anyone can see if there are any syntax errors
    P.S. my save part of the code works.
    thanks in advance for help
    [code=c++]
    ofstream myfile;
    ifstream load_file("Game save.txt");
    string line;

    class BadConversion : public std::runtime_er ror {
    public:
    BadConversion(c onst std::string& line)
    : std::runtime_er ror(line)
    { }
    };

    inline double convertToDouble (const std::string& line)
    {
    std::istringstr eam i(line);
    double x;
    if (!(i >> x))
    throw BadConversion(" convertToDouble (\"" + line + "\")");
    return x;
    }

    if(load_file.is _open())
    {
    while(! load_file.eof() )
    {
    getline (load_file,line );
    money = convertToDouble (line);
    //cout<<"Here is your previous game winnings: ";
    //cout << money << "\n";
    //Sleep(3000);
    }
    load_file.close ();
    }
    [/code]
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    I'm trying to load from a file i was just wondering if anyone can see if there are any syntax errors
    Generally, this is what a compiler does for you. We can check if there are overall incorrect code that wouldn’t be caught by a compiler.

    Slightly odd, but it seems like you have statements outside of a function scope. Look at that part: if (load_file.is_o pen… ) . Is it within a function?

    You’ve definitely misused EOF. Look at the while loop where you test EOF, and then use getline. Let’s see if you get the hint, and if not, I’ll explain further. eof() doesn’t check if the next read would hit an EOF. It checks if the EOF flag is set, which would only be set by a previous I/O operation. Now, why would a I/O operation set the EOF flag?

    Comment

    Working...