y always equals 1?

So it's my 3rd day of programming class and I'm having troubles with my program assignment. The assignment is listed in the comment. Everything ran fine when I had it square and cube the negative even/odd numbers. Once I got to the positive even and odd numbers, the program makes every output a 1 no matter what the number is. I tried to google search but I had no luck, I just don't know how to word my question correctly since I'm a newbie to this. Thanks for the help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 /*write a program that asks the user to enter an integer x.  Based on the value of x, do the following:

- if x is negative and even, calculate and display its value raised to the 3rd power

- if x is negative and odd, square it and display the result

- if x is greater than or equal to 0 and even, calculate and display the exponent of x, or exp(x)

- if x is grerter than or equal to 0 and odd, calculate and display the cube root of x

*/

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	//Declare variables
	short x, y;

	//Ask for Variables
	 cout << "Please enter a value for x: ";
	cin >>x;

	if ((x < 0) && (x % 2 == 0));
	{
		y = pow(x, 3);
	}
	if ((x < 0) && (x % 3 == 0));
	{
		y = pow(x, 2);
	}
	if ((x >= 0) && (x % 3 == 0));
	{
		y = pow(x, 1/3);
	}
	if ((x >= 0) && (x % 2 == 0));
	{
		y = pow(x, 1 / 2);
	}
	cout << "y=" << y << endl;
	
	return 0;
}
If you put a semicolon at the end of an if statement line it will create an if statement that does nothing.

1
2
3
4
if ((x < 0) && (x % 2 == 0)); // If x is negative and even, do nothing.
{
	y = pow(x, 3); // This is not part of the if statement so it will always be executed.
}


Also note that (x % 3 == 0) is not how you check if a number is odd.
Last edited on
Well that fixed most of my issue. I can see why (x % 3 == 0) doesn't mean a number is odd. I can't quite get my finger on how I would be able to check if a number is odd.
I can't quite get my finger on how I would be able to check if a number is odd.

You do successfully test for even numbers, don't you? What is a number that is not even?

You do know if. Have you seen else?


Another note: you have 1 / 3 and 1 / 2. Those are integer divisions and the result of both is 0. If you do want a floating point result, then at leas one of the operands has to be a float. For example: 1.0 / 3

Furthermore, the <cmath> has these:
http://www.cplusplus.com/reference/cmath/sqrt/
http://www.cplusplus.com/reference/cmath/cbrt/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*write a program that asks the user to enter an integer x.  Based on the value of x, do the following:

- if x is negative and even, calculate and display its value raised to the 3rd power

- if x is negative and odd, square it and display the result

- if x is greater than or equal to 0 and even, calculate and display the exponent of x, or exp(x)

- if x is grerter than or equal to 0 and odd, calculate and display the cube root of x

*/
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main()
{
	//Declare variables
	double x, y;

	//Ask for Variable
	cout << "Please enter a value for x: " << endl;
	cin >> x;

	if ((x < 0) && fmod(x , 2 == 0))
	{
		y = pow(x, 3);
		cout << "y=" << y << endl;
	} // If x is a negative number and divisable by 2, display the number cubed
	else
	{
		if ((x < 0) && fmod(x, 2 == 0))
		{
			y = -pow(x, 2);
			cout << "y=" << y << endl;
		} // If x is a negative number but has a remanider when divided by 2, hence a odd number, display the number sqaured
		else
		{
			if ((x > 0) && fmod(x, 2 == 0)) 
			{
				y = exp (x);
				cout << "y=" << y << endl;
			} // If x is a positive number and divasble by 2, display the exponent of that number

			else
			{
				if ((x > 0) && fmod(x, 2 != 0))
				{
					y = cbrt (x);
					cout << "y=" << y << endl;
				} // If x is a postive number and has a remainder when divided by 2, hence an odd number, display the cube root
			}
		}
	}

	return 0; 
}


Well I cleaned up a bit and used else but now it won't read what I consider to be an odd number. It was working when I was using % but I didn't like how exponents topped off at a certain number so I switched over to doubleand fmod for division.

EDIT: sheesh, shows how much I know about operators..
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*write a program that asks the user to enter an integer x.  Based on the value of x, do the following:

- if x is negative and even, calculate and display its value raised to the 3rd power

- if x is negative and odd, square it and display the result

- if x is greater than or equal to 0 and even, calculate and display the exponent of x, or exp(x)

- if x is grerter than or equal to 0 and odd, calculate and display the cube root of x

*/
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main()
{
	//Declare variables
	long x, y;

	//Ask for Variable
	cout << "Please enter a value for x: " << endl;
	cin >> x;

	if ((x < 0) && (x % 2 == 0))
	{
		y = pow(x, 3);
		cout << "y=" << y << endl;
	}
	if ((x < 0) && (x % 2 != 0))
	{
		y = pow(x, 2);
		cout << "y=" << y << endl;
	}
	if ((x > 0) && (x % 2 == 0))
	{
		y = exp(x);
		cout << "y=" << y << endl;
	}
	if ((x > 0) && (x % 2 != 0))
	{
		y = cbrt(x);
		cout << "y=" << y << endl;
	}
	return 0;
}


I went the easy way out, this should work up in till the limit of long. I wanted to be able to exceed that by using double but I feel like I spent to much time on this and it's due on Monday. Thanks for the help guys.
Last edited on
Topic archived. No new replies allowed.