### A simple calculator program

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

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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990`` ``````#include 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.