I am doing something wrong, I have tried for two days and I still cannot figure it out. Can someone help me? Thank you.

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void instructUser();
double doDivideZero(double &);




int main()
{
	instructUser();
	
	
	double displayedVal;
	double newEntry;
	char command_character ;

	
	displayedVal = 0.0;

	
	cout << "  Enter accepted Operator:" ;
	cin >> command_character;
	while (command_character != 'Q' || command_character != 'q')
	{
		switch(command_character)
		{
		case 'c':
		case 'C': displayedVal = 0.0;
				  break;
		case '+': cout << "  Enter Number:";
				  cin >> newEntry;
				  displayedVal = displayedVal + newEntry;
				  break;
		case '-': cout << "  Enter Number:";
				  cin >> newEntry;
				  displayedVal = displayedVal - newEntry;
				  break;
		case '*': cout << "  Enter Number:";
				  cin >> newEntry;
				  displayedVal = displayedVal * newEntry;
				  break;
		case '/': cout << "  Enter Number:";
			      cin >> newEntry;
				  displayedVal = displayedVal / newEntry;
				  if (newEntry == 0)
				  {
					  doDivideZero(double &);
				  }
				  
				  break;
		case '^': cout << "  Enter Number:";
				  cin >> newEntry;
				  displayedVal = pow (displayedVal,newEntry);
				  break;
				  default : cout << "  Unacceptable Operator(" << command_character << ")" << endl;
		}
		cout << "  The result so far is: " <<displayedVal<< endl;
		cout << "  Enter Operator:";
		cin >> command_character;
		
		
	}




	system ("pause");
	return 0;
}

void instructUser()

{
		cout << "                    " <<endl;
		cout << "  ***************************************************************************" <<endl;
		cout << "  *  This program takes your input and selected mathematical operator       *" <<endl;
		cout << "  *  and returns the answer to the screen. If an illegal operator is        *" << endl;
		cout << "  *  selected, an error message will be displayed. Be careful which         *" <<endl;
		cout << "  *  operator you select because the program depends on you for input.      *" <<endl;
		cout << "  *  The only error check function it has, is for unacceptable opreators.   *" <<endl;
		cout << "  *  Acceptable operators are : (+ , - , / , * ,^,c). The character c sets  *" <<endl;
		cout << "  *  the value stored to ZERO. Enter Q to exit. ENJOY YOUR PROGRAM !!!!!!   *" <<endl;
		cout << "  ***************************************************************************" <<endl;
		cout << "                     " <<endl;



}

double doDivideZero(double &)
{
	double newEntry;
	double displayedVal;
	newEntry =0;
	displayedVal = 0.0;
	


	if (newEntry !=0)
	{
		displayedVal = displayedVal / newEntry;
	} else cout << "Wrong Operation, Cannot Divide by Zero" << endl;
	
		
		
	
	return 0;	
}

:cry: :cry: :cry:

Greetings kisseric,

Try changing the line:

if (newEntry == 0)
{
	doDivideZero(double &);
}

To:

if (newEntry == 0)
{
	doDivideZero(displayedVal);
}

Once I did this, your program ran just fine. I compiled it in Dev-C++.

- Stack Overflow

Thanks for the tip. It compiled OK but it messed up the other operations. For example after trying to divide by Zero, I am unable to do any other operations. How do I for the program to quit after an attempt to divide by Zero?

Also, check this line:

double doDivideZero(double &)

You need to name your variable, such as (double& x)

Forget about your doDivideZero function and replace some of your code with

case '/': cout << "  Enter Number:";
			      cin >> newEntry;
				  if (newEntry == 0)
				  {
		              cout << "Wrong Operation, Cannot Divide by Zero" << endl;
		              newEntry = 1;
				  }
				  displayedVal = displayedVal / newEntry;
				  break;

This keeps the program going ...

#include <iostream>


using namespace std;



float sub (float a, float b)
{
float dif;
dif=a - b;
return dif;
}


float addition (float c, float d)
{
float sum;
sum=c+d;
return sum;
}



float divi (float e, float f)
{
float quo;
quo = e/f;
return quo;
}


float multi (float f, float g)
{
float pro;
pro=f * g;
return pro;
}


