I have recently written code for a calculator. I've used functions to define how to do the operations. The user selects the operation using a character. That choose the if statement. My problem is no matter what the user choose the program works its way threw all the if statements.
#include <iostream>
usingnamespace std;
float division (float a, float b)
{
float r1;
r1=a/b;
return (r1);
}
float multiplication (float c, float d)
{
int r2;
r2=c*d;
return (r2);
}
float addition(float e, float f)
{
int r3;
r3=e+f;
return(r3);
}
float subtraction(float g, float h)
{
int r4;
r4=g-h;
return(r4);
}
int main()
{
char opselect;
float ma;
float mb;
float ans;
cout<<"Welcome to Ryan's Calculator!\n\n";
menu:
cout<<"Please select an operation:\n\n";
cout<<"D Division \nM Multiplication \nA Addition \nS Subtraction \n\n";
cout<<"Please use a letter to select the operation: ";
cin>>opselect;
if (opselect == 'D' || 'd')
{
cout<< "What two numbers would you like to divide: ";
cin>>ma;
cin>>mb;
ans = division (ma, mb);
cout<<"The answer is: "<<ans;
}
if (opselect == 'M' || 'm')
{
cout<< "What two numbers would you like to multiply: ";
cin>>ma;
cin>>mb;
ans = multiplication (ma, mb);
cout<<"The answer is: "<<ans;
}
if (opselect == 'A' || 'a')
{
cout<< "What two numbers would you like to add: ";
cin>>ma;
cin>>mb;
ans = addition (ma, mb);
cout<<"The answer is: "<<ans;
}
if (opselect == 'S' || 's')
{
cout<< "What two numbers would you like to subtract: ";
cin>>ma;
cin>>mb;
ans = subtraction (ma, mb);
cout<<"The answer is: "<<ans;
}
else
{
goto menu;
}
}
float division (float a, float b)
{
float r1;
r1=a/b;
return (r1);
}
float multiplication (float c, float d)
{
int r2;
r2=c*d;
return (r2);
}
float addition(float e, float f)
{
int r3;
r3=e+f;
return(r3);
}
float subtraction(float g, float h)
{
int r4;
r4=g-h;
return(r4);
}
If you look at your int r inside each function you see that you did float and then followed by int. Who is to say since this is a calculator that I might want to had 4.5 or 6.5 with 34.8?
all your int r should be float so that doesn't effect you. Secondly since I am sure that for you to use functions you probably had to deal understanding scopes. Each float/int r doesn't need to be called r1, r2.... since that variable is only being used by that function and that function only hence meaning that it is local you can call each one float r and r=a*b etc because these are local procedure in a function. plus it reduntant to accept demicals but not calculate them...
local ( in a specific function/class/structure that isn't made public ex. if, do_while,for, int name( int a, int b)
global can't be access from anywhere, for something to be declare global it has to be made outside of any function.
It's considered really bad (and dangerous) practice to use goto. DON'T do it. There is always an alternative for this.
In your case use a loop like while and a condition to check.
#include <iostream>
usingnamespace std;
float division (float a, float b)
{
float r;
r=a/b;
return (r);
}
float multiplication (float a, float b)
{
float r;
r= a*b;
return (r);
}
float addition(float a, float b)
{
float r;
r=a+b;
return(r);
}
float subtraction(float a, float b)
{
float r;
r=a-b;
return(r);
}
int main()
{
char opselect;
bool playagain = true;
char playagainchar;
float ma;
float mb;
float ans;
cout<<"Welcome to Ryan's Calculator!\n\n";
while (playagain == true)
{
cout<<"Please select an operation:\n\n";
cout<<"D Division \nM Multiplication \nA Addition \nS Subtraction \n\n";
cout<<"Please use a letter to select the operation: ";
cin>>opselect;
if (opselect == 'D' || opselect == 'd')
{
cout<< "What two numbers would you like to divide: ";
cin>>ma;
cin>>mb;
ans = division (ma, mb);
cout<<"The answer is: "<<ans<<"\n";
cout<<"Would you like to do another Calculation (Y/N): ";
cin>>playagainchar;
cout<<"\n\n";
if(playagainchar != 'y') playagain = false;
}
if (opselect == 'M' || opselect == 'm')
{
cout<< "What two numbers would you like to multiply: ";
cin>>ma;
cin>>mb;
ans = multiplication (ma, mb);
cout<<"The answer is: "<<ans<<"\n";
cout<<"Would you like to do another Calculation (Y/N): ";
cin>>playagainchar;
cout<<"\n\n";
if(playagainchar != 'y') playagain = false;
}
if (opselect == 'A' || opselect == 'a')
{
cout<< "What two numbers would you like to add: ";
cin>>ma;
cin>>mb;
ans = addition (ma, mb);
cout<<"The answer is: "<<ans<<"\n";
cout<<"Would you like to do another Calculation (Y/N): ";
cin>>playagainchar;
cout<<"\n\n";
if(playagainchar != 'y') playagain = false;
}
if (opselect == 'S' || opselect == 's')
{
cout<< "What two numbers would you like to subtract: ";
cin>>ma;
cin>>mb;
ans = subtraction (ma, mb);
cout<<"The answer is: "<<ans<<"\n";
cout<<"Would you like to do another Calculation (Y/N): ";
cin>>playagainchar;
cout<<"\n\n";
if(playagainchar != 'y') playagain = false;
}
}
cout<<"Thanks for calculating!";
cin.get();
return 0;
}