I am trying to make a simple calculator program, it's not finished yet, but when i try to "compile and run" it in Dev C++, it says there is 39 errors! How can there be thirty nine??
So yeah please help me out by reading through this code and telling me what you think could be the problems. The code looks good to me, but I'm only pretty new to C++...
int main() // main function, calls other functions on request
{
using namespace std;
cout << "Simple Calculator. Written by @@@@@ @@@@@@@@@\n\n";
cout << "\tType 0 for normal functions";
cout << "\n\yType 1 for special functions\n";
double decision;
cin >> decision; // creates the variable for users input
if (decision == 0)
{
normalfun(double decision2)
else specialfun(float)
}
system("pause");
return 0;
}
void normalfun(double decision2)
{
using namespace std;
cout << "\n\tType 1 for addition,";
cout << "\n\tType 2 for subtraction,";
cout << "\n\tType 3 for multiplication,";
cout << "\n\tType 4 for division\n\n";
cin >> decision2;
{
if (decision2 == 1);
add();
if (decision2 == 2);
subtract();
if (decision2 == 3);
multiply();
if (decision2 == 4);
divide();
}
}
void specialfun(float) // special function
{
using namespace std;
cout << "This is special function ";
}
In before Paul : iostream.h is not a standard header. You have to include <iostream>.
Also, please post your code between [CODE]code[/CODE] tags.
What errors are you getting? Concentrate only on the very first one. 99% percent of the time, the first errors screws with the compiler, who doesn't understand the rest of the code, and generates more errors.
If you can fix the errors 1 by 1, starting with the first, you should be fine. I bet there isn't more than a handful.
Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
So yeah please help me out by reading through this code and telling me what you think could be the problems. The code looks good to me, but I'm only pretty new to C++...
That's not how to program; Produce a heap of non-working code and then let someone else sort it out.
Instead use stepwise refinement. Start with a small working program. Then add stuff in small increments and make sure each addition works before you continue. You'll have a working program from start to finish.
Your first action should be to make a correct intendation. Then start with main function to make it compile
Code:
int main() // main function, calls other functions on request
{
using namespace std;
cout << "Simple Calculator. Written by @@@@@ @@@@@@@@@\n\n";
cout << "\tType 0 for normal functions";
cout << "\n\yType 1 for special functions\n";
double decision;
cin >> decision; // creates the variable for users input
if (decision == 0)
{
normalfun(double decision2)
else specialfun(float)
}
system("pause");
return 0;
}
The trouble starts with 'if (decision == 0)':
'decision' is of type double. Therefore it should be compared with 0. (though the compiler would correct that for you) and in case of 0 it should work. Generally, a double variable should not be compared with a double literal cause a double might have different internal representations for the same decimal, e. g. 3.0 internally may be 2.99999999999999999999_ or 3.0000000000000000000000_. If you compare the first with 3.0 the comparision will not return equality.
Your solution is easier. You simply turn the 'double decision' to 'int decision' and are done.
After the if statement you open a curly bracket which opens a new if block. But that block isn't closed before the else. Generally, it is
Code:
if (<condition>)
{
<statements>
}
else
{
<statements>
}
So, you need either two blocks with curly brackets or you omit the curly brackets at all what is possible if it is only one statement. I would recommend to always use brackets especially if you were not able to make a correct intendation (as in the code you posted).
Next is that the calls of normalfun and specialfun have wrong syntax. In a call the argument types are not repeated but you pass only the arguments.
Code:
normalfun(decision);
And of course you need to pass variables defined in the calling function and not those which were only prototypes in the called function. So, you have to pass 'decision' and not 'decision2'.
In your case it is unclear why you were passing an argument at all. If you look at normalfun implemention you see that t he argument passed wasn't used but overwritten by a new user input. Same applies for specialfun whcih for any reason has a float argument and actually should have no argument.
Last edited by Brad Jones; August 13th, 2012 at 10:23 AM.
Bookmarks