Simple calculator 2 variations

Hi everyone: this is the prompt to create the program.

Description
Write a simple calculator so that a user can input a number with two values separated by an operator and the computer print out the proper answer. The calculator should round answers to two decimal places. Your calculator should include the ^ operator for exponentiation. All calculations should be done in the appropriately named function. Four of the results should be printed in main( ) and one result printed directly from within the function. Be sure to check for impossibilities in division and exponentiation.
The user should be able to use the calculator until they desire to quit.
Required Functions: add( ), subtract( ), multiply( ), divide( ), exponentiate( )
Required Operators: +, -, *, /, ^



//Final Program
//Program # 5
//I have really enjoyed this class!!

//This program is a simple calculator that allows a user to input a number with two values seperated by an operator and the computer print out the proper answer.

#include "iostream.h"
#include "math.h"
#include "iomanip.h"


//Declare variables
double operation;
double response;
double value_x;
double value_y;
double result;
double z;
int Y,y;
double Add_function (double, double);
double Subtract_function (double, double);
double Multiply_function (double, double);
double Divide_function (double, double);
double Exponent_function (double, double);

double main(void)
{
//Introduce program
cout <<"Welcome to Brenda's C++ Simple Calculator"<< endl;
cout <<" Program adds, subtracts, multiplies, divides, & exponentiates" << endl;
cout <<"---------------------------------------------------------------" << endl << endl;

//Ask user for the first value
cout << endl;
cout << "Enter the first value: " << endl;
cin >> value_x;


//Ask user for the second value
cout << endl;
cout << "Enter the second value: " << endl;
cin >> value_y;

//Ask the user to enter the operation they wish to perform
//While loop
//If statement
cout << endl;
cout << "Please enter the operation you wish to perform ( + , - , * , / , ^ ): ";
cout << "If you would like to exit the calculator please enter Q or q"<< endl;
cin >> operation;

do while ((operation != 'Q') || (operation != 'q'))
{
/*if (operation == '+')
{
z = Add_function(value_x,value_y);

}*/

if (operation == '-')
{
z= Subtract_function(value_x,value_y);
cout << "The value of" << value_x << operation << value_y << "=" << z << endl;
}

else if (operation == '*')
{
z= Multiply_function(value_x,value_y);
cout << "The value of" << value_x << operation << value_y << "=" << z << endl;
}

else if (operation == '/')
{
z= Divide_function(value_x,value_y);
cout << "The value of" << value_x << operation << value_y << "=" << z << endl;
}

else if (operation == '^')
{
z= Exponent_function(value_x,value_y);
cout << "The value of" << value_x << operation << value_y << "=" << z << endl;
}

else
{
cout << endl;
cout << "Sorry, that operation is not recognized!" << endl;
cout << "Do you want to perform another calculation? (Y or N)";
cin >> response;
cin.get ();
while ((response == Y) || (response == y));
{
cout << "Please enter the operation you wish to perform" << endl;
cin >> operation;
cout << endl << endl;
}
}
}

}

//functions

double Add_function(double x,double y)
{ double h;
h=x+y;
cout << "The value of" << x << "+" << y << "=" << h << endl;
return h;
}

double Subtract_function(double x,double y)
{ double b;
b=x-y;
return b;
}

double Multiply_function(double x,double y)
{ double c;
c=x*y;
return c;
}
double Divide_function(double x,double y)
{ double s;
if (( x == 0) || (y == 0))
{cout << "Cannot divide by 0. Result is undefined." << endl;
}
else
{
s=x/y;
}
return s;
}
double Exponent_function(double x,double y)
{
double p;
p= pow(x,y);
return p;
}





This is the second possibility of my program
i keep receiving this error
cpp(101) : error C2059: syntax error : '}'
warning C4508: 'main' : function should return a value; 'void' return type assumed

if i take out the "}"
i get the following error
.cpp(133) : fatal error C1004: unexpected end of file found



//Program # 5
//I have really enjoyed this class!!

//This program is a simple calculator that allows a user to input a number with two values seperated by an operator and the computer print out the proper answer.

#include "iostream.h"
#include "math.h"
#include "iomanip.h"

//functions

double Add_function(double x,double y)
{ double h;
h=x+y;
cout << "The value of" << x << "+" << y << "=" << h << endl;
return h;
}

double Subtract_function(double x,double y)
{ double b;
b=x-y;
return b;
}

double Multiply_function(double x,double y)
{ double c;
c=x*y;
return c;
}
double Divide_function(double x,double y)
{ double s;
if (( x == 0) || (y == 0))
{cout << "Cannot divide by 0. Result is undefined." << endl;
}
else
{
s=x/y;
}
return s;
}
double Exponent_function(double x,double y)
{
double p;
p= pow(x,y);
return p;
}


void main()
{
//Declare variables
double operation;
double response;
double value_x;
double value_y;
double result;
double z;
int Y,y;

//Introduce program
cout <<"Welcome to Brenda's C++ Simple Calculator"<< endl;
cout <<" Program adds, subtracts, multiplies, divides, & exponentiates" << endl;
cout <<"---------------------------------------------------------------" << endl << endl;

//Ask user for the first value
cout << endl;
cout << "Enter the first value: " << endl;
cin >> value_x;


//Ask user for the second value
cout << endl;
cout << "Enter the second value: " << endl;
cin >> value_y;

//Ask the user to enter the operation they wish to perform
//While loop
//If statement
cout << endl;
cout << "Please enter the operation you wish to perform ( + , - , * , / , ^ ): ";
cout << "If you would like to exit the calculator please enter Q or q"<< endl;
cin >> operation;

do while ((operation != 'Q') || (operation != 'q'))
{
/*if (operation == '+')
{
z = Add_function(value_x,value_y);

}*/

if (operation == '-')
{
z= Subtract_function(value_x,value_y);
cout << "The value of" << value_x << operation << value_y << "=" << z << endl;
}

else if (operation == '*')
{
z= Multiply_function(value_x,value_y);
cout << "The value of" << value_x << operation << value_y << "=" << z << endl;
}

else if (operation == '/')
{
z= Divide_function(value_x,value_y);
cout << "The value of" << value_x << operation << value_y << "=" << z << endl;
}

else if (operation == '^')
{
z= Exponent_function(value_x,value_y);
cout << "The value of" << value_x << operation << value_y << "=" << z << endl;
}

else
{
cout << endl;
cout << "Sorry, that operation is not recognized!" << endl;
cout << "Do you want to perform another calculation? (Y or N)";
cin >> response;
cin.get ();
while ((response == Y) || (response == y));
{
cout << "Please enter the operation you wish to perform" << endl;
cin >> operation;
cout << endl << endl;
}
}
}
May I ask why you have quotation marks around your includes O.O?

They go like this:

1
2
3
#include <iostream>
#include <math.h>  
#include <iomanip> 


May I ask why you have quotation marks around your includes O.O?

They go like this:

1
2
3
#include <iostream>
#include <math.h>  
#include <iomanip>  


They also go like this:

1
2
3
#include "iostream"
#include "math.h" 
#include "iomanip"  


The C++ standard specifies both.
Last edited on
Alright, thanks for the heads up. :)
Topic archived. No new replies allowed.