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 ()

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);
}
make everything dark blue and press the <> button you then get this

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

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??
9
10
11
12
13
14
15
16
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:

1
2
3
4
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.
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
#include <iostream>

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.