OOP class calculator

Hi! I am working on my header file and I have this problem in line 31 expected `,' or `;' before '<<' token, can someone help please.

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
#ifndef Calculator_H
#define Calculator_H

class Calculator
{
private:
        float a;
        float b;
public:
       Calculator();
       float add(float, float);
       float Subtract(float, float);
       float divide(float, float);
       float multiply(float, float);
};

Calculator::Calculator()
{
a = 0;
b = 0;
}
float Calculator::add(float a, float b)
{ return a + b;}
float Calculator::Subtract(float a, float b)
{ return a - b;}
float Calculator::divide(float a, float b)
{ return a / b;}
float Calculator::multiply(float a, float b)
{ return a * b;}

cout<<"You can choose from add, subtract, divide or multiply."<<endl;
cout<<"Do you have 2 numbers to perform a function on? (y/n)";
cin>>op;

cout<<"What function would you like to try? ";
cin>>op;
       
if(op == "add")
{
cout<<"Type in 2 numbers to add: ";
cin>>a>>b;
cout<<x<<" + "<<y<<" = "<<c.add(a,b)<<endl;
}
else
{
if(op == "subtract")
cout<<"Type in 2 numbers to subtract: ";
cin>>a>>b;
cout<<x<<" - "<<y<<" = "<<c.subtract(a,b)<<endl;
}
else
{
if(op == "divide")
cout<<"Type in 2 numbers to divide: ";
cin>>a>>b;
cout<<x<<" / "<<y<<" = "<<c.divide(a,b)<<endl;
}
else
{
if(op == "multiply")
cout<<"Type in 2 numbers to multiply: ";
cin>>a>>b;
cout<<x<<" * "<<y<<" = "<<c.divide(a,b)<<endl;
}
}
}
cout<<"Try again? ";
cin>>op;
}
}
Half of your code is bad :S

It is not a header file. Header files looks like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef Calculator_H
#define Calculator_H
class Calculator
{
	private:
        float a;
        float b;
	public:
       Calculator();
       float add(float, float);
       float Subtract(float, float);
       float divide(float, float);
       float multiply(float, float);
};
#endif Calculator_H 


And this is cpp file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "calc.h"
Calculator::Calculator()
{
	a = 0;
	b = 0;
}

float Calculator::add(float a, float b)
{ return a + b;}

float Calculator::Subtract(float a, float b)
{ return a - b;}

float Calculator::divide(float a, float b)
{ return a / b;}

float Calculator::multiply(float a, float b)
{ return a * b;}


And all else goes to main file, I think.

And this is how should look constructor in your case
1
2
3
4
Calculator::Calculator() :
a(0),
b(0)
{}
Last edited on
Everything below line 31 is in a class statement. Code cannot be processed here.

You need to put this code in some sort of a function so that the compiler will know when to process it.
everything below line 31 is, well, nothing! You must put it in a function! mopve it to the main() functionin cour .cpp file!
Topic archived. No new replies allowed.