Undefinied function atof()

Hello, I'm new here, and also pretty new at C++. I've now made a kind of calculator how you can calculate your tax of your income in the Netherlands, where I come from. I'm sorry if I coded a bit unclear, for so far I've learned everything on my own and I'm a bit proud of myself. I'm still 14 years old, but please don't underestimate me, I hate that. I can do more than this, this is something i just made in about 10 minutes or something.

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
#include <iostream>
#include <string>

int main() { 
using namespace std;
string earn = "0";
float tax; 

/*
5821.209
5630.9825
9070.74
*/

cout << "This program calculates the tax of the money you earn in holland. " << endl;

cout << "Enter your income please: ";
cin >> earn;

if (!(isdigit(atof(earn)))) {
return 1;
}

if (atof(earn) < 17046) { 
tax = atof(earn) / 100 * 34.15;

}

if (atof(earn) > 17046 && atof(earn) < 30631) {
tax = (atof(earn) - 13585) / 100 * 41.25 + 5821.209;

}

if (atof(earn) < 52228 && atof(earn) > 30631) {
tax = (atof(earn) - 21597) / 100 * 42 + 5821.209 + 5630.9825;

}

if (atof(earn) > 52228) {
tax = (atof(earn) - 52228) / 100 * 52 + 5821.209 + 5630.9825 + 9070.74;

}

cout << endl <<  "Your tax withoudt deduction is " << tax << endl;
return 0;
}


I wanted to build the deduction into it, but I now got errors so first I want to get the errors out of it. These are the errors:

C:\CPPs\tax>bcc32 tax
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
tax.cpp:
Error E2285 tax.cpp 21: Could not find a match for 'atof(string)' in function ma
in()
Error E2285 tax.cpp 26: Could not find a match for 'atof(string)' in function ma
in()
Error E2285 tax.cpp 27: Could not find a match for 'atof(string)' in function ma
in()
Error E2285 tax.cpp 32: Could not find a match for 'atof(string)' in function ma
in()
Error E2285 tax.cpp 32: Could not find a match for 'atof(string)' in function ma
in()
Error E2285 tax.cpp 33: Could not find a match for 'atof(string)' in function ma
in()
Error E2285 tax.cpp 37: Could not find a match for 'atof(string)' in function ma
in()
Error E2285 tax.cpp 37: Could not find a match for 'atof(string)' in function ma
in()
Error E2285 tax.cpp 38: Could not find a match for 'atof(string)' in function ma
in()
Error E2285 tax.cpp 42: Could not find a match for 'atof(string)' in function ma
in()
Error E2285 tax.cpp 43: Could not find a match for 'atof(string)' in function ma
in()
*** 11 errors in Compile ***


When I remove the "Using namespace std;" I don't get any errors of atof, but i do get errors of the string earn and of the cout and the cin.

I'm using the borland compiler, as you see. Any tips for me does somebody has got MSN or whatever and want to help me, just response, I would be glad to hear anything.

Thank you, Jyy
Last edited on
Try throwing a #include <cstdlib> in your program.
That won't work either... The strange point is: if I delete the using namespace std;, I don't get any error of the atof, but then I get errors of all my cin's and couts... Thanks anyway. another suggestion?

Edit:

I think the error is because I made a string of "earn". When I make a char of it, I don't get any errors. I works! Now I have this code:

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
#include <iostream>
#include <string>

int main() { 
using namespace std;
char earn[10];
float tax; 

/*
5821.209
5630.9825
9070.74

http://www.cplusplus.com/forum/beginner/7147/

*/

cout << "This program calculates the tax of the money you earn in holland. " << endl;

cout << "Enter your income please: ";
cin.getline(earn,10);

/*
if (!(isdigit(atof(earn)))) {
return 1;
}
*/

if (atof(earn) < 17046) { 
tax = atof(earn) / 100 * 34.15;

}

if (atof(earn) > 17046 && atof(earn) < 30631) {
tax = (atof(earn) - 13585) / 100 * 41.25 + 5821.209;

}

if (atof(earn) < 52228 && atof(earn) > 30631) {
tax = (atof(earn) - 21597) / 100 * 42 + 5821.209 + 5630.9825;

}

if (atof(earn) > 52228) {
tax = (atof(earn) - 52228) / 100 * 52 + 5821.209 + 5630.9825 + 9070.74;

}

cout << endl <<  "Your tax withoudt deduction is " << tax << endl;
return 0;
}


