### calculator problem

Pass in parameters to each function for the values to use and the functions will return the result. Use a function to read in the numbers involved. These numbers will be doubles. Also write a function that reads in the operator and returns a boolean – true if the operator is valid, false if not valid. This function will have two parameters. First is a string of characters containing the valid operators. The second is a reference parameter where the operator will be placed if the operator entered is valid.
Can anybody help me to find error, its keep saying me missing declaration ";".
#include <iostream>

using namespace std;

#include "math.h"

void main ()

{
char validops [] = {'+','-','*','/', 'c','C','x','X'};
double D1;
double D2;
double D3;
char Operator(0);
bool NeedAnotherOperator (0);

cout<< " Enter a number: ";
do
{
do
{
do
{
cout << "Enter an operator: ";
} while ( (validops, Operator) ==false);
switch (Operator)
{
case'+':
case'-':
case'*':
case'/':
NeedAnotherOperator= false;
break;
case 'C':
case 'c':

D3=0;
cout << "the calculator is cleared now. Please enter a number: ";
NeedAnotherOperator = true;
break;
case 'X':
case 'x':
exit(0);
break;
default:
cout<< " Invalid Operator. Please enter a valid operator: " << endl;
NeedAnotherOperator=true;
}
}
while (NeedAnotherOperator);
cout << "Enter another number:";

switch (Operator)
{
case '+':
D3 = D1 + D2;
cout << " Your Result is : " << D3 << endl;
break;
case '-':
D3 = D1 - D2;
cout << " Your Result is : " << D3 << endl;
break;
case '*':
D3 = D1 * D2;
cout << " Your Result is : " << D3 << endl;
break;
case '/':
D3= D1 / D2;
{ if (D2==0)
{
cout << " Can't divide by 0 " << endl;
}
else
{

cout << " Your Result is : "<< D3 << endl;
}
}
break;
default:
break;

}
D1=D3;
}
while (!NeedAnotherOperator);
}
make everything dark blue and press the <> button you then get this

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````#include using namespace std; #include "math.h" void main () double ReadDouble() { char validops [] = {'+','-','*','/', 'c','C','x','X'}; double D1; double D2; double D3; char Operator(0); bool NeedAnotherOperator (0); cout<< " Enter a number: "; D1 = ReadDouble (); do { do { do { cout << "Enter an operator: "; } while ( (validops, Operator) ==false); switch (Operator) { case'+': case'-': case'*': case'/': NeedAnotherOperator= false; break; case 'C': case 'c': D3=0; cout << "the calculator is cleared now. Please enter a number: "; D1 = ReadDouble (); NeedAnotherOperator = true; break; case 'X': case 'x': exit(0); break; default: cout<< " Invalid Operator. Please enter a valid operator: " << endl; NeedAnotherOperator=true; } } while (NeedAnotherOperator); cout << "Enter another number:"; D2 = ReadDouble (); switch (Operator) { case '+': D3 = D1 + D2; cout << " Your Result is : " << D3 << endl; break; case '-': D3 = D1 - D2; cout << " Your Result is : " << D3 << endl; break; case '*': D3 = D1 * D2; cout << " Your Result is : " << D3 << endl; break; case '/': D3= D1 / D2; { if (D2==0) { cout << " Can't divide by 0 " << endl; } else { cout << " Your Result is : "<< D3 << endl; } } break; default: break; } D1=D3; } while (!NeedAnotherOperator); }``````

Its now easier to look at see, and more tempting to read the code in order to help you, its a handy function they put on all coding sites
I think you forgot to pop a ; somewhere, what line does it say missing declaration is on??
 ``910111213141516`` ``````void main () double ReadDouble() { //... }``````

Um.. what?
Last edited on
its says missing ";" after double but if i put ";" after readdouble, it shows more error.
You are not understanding something so basic that it is hard to explain. Do you understand what a function definition is? It consists of a return type of a function, the name of the function, and a set of parentheses that may contain parameters. An example:

 ``1234`` ``````int main() { return 0; }``````

On line 9, you have `void main()` but do not have the {} brackets with code following it.
Even if I put brackets its showing error, I am totally lost..
OK, it's obvious from what you posted above that the huge block of code you put after `double ReadDouble()` is supposed to be the main function.

So, switch lines 9 and ll. Then add a semicolon after `double ReadDouble()`. I'm assuming that you have the function definition for that somewhere else.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293`` ``````#include using namespace std; #include "math.h" double ReadDouble(); void main () { char validops [] = {'+','-','*','/', 'c','C','x','X'}; double D1; double D2; double D3; char Operator(0); bool NeedAnotherOperator (0); cout<< " Enter a number: "; D1 = ReadDouble (); do { do { do { cout << "Enter an operator: "; } while ( (validops, Operator) ==false); switch (Operator) { case'+': case'-': case'*': case'/': NeedAnotherOperator= false; break; case 'C': case 'c': D3=0; cout << "the calculator is cleared now. Please enter a number: "; D1 = ReadDouble (); NeedAnotherOperator = true; break; case 'X': case 'x': exit(0); break; default: cout<< " Invalid Operator. Please enter a valid operator: " << endl; NeedAnotherOperator=true; } } while (NeedAnotherOperator); cout << "Enter another number:"; D2 = ReadDouble (); switch (Operator) { case '+': D3 = D1 + D2; cout << " Your Result is : " << D3 << endl; break; case '-': D3 = D1 - D2; cout << " Your Result is : " << D3 << endl; break; case '*': D3 = D1 * D2; cout << " Your Result is : " << D3 << endl; break; case '/': D3= D1 / D2; { if (D2==0) { cout << " Can't divide by 0 " << endl; } else { cout << " Your Result is : "<< D3 << endl; } } break; default: break; } D1=D3; } while (!NeedAnotherOperator); }``````

didn't work:-(
Topic archived. No new replies allowed.