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.
#include <iostream>
#include <string>
int main() {
usingnamespace 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.
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:
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?
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.
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 ***
#include <iostream>
#include <sstream>
#include <string>
usingnamespace 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 earnwhile (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;
}