i put the isdigit code between /* */ because it doesn't work, and i prefer the string instead of the char[10].

Thanks, Jyy
Last edited on
Can someone still help me with making the code better? And am I able to put it into a graphic design and how? I never done that before...
Thank you Zaita, I already know a lot more since i'm here :)

But still I get errors when using a string in the atof function, is it able to use a string instead of a bunch of chars? and is somebody able to help me with the lay-out(now it's just a stupid black screen) or the isdigit() function?

Jyy
Look at that link I posted for you. It shows you how to convert from a string to an integer after reading from cin using getline. The conversion is identical when converting to a float or double. You won't need to worry about using the old style atof() and isdigit() etc functions.

I'd also use a double instead of a float where possible for increased accuracy.
I'm sorry, but I think I don't really understand it...

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
#include <iostream>
#include <string>
#include <sstream>


int main() { 

using namespace std;

string earn = "0";
float tax; 
 
/*
5821.209
5630.9825
9070.74
http://www.cplusplus.com/forum/beginner/7147/
*/

cout << "This program calculates the tax of the money you earn in holland. " << endl;





 while (true) {
cout << "Enter your income please: ";
getline(cin, earn);

   stringstream myStream(earn);
   if (myStream >> earn) {
     break;
     }
   cout << "Invalid number, please try again" << endl;
 }




if (earn < 17046) { 
tax = earn / 100 * 34.15;
}

else if (earn > 17046 && earn < 30631) {
tax = (earn - 13585) / 100 * 41.25 + 5821.209;
}

else if (earn < 52228 && earn > 30631) {
tax = (earn - 21597) / 100 * 42 + 5821.209 + 5630.9825;
}

else if (earn > 52228) {
tax = (earn - 52228) / 100 * 52 + 5821.209 + 5630.9825 + 9070.74;
}
else {
cout << "Unexpected error!";
}

cout << endl <<  "Your tax withoudt deduction is " << tax << endl;
return 0;
}




C:\CPPs\tax>bcc32 tax
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
tax.cpp:
Error E2094 tax.cpp 40: 'operator<' not implemented in type 'string' for arguments of type 'int' in function m
ain()
Error E2094 tax.cpp 41: 'operator/' not implemented in type 'string' for arguments of type 'int' in function m
ain()
Error E2094 tax.cpp 44: 'operator>' not implemented in type 'string' for arguments of type 'int' in function m
ain()
Error E2094 tax.cpp 44: 'operator<' not implemented in type 'string' for arguments of type 'int' in function m
ain()
Error E2094 tax.cpp 45: 'operator-' not implemented in type 'string' for arguments of type 'int' in function m
ain()
Error E2094 tax.cpp 48: 'operator<' not implemented in type 'string' for arguments of type 'int' in function m
ain()
Error E2094 tax.cpp 48: 'operator>' not implemented in type 'string' for arguments of type 'int' in function m
ain()
Error E2094 tax.cpp 49: 'operator-' not implemented in type 'string' for arguments of type 'int' in function m
ain()
Error E2094 tax.cpp 52: 'operator>' not implemented in type 'string' for arguments of type 'int' in function m
ain()
Error E2094 tax.cpp 53: 'operator-' not implemented in type 'string' for arguments of type 'int' in function m
ain()
*** 10 errors in Compile ***
Change the type of earn to float.
Careful, he's using earn in two different type contexts.

You need a string for string input.
You need a float (or double) for numeric input.

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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
  {
  double earn;  // As Helios indicated, this should be a floating point value

  cout << "This program calculates the tax of the money you earn in Holland." << endl;

  // Force the user to input a valid floating-point number and store it in earn
  while (true)
    {
    cout << "Enter your income please: " << flush;

    string users_input;  // All input is a string
    getline(cin, users_input);  // Very good! Get an entire line of input from the user every time!

    // If that line of input can be converted to a floating-point type, we're done
    if (istringstream(users_input) >> earn) break;

    // Otherwise, complain and repeat
    cout << "Invalid number, please try again\n";
    }

  cout << "Good job! You entered the numeric value " << earn << " .\n";
  return 0;
  }


Hope this helps.
okay, it works! Really thank you all!
Topic archived. No new replies allowed.