#include <iostream>
usingnamespace std;
int choose(){
char select;
cin>>select;
switch(select){
case'i':int iCalculator(int x, int y);
break;
case'd': double iCalculator(double dx, double dy);
break;
case'q': exit(1);
break;
default:"Select a valid choice";
}
}
int main(){
cout<<"Calculator Ready"<<endl;
cout<<"Enter i for integer, d for double and q to quit"<<endl;
cout<<choose;
system("pause");
return 0;
}
int iCalculator(int x, int y){
cout<<"Enter first interger";
cin>>x;
cout<<"Enter Second Interger";
cin>>y;
cout<<"What operation do you want for your intergers?(+,-.*,/)";
char operating;
cin>>operating;
int calculated=0;
switch(operating){
case'+': calculated=x+y;
break;
case'-': calculated=x-y;
break;
case'*': calculated=x*y;
break;
case'/': calculated=x/y;
if(y==0){cout<<"Cannot divide by zero";
iCalculator(x,y);};
break;
default: cout<<"look here genuis I gave you a choice, now choose a valid response, you dip.";
break;
}
return calculated;
}
double iCalculator(double dx, double dy){
cout<<"enter your first double: ";
cin>>dx;
cout<<"enter your second double: ";
cin>>dy;
cout<<"What operation do you want for your doubles?(+,-.*,/)";
char operating;
cin>>operating;
double calculated=0.0;
switch(operating){
case'+': calculated=dx+dy;
break;
case'-': calculated=dx-dy;
break;
case'*': calculated=dx*dy;
break;
case'/': calculated=dx/dy;
if(dy==0){cout<<"Cannot divide by zero";
iCalculator(dx,dy);};
break;
default: cout<<"look here genuis I gave you a choice, now choose a valid response, you dip.";
break;
}
return calculated;
}
To return a value from a function you have to use a return statement. On line 9 and 11 you are not calling the functions, you are simply declaring them. Calling iCalculator(int,int); would look something like this: iCalculator(0, 0); And if you want to return the value that the function return you put the return keyword in front of it, like this: return iCalculator(0, 0);
x and y (and dx and dy) shouldn't really be parameters because the initial values are not used by the function. Instead I think you should give the functions two different names (e.g. iCalculator and dCalculator) and declare x and y as normal variables inside the functions.
I cleaned it up a bit with your suggestions. It still give me the "not all control paths return a value" warning for the int choose function(line 82), and now a conversion data loss error at line 75. The program itself when run gives the statements in main(), but is followed by a random reference I think, and the "press any key to continue."
#include <iostream>
usingnamespace std;
double dCalculator(){
double x;
double y;
cout<<"enter your first double: ";
cin>>x;
cout<<"enter your second double: ";
cin>>y;
cout<<"What operation do you want for your doubles?(+,-.*,/)";
char operating;
cin>>operating;
double calculated=0.0;
switch(operating){
case'+': calculated=x+y;
break;
case'-': calculated=x-y;
break;
case'*': calculated=x*y;
break;
case'/': calculated=x/y;
if(y==0){cout<<"Cannot divide by zero";
dCalculator();};
break;
default: cout<<"look here genuis I gave you a choice, now choose a valid response, you dip.";
break;
}
system("pause");
return calculated;
}
int iCalculator(){
int x;
int y;
cout<<"Enter first interger";
cin>>x;
cout<<"Enter Second Interger";
cin>>y;
cout<<"What operation do you want for your intergers?(+,-.*,/)";
char operating;
cin>>operating;
int calculated=0;
switch(operating){
case'+': calculated=x+y;
break;
case'-': calculated=x-y;
break;
case'*': calculated=x*y;
break;
case'/': calculated=x/y;
if(y==0){cout<<"Cannot divide by zero";
iCalculator();};
break;
default: cout<<"look here genuis I gave you a choice, now choose a valid response, you dip.";
break;
}
system("pause");
return calculated;
}
int choose(){
char select;
cin>>select;
switch(select){
case'i':return iCalculator();
break;
case'd': return dCalculator();
break;
case'q': return 0;
break;
default:"Select a valid choice";
}
system("pause");
}
int main(){
cout<<"Calculator Ready"<<endl;
cout<<"Enter i for integer, d for double and q to quit"<<endl;
cout<<choose;
system("pause");
}
#include <iostream>
usingnamespace std;
double dCalculator()
{
double x;
double y;
cout<<"enter your first double: "<<endl;
cin>>x;
cout<<"enter your second double: "<<endl;
cin>>y;
cout<<"What operation do you want for your doubles?(+,-.*,/)"<<endl;
char operating;
cin>>operating;
double calculated=0.0;
switch(operating){
case'+': calculated=x+y;
break;
case'-': calculated=x-y;
break;
case'*': calculated=x*y;
break;
case'/': calculated=x/y;
if(y==0){cout<<"Cannot divide by zero";
dCalculator();};
break;
default: cout<<"look here genuis I gave you a choice, now choose a valid response, you dip.";
break;
}
system("pause");
cout<<calculated<<endl;
return calculated;
}
int iCalculator(){
int x;
int y;
cout<<"Enter first interger"<<endl;
cin>>x;
cout<<"Enter Second Interger"<<endl;
cin>>y;
cout<<"What operation do you want for your intergers?(+,-.*,/)"<<endl;
char operating;
cin>>operating;
int calculated=0;
switch(operating){
case'+': calculated=x+y;
break;
case'-': calculated=x-y;
break;
case'*': calculated=x*y;
break;
case'/': calculated=x/y;
if(y==0){cout<<"Cannot divide by zero";
iCalculator();};
break;
default: cout<<"look here genuis I gave you a choice, now choose a valid response, you dip.";
break;
}
cout<< calculated<<endl;
system("pause");
return calculated;
}
double choose(){
char select;
cin>>select;
switch(select){
case'i':return iCalculator();
break;
case'd': return dCalculator();
break;
case'q': return 0;
break;
default:"Select a valid choice"; }
system("pause");
}
int main(){
cout<<"Calculator Ready"<<endl;
cout<<"Enter i for integer, d for double and q to quit"<<endl;
cout<< choose();
}