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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
using std::ios;
using std::cout;
using std::endl;
using std::cin;

#include <iomanip>
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:

1
2
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.