Can someone tell me what I am doing wrong? I started to learn programming last week so i am still not that great at it. Thanks
//Function and If else program
#include <iostream>
using namespace std;
int func (int x, int y);
int main()
{
int x;
int y;
cout<<"In this program when x > y... x - y is executed\n"
<<"and when x < y... x * y is executed\n";
cout<<"Enter number for x: " << "\n";
cin>> x;
cout<<"Enter number for y: " << "\n";
cin>> y;
}
int func (int x, int y)
{
if (x > y)
{
cout<<"The difference is: "<<endl;
return x - y;
}
else
{
cout<<"The product is: "<<endl;
return x * y;
}
I just want this program to take int x and int y and the if int x is greater then int y i want it to subtract but if int x is less than int y i want it to multiply
//Function and If else program
#include <iostream>
using namespace std;
int func (int x, int y);
int main()
{
int x;
int y;
cout<<"In this program when x > y... x - y is executed\n"
<<"and when x < y... x * y is executed\n";
cout<<"Enter number for x: " << "\n";
cin>> x;
cout<<"Enter number for y: " << "\n";
cin>> y;
cout << func( x, y ) << endl;
system("PAUSE");
return 0;
}
int func (int x, int y)
{
if (x > y)
{
cout<<"The difference is: "<<endl;
return x - y;
}
else
{
cout<<"The product is: "<<endl;
return x * y;
}
Hi
At 1st, return 0 and system("PAUSE") must be in main() and the function func must come out of main, following it. Then, generally speaking, we define functions as subroutines that can be used to accomplish some task, however, definition of a function doesn't do anything on its own unless the function is called. So your program must be like this:
#include <iostream>
using namespace std;
int func (int x, int y);
int main()
{
int x;
int y;
cout<<"In this program when x > y... x - y is executed\n"
<<"and when x < y... x * y is executed\n";
cout<<"Enter number for x: " << "\n";
cin>> x;
cout<<"Enter number for y: " << "\n";
cin>> y;
cout<<func(x,y);
system("PAUSE");
return 0;
}
int func (int x, int y)
{
if (x > y)
{
cout<<"The difference is: "<<endl;
return x - y;
}
else
{
cout<<"The product is: "<<endl;
return x * y;
}
}
However, writing programs in this fashion is not as efficient as expected in professional programs.
Good luck