C++ Issue

TheRundownRabbit

Wicked Prolapse
Aug 27, 2009
3,826
0
0
Alright, I am having trouble with my program. In a switch statement I created for something called "MatLab Jr." I have various functions that are supposed to be selected. One does exponents, another, hypotenuse etc etc. The issue is, for the functions, every time the values are entered, the same thing happens "The answer is 0"

Any help?
 

thesilentman

What this
Jun 14, 2012
4,513
0
0
Haseo21 said:
Alright, I am having trouble with my program. In a switch statement I created for something called "MatLab Jr." I have various functions that are supposed to be selected. One does exponents, another, hypotenuse etc etc. The issue is, for the functions, every time the values are entered, the same thing happens "The answer is 0"

Any help?
Is it okay if you post your code? I'll have no clue what's causing it until I actually see it just enclose it in code and /code like this:

Code:
 tsm@ubuntu:~$ firefox www.escapistmagazine.com
Quote me if you don't know how to use the tags.

I'll be here, but I apologize if I don't get back immediately.
 

TheRundownRabbit

Wicked Prolapse
Aug 27, 2009
3,826
0
0
thesilentman said:
Haseo21 said:
Alright, I am having trouble with my program. In a switch statement I created for something called "MatLab Jr." I have various functions that are supposed to be selected. One does exponents, another, hypotenuse etc etc. The issue is, for the functions, every time the values are entered, the same thing happens "The answer is 0"

Any help?
Is it okay if you post your code? I'll have no clue what's causing it until I actually see it just enclose it in code and /code like this:

Code:
 tsm@ubuntu:~$ firefox www.escapistmagazine.com
Quote me if you don't know how to use the tags.

I'll be here, but I apologize if I don't get back immediately.
Here it is
Code:
#include 
#include 
#include <stdlib.h>
using namespace std;

void menu();
int power(int x, int y, int ans);
int hypotenuse(int ans, int x, int y);
int absolute(int x, int ans);
int prime(int x, int y, int ans);

int x;
int y;
int answer;
int choice;
int number;


int main()
{
	menu();
}
	
int power(int y, int x, int answer)
{
 answer = x ^ y;
}
int hypotenuse(int answer, int x, int y)
{
 answer = sqrt((x * x)+(y * y));
}
int absolute(int x, int answer)
{
 answer = fabs(x);
}
int prime(int x, int y, int answer)
{
 answer < x % !0;
}

void menu()
{
cout << "Welcome to Matlab Jr. what would you like to do?" << endl;
	cout << " 1. Power " << endl << " 2. Hypotenuse " << endl << " 3. Absolut Value " << endl << " 4. Prime Number " << endl << " 5. Quit " << endl;
	cin >> choice;
	
	choice = choice;
	
switch ( choice )
	{ 
	 case 1:
	 
	 cout << "Input the value you to find the power of." << endl;
	 cin >> x;
	 cout << "Input the power." << endl;
	 cin >> y;
	 
	 int power();
	 
	 cout << "The answer is " << answer << endl;
	 break;
	 
	 case 2:
	 
	 cout << "Input a value for x." << endl;
	 cin >> x;
	 cout << "Input a value for y." << endl;
	 cin >> y;
	 
	 int hypotenuse();
	 
	 cout << "Hypotenuse is " << answer <<endl;
	 
	 break;
	 
	 case 3:
	 
	 cout << "Please input an x value." << endl;
	 cin >> x;
	 
	 int absolute();
	 
	 cout << "The absolute value is " << answer << endl;
	 
	 break;
	 
	 case 4:
	 
	 cout << "Please input an x value." << endl;
	 cin >> x;
	 
	 int prime();
	 
	 break;
	 
	 case 5:
	 
	 break;
	 
	}
}
 

RoboGeek

New member
Apr 3, 2010
128
0
0
Im not 100% sure as i haven't used mat lab yet but your not sending your variables to your math functions although X and Y are global so it might not matter, try doing this for your functions
Code:
int power(int y, int x )
{
int answer
answer = x ^ y;
return answer;
}     

void menu(){

cout << "Input the value you to find the power of." << endl;
cin >> x;
cout << "Input the power." << endl;
cin >> y;

int answer1= int power(y,x)

cout << "The answer is " <<answer1 << endl;

}
i might be wrong but thats how i would usually code a function like that
 

Assassin Xaero

New member
Jul 23, 2008
5,392
0
0
Think RoboGeek is correct. I mostly only know C# (mess with C++ maybe once - so this may not all be correct), but the "answer" is not being set:

int hypotenuse(int answer, int x, int y)
{
answer = sqrt((x * x)+(y * y));
}

How that even builds amazes me since it is an int but doesn't return anything.

When passing "answer" in, it because local to that method (or whatever they are called in C++). Way I see it, you have a few options:

1. Return "answer" as RoboGeek said (which would not require it being passed in).
2. Don't pass it in and leave as it since "answer" is declared outside of the method.
3. If you must pass answer in, make it an "out":

void hypotenuse(out int answer, int x, int y)
{
answer = sqrt((x * x)+(y * y));
}

The third isn't needed since turning answer would be better, plus I'm not sure if "out" even works in C++. So, hopefully that may help.
 

thesilentman

What this
Jun 14, 2012
4,513
0
0
...And it seems RoboGeek beat me to it. Also, double-check the math functions.

That's all. Come back anytime for more help!
 
Apr 8, 2010
463
0
0
From what I can tell there are some issues with that thing you have there and I'm also genuinely surprised that this compiles - it sure enough didn't for me without some casting and serious tweaking. Anyway, here's a small example of code that works and bears some similarity to your stuff there - take a look at this.


Code:
#include <math.h>
#include <stdio.h>
#include 

using namespace std;

int x, y;
double answer;

void Hypotenuse()
{
double a = (double) x;
double b = (double) y;
answer = sqrt((a*a) + (b*b));
}


int main()
{
printf("insert integer \n");
cin >> x;
cin >> y;
printf("Your input was %4u %4u \n", x, y);
Hypotenuse();
printf("Hypotenuse of x and y is %4e \n", answer);
system("PAUSE");
return 0;
}
Now the following errors really stuck out to me while trying to get your code to work:
[ul][li] Function variables: every variable you define within a function declaration is local i.e. only usable within that function. Since your variables x and y are global, you don't need to specify them as separate variables in the function. The function can use them just fine because they are global [/li][/ul]

[ul][li] Function declaration type: the prefix in front of a function defines what datatype the function returns. If the function itself doesn't return anything but merely changes some external variable (as in your case with answer) you don't need them to return anything and you can set them to void.[/li][/ul]

[ul][li]Function call: In your code you called your functions like
Code:
int power (int, int, int)
by writing
Code:
int power();
. That's not correct: since the value inside the variables in brackets are actually expected to be given to the function when called you'd need to specify the variables during the call. You also don't need the data type out front. Hence you'd call power like
Code:
power(2, 4, 1);
for instance.[/li][/ul]

[ul][li]Short function declaration: When you use short function declarations as in the first lines of your code, you don't give the variable name but only the expected types. Later during your definition you need to name them inside the brackets.[/li][/ul]

[ul][li] Type compatiblity: Some of the functions you use (sqrt, fabs) expect float or double types. I had to cast the integers into those types prior to using those functions. I wonder why that isn't the case for you here.[/li][/ul]

Take that stuff with a grain of salt though - I'm no expert on the matter.

You might also want to take a look at this tutorial here [http://www.cplusplus.com/doc/tutorial/]. It's very well written and might be worth checking out if you want to know more.