OPP calculatot

May 2, 2012 at 3:42am
Hi! I am having a problem in calculator.cpp, it says unterminated #ifdef. Can someone help me please. This is my code

calculator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "Calculator.h"
using namespace std;

int main(void){
   Calculator c;
   char op;
       
   do{   
         switch( op=c.ShowCalculator()){
                      case 'a': c.Add();      break;
                      case 'b': c.Subtract(); break;
                      case 'c': c.Multiply(); break;
                      case 'd': c.Divide();   break; 
                      default:  c.Setop(-1);
         } 
   }while(op!='q'&& op!='Q');
   
   return 0;
}


calculator.h
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
#ifdef Calculator_H
#define Calculator_H

class Calculator
{
private:
        double x1;
        double x2;
public:
       calculator()
       double c.add(float x1, float x2);
       double c.substract(float x1, float x2);
       double c.multiply(float x1, float x2);
       double c.divide(float x1, float x2);
};

double Calculator::getadd()
{
return x1+x2;
cout<<"\nResult: "<<x1<<"+"<<x2<<"="<<x1+x2;
}
double Calculator::getsubstract()
{
return x1-x2;
cout<<"\nResult: "<<x1<<"."<<x2<<"="<<x1-x2;
}
double Calculator::getmultiply()
{
return x1*x2;
cout<<"\nResult: "<<x1<<"*"<<x2<<"="<<x1*x2;
}
double Calculator::getdivide()
{
return x1/x2;
cout<<"\nResult: "<<x1<<"/"<<x2<<"="<<x1/x2;
}
May 2, 2012 at 4:01am
It should be #ifndef It means "If not defined". It tells the program only to define your header if it's not already defined.

Hope this helped.
May 2, 2012 at 7:49pm
the syntax for using #ifdef is :-

1
2
3
4
5
6
7
8
9
10
#ifndef ABC
         statement 1;
         statement 2;
         statement 3;
                .
                .
                .
                .
         statement n;
#endif 


The #endif tells the compiler that the 'if' conditions for 'not defined' are over.
this is the error in the calculator.h
Last edited on May 2, 2012 at 7:50pm
May 3, 2012 at 3:12am
Thank you it did correct that error, but know i have this one: expected before token
May 3, 2012 at 3:16am
1
2
3
4
5
6
#ifndef Calculator_H
#define Calculator_H

   // ... The code

#endif 


Or simply in Visual studio
1
2
3
#pragma once

// ... the code 
Last edited on May 3, 2012 at 3:17am
May 3, 2012 at 8:36am
will u please explain how does this '#pragma once' work
May 3, 2012 at 9:37am
pragma directive is Microsoft preprocessor extension and it is only one of the many progmas available in Visual sudio.

It simply works so that your header in not included into translation unit more then once to avoid multiple inclusions and name chlases, just like traditional ifndef define endif preprocessor.

If you like to learn more about other pragmas then have a lok at this site:
http://msdn.microsoft.com/en-us/library/d9x1s805%28v=vs.80%29.aspx
May 3, 2012 at 10:03am
thanx for answering
Topic archived. No new replies allowed.