int main ()
{
float a;
float b;
float opt;


cout << "Welcome to the calculator" << endl;
cout << "There are four options " << endl;
cout << "Option 1 is subtraction, Option 2 is addition, option 3 is division, and option 4 is multiplication " << endl;
cout << "Enter your option and two digits " << endl;
cin >> opt >> a >> b;



if (opt == 1)
{
float temp= sub (a, b);
cout << "Your difference is " << temp << endl;
}


else
if (opt==2)
{
float temp2= addition (a, b);
cout << "Your sum is " << temp2 << endl;
}
else
if (opt == 3)
{
float temp3= divi (a,b);
cout << "Your quotient is " << temp3 << endl;
}
else
if (opt == 4)
{
float temp4= multi (a,b);
cout << "Your product is " << temp4 << endl;
}
else
{
cout << "That function is not supported in calculator!" << endl;
}
cout << "Thank you for using calculator" << endl;


return 0;
}
//Trademark of JASE Inc

Edited 3 Years Ago by happygeek: fixed formatting

Comments
4 years late, and no code tags

hay guys i just have to ask that i wana make a programe which will take a input from user and then print it linewise on screen for example if i enter 3214 then it will print
3
2
1
4
please help me soon ..its my assignment

your line numbers dont need to be there, thats part of the reason your code is screwing up that was almost the whole reason......second the complier im using says the "cout" is undeclared and in parentheses it says (first use this function).....the complier i use is Dev-C++ if that matters.

Talk about resurrecting the dead on Halloween.. never seen a post this old get bumped to the top in a long time (this thread originated back in 2004!)

make a calculator program to add ,substract,multiply,divide and get the square root of the number...
tnx

hope youwill help me...

And yet another hijack bump to a 6 year old thread. I don't think he read even one thing he was asked to read upon registering, so why would he read the important stickys and announcements at the top of the forum itself?

#include<iostream>
#include<conio.h>

using namespace std;

int add(int a, int b)
{
    cout<<"Please enter a number"<<endl;
    cin>>a;
    cout<<"Please enter another number to add\n";
    cin>>b;
    int c = a + b;
    cout<<"Output: "<<a<<" + " <<b<< " = "<<c;
    return c;
}

int sub(int a, int b)
{
    cout<<"Please enter a number"<<endl;
    cin>>a;
    cout<<"Please enter another number to subtract\n";
    cin>>b;
    int c = a - b;
    cout<<"Output: "<<a<<" - " <<b<< " = "<<c;
    return c;
}

int mul(int a, int b)
{
    cout<<"Please enter a number"<<endl;
    cin>>a;
    cout<<"Please enter another number to multiply\n";
    cin>>b;
    int c = a * b;
    cout<<"Output: "<<a<<" * " <<b<< " = "<<c;
    return c;
}

int devide(int a, int b)
{
    cout<<"Please enter a number"<<endl;
    cin>>a;
    cout<<"Please enter another number to devide\n";
    cin>>b;
    int c = a / b;
    cout<<"Output: "<<a<<" / " <<b<< " = "<<c;
    return c;
}


int input(int option,int a,int b)
{
    while(1)
    {
cout<<endl;
cout<<"-----------------------------------------------------\n"<<endl;
    cout<<"Please select the option you want to perform"<<endl;
    cout<<"1. Addition"<<endl<<"2. Subtraction"<<endl<<"3. Multiplication\n"<<"4. Devision\n"<<"5. Exit Calculator\n";
    cout<<"\nPlease enter your option:";
    cin >>option;
    switch(option){
        case 1:
    cout<<"You have selected Addition\n";
    add(a,b);
    break;
case 2:
    cout<<"You have selected Subtraction\n";
    sub(a,b);
    break;
    case 3:
    cout<<"You have selected Multiplication\n";
    mul(a,b);
    break;
    case 4:
    cout<<"You have selected Devision\n";
    devide(a,b);
    break;
case 5:
    cout<<"Exiting...";
    break;
    default:
    cout<<"Please select a valid option";
}
if (option == 5)
{
    break;
}

    }
}

int main()
{
cout<<"Caculator";
int option,a,b;
input(option,a,b);
getch();
return 0;
}
This article has been dead for over six months. Start a new discussion instead.