Hi, I hope someone can help me out with this problem I'm getting. My professor keeps telling me "you use atof, but you do not define it or include something that does " but I'm really not sure what he is referring to when he says that I don't define it or include something that defines it. I looked through my book and cannot find anything that refers to this. Hope someone can help me out.
All functions/objects (except for actual built-in C++ keywords) need to be defined somewhere. For your program to use them, you either need to define them yourself or #include a header which defines them.
For example, to use cout, you need to #include <iostream> because that is the header that defines cout.
The standard atof function is defined in <cstdlib>. So if you want to use that function, you must #include <cstdlib>
Because atof only returns a number, it doesn't tell you if the user did actually input a valid number.
If it isn't valid, it returns 0.0f, but even if user did input 0.0f it returns 0.0f.
You should directly use cin to get the float value, if you can, like:
It seems to me that Disch is correct about what the professor was saying, based on what was written by the OP. Sometimes the code compiles when you fail to include the header, and sometimes it doesn't.
On the other hand, EssGeEich is absolutely correct about the limitations of the atof function. It is a very primitive function which provides no way of knowing whether an error really occurred.
If you decide to use the STL stream classes for IO, which is far more capable, then I suggest that you read through these FAQs on the subject. I have always found these questions/answers to be invaluable when trying to quickly learn the basics of the IO Stream libraries. http://www.parashift.com/c++-faq-lite/input-output.html