I am working in my text book Problem Solving, Abstraction, and design using C++, 5th edition, Friedman. I am trying to learn about file i/o. My code will not compile and I am getting frustrated. Any input as to why it will not compile would be greatly appriciated. Here is my code.
D:\prog\cc\foo\a>g++ -Wall a.cpp
a.cpp: In function 'float processEmp(std::ifstream&, std::ofstream&)':
a.cpp:76: error: 'salary' was not declared in this scope
a.cpp:80: error: expected `)' before ';' token
You need to declare salary before you can use it.
Function argument lists must be terminated by a ')' character.
Make sure you compile with all warnings turned on.
Hope this helps.
I knew it was something like that. Thanks. Now on to the next big thing. I am stepping through, it opens my text file ok, reads string value 'name' and reads the hours and rate. It even created the output file salary.txt. The problem now it it just sits in a never ending loop and nothing is getting written in the out file. I watch my veriables and I get correct values. Here is the whole of my text file. named empFile.txt
Jim Baxter
35.5 7.25
Adrian Cybriwsky
40 6.50
Ayisha Mertens
20.0 8.00
, if you would like to venture on...... You guys are awsome!
You are only checking for the end of file, you aren't checking for errors. So the file enters an error state because the second time it hits getline(), its getting the carriage return from the line with your number data on it because you retrieved them with the >> operator. So then you try and put the second person's first and last name into hours and rate (respectively) which puts the stream into an error state, and it just sits there because you only check for eof. Now you have an infinite loop. Put your original line (the one from outside the loop)
getline(eds, name);
inside the while condition so it reads
while ( getline(eds, name) ) // you can now get rid of your original getline() statement
then your getline() inside the loop can become
eds.ignore() //to throw away the thing sending the stream into a failed state
Now this will continue the loop so long as it is able to accomplish a getline() from the stream, whether or not it stops from eof, fail, or bad. And the carriage return is ignored, so it doesn't cause an issue.
This did the trick.
This book has some issues. I typed exactly as it stated, it did not include <stream>, but does include <fstream>.
My text file is set up exactly as directed, yet my program ran infinitely.
Just some of the challenges of taking online classes.