### Could someone please clarify this atof error my professor keeps telling me about?

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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159`` ``````#include using std::ios; using std::cout; using std::endl; using std::cin; #include using std::setprecision; int main () { cout.setf(ios::fixed|ios::showpoint); cout << setprecision(2); // Variables int fifty,twenty,ten,five,dollar,half,quarter,dime,nickel,penny; double purchase,tendered,change; // Ask the amount of purchase and tendered cout << "Please enter amount of purchase and tendered (space separated) "; char buf[100]; cin >> buf; purchase = atof(buf); char buff[100]; cin >> buff; tendered = atof(buff); // Calculate change change = tendered - purchase; cout << "Your change is \$" << change << endl << endl; // Calculate amount for each currency for (fifty = 0; change >= 50.00; fifty++) { change -= 50.00; } for (twenty = 0; change >= 20.00; twenty++) { change -= 20.00; } for (ten = 0; change >= 10.00; ten++) { change -= 10.00; } for (five = 0; change >= 5.00; five++) { change -= 5.00; } for (dollar = 0; change >= 1.00; dollar++) { change -= 1.00; } for (half = 0; change >= 0.50; half++) { change -= 0.50; } for (quarter = 0; change >= 0.25; quarter++) { change -= 0.25; } for (dime = 0; change >= 0.10; dime++) { change -= 0.10; } for (nickel = 0; change >= 0.05; nickel++) { change -= 0.05; } for (penny= 0; change > 0.00; penny++) { change -= 0.01; } // Determine how many bills are of each currency is being used // If 0 skip over and do not cout anything // If greater than 2 cout with "s", if it equals one cout without the plural "s" if (fifty >= 2) cout << fifty << " \$50 bills" << endl; else if (fifty == 1) { cout << fifty << " \$50 bill" << endl; } if (twenty >= 2) cout << twenty << " \$20 bills" << endl; else if (twenty == 1) { cout << twenty << " \$20 bill" << endl; } if (ten >= 2) cout << ten << " \$10 bills" << endl; else if (ten == 1) { cout << ten << " \$10 bill" << endl; } if (five >= 2) cout << five << " \$5 bills" << endl; else if (five == 1) { cout << five << " \$5 bill" << endl; } if (dollar >= 2) cout << dollar << " \$1 bills" << endl; else if (dollar == 1) { cout << dollar << " \$1 bill" << endl; } if (half >= 2) cout << half << " 50-cent coins" << endl; else if (half == 1) { cout << half << " 50-cent coin" << endl; } if (quarter >= 2) cout << quarter << " 25-cent coins" << endl; else if (quarter == 1) { cout << quarter << " 25-cent coin" << endl; } if (dime >= 2) cout << dime << " 10-cent coins" << endl; else if (dime == 1) { cout << dime << " 10-cent coin" << endl; } if (nickel >= 2) cout << nickel << " 5-cent coins" << endl; else if (nickel == 1) { cout << nickel << " 5-cent coin" << endl; } if (penny >= 2) cout << penny << " 1-cent coins" << endl; else if (penny == 1) { cout << penny << " 1-cent coin" << endl; } }``````
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> `
I think I found this simple mistake...

Is all my professor professor referring to is that I didn't include #include <cstdlib>?
Probably not.
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:

 ``12`` ``````float Value = 0.f; cin >> Value;``````
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
Topic archived. No new replies allowed.