A simple calculator program

Following is my calculator program but i have two errors reported by my IDE

1)Error 1: error C3861: 'confir_mation': identifier not found c:\users\abhinav\documents\visual studio 2010\abhinav_examples

2)Error 2: error C3861: 'main': identifier not found

I'm new to c++ programming, any help is much appreciated!
//program:

#include "stdafx.h"
#include <iostream>
using namespace std;
double a,b,c;
int y;

int operation()
{

cout << "Please choose a mathematical operand from following available: \n '1' to divide \n '2' to multiply \n '3' to add \n '4' to subtract\n";
cin >> c;
if ( c==1 )
{
cout << "The result is: " << (a/b);
}
else if ( c==2 )
{
cout << "The result is: " << (a*b);
}
else if ( c==3 )
{
cout << "The result is: " << (a+b);
}
else if ( c==4 )
{
cout << "The result is: " << (a-b);
}
else
{
cout << "Don't be stupid!!! Try Again...";
cout << "\n\n\n" ;
cout << operation();
}

cout << confir_mation();
}

int confir_mation()
{
cout << "Want to calculate more?? \n"
<< "Enter '0' for a 'Yes' and '1' to 'Termiate':\n";
cin >> y;
cout << "\n\n\n";
if( y==0 )
{
cout << "Welcome again...";
return main();
}
else if( y==1)
{
cout << "Leaving so soon!\n\n";
return 0;
}
else
{
cout << "Don't be a jerk! Enter a valid input! \n\n\n";
cout << confir_mation();
}
return 0;
}
int main()
{
cout << "Enter first number to be evaluated: \n";
cin >> a;
cout << "You entered: " << a;
cout << "\n\n\n";

cout << "\nEnter the second one: \n";
cin >> b;
cout << "You entered: " << b;
cout << "\n\n\n";
cout << operation();
return 0;
}
Why do you cout the function? To call a function, just put the function on there.
You cannot call main().
The reason for "identifier not found" messages here is that the name is used before it has been declared. The general solution is to declare function prototypes at the top of the code. Then the functions themselves can be placed in any order. I prefer to put function main() first.

The program needs to use some sort of loops to control the processing.

Example:
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
#include <iostream>
using namespace std;

bool confir_mation();
double operation(double a, double b);

int main()
{
    double a, b;

    do
    {
        cout << "Enter first number to be evaluated: \n";
        cin  >> a;

        cout << "\nEnter the second one: \n";
        cin  >> b;

        cout << "The result is: " << operation(a,b) << endl;

    } while (confir_mation());

    return 0;
}

double operation(double a, double b)
{
    cout << "Please choose a mathematical operand from following available: \n"
         << " '1' to divide \n '2' to multiply \n '3' to add \n '4' to subtract\n";
    char ch;

    do {
        cin >> ch;

        if ( ch=='1' )
        {
            if (b == 0)
            {
                cout << "Cannot divide by zero\n";
                return 0;
            }
            return a/b;
        }
        else if ( ch=='2' )
        {
            return a*b;
        }
        else if ( ch=='3' )
        {
            return a+b;
        }
        else if ( ch=='4' )
        {
            return a-b;
        }

        cout << "Try Again...";
        cout << "\n\n\n" ;

    } while (true);

    return 0;
}

bool confir_mation()
{
    cout << "Want to calculate more?? \n"
         << "Enter '0' for a 'Yes' and '1' to 'Terminate':\n";
    char ch;
    do {
        cin >> ch;
        cout << "\n\n\n";
        if( ch=='0' )
        {
            cout << "Welcome again...";
            return true;
        }
        else if (ch=='1')
        {
            cout << "Leaving so soon!\n\n";
            return false;
        }

        // Fall through to here
        cout << "Enter a valid input! \n\n\n";

    }  while (true);

    return true;
}
Last edited on
Topic archived. No new replies allowed.