Debugging program
Mar 19, 2012 at 9:06pm UTC
Hey, my program is supposed to take two values and determine if they are even or odd using a bool value, if both are even, then it adds the sum of the squared numbers but only if they are even. If they are odd it prompts the user to pick another two numbers. Here's the program:
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include <iostream>
using namespace std;
int evenSquares(int , int );
bool checkPositive(int ,int );
int main()
{
int int1, int2, sum;
cout <<"Please enter two integers" << endl;
cin >> int1 >> int2;
checkPositive(int1, int2);
while (!(checkPositive(int1, int2)))
{
cout<<"One or more of the integers you entered are invaild.\n" <<endl;
cout <<"Please enter two integers" << endl;
cin >> int1 >> int2;
}
sum = evenSquares(int1, int2);
cout << sum << endl;
system("Pause" );
return 0;
}
int evenSquares(int x, int y)
{
int num;
if ( x >= y)
{
while (y <= x)
{
if (y % 2 == 0)
{
num += y * y;
y++;
}
else if (y % 1 == 0)
y++;
}
}
else
{
while (x <= y)
{
if (x % 2 == 0)
{
num += x * x;
x++;
}
else if (x % 1 == 0)
x++;
}
}
return num;
}
bool checkPositive(int a, int b)
{
bool status;
if (a % 2 == 0 && b % 2 == 0)
{
status = true ;
return status;
}
else
{
status = false ;
return status;
}
}
It's not the evenSquares fault though it has something to do with the highlighted area. Here's the evenSquare function by itself, it works just fine:
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
#include <iostream>
using namespace std;
int evenSquares(int ,int );
int main()
{
int int1, int2, sum;
cout << "2 numbers" << endl;
cin >> int1 >> int2;
sum = evenSquares(int1, int2);
cout << sum << endl;
system("pause" );
return 0;
}
int evenSquares(int x, int y)
{
int num;
if ( x >= y)
{
while (y <= x)
{
if (y % 2 == 0)
{
num += y * y;
y++;
}
else if (y % 1 == 0)
y++;
}
}
else
{
while (x <= y)
{
if (x % 2 == 0)
{
num += x * x;
x++;
}
else if (x % 1 == 0)
x++;
}
}
return num;
}
suggestions?
Mar 19, 2012 at 10:08pm UTC
Do one thing and do it well.
my program is supposed to take two values and determine if they are even
1 2 3
bool is_even( int n ){
return n%2 == 0;
}
if both are even, then it adds the sum of the squared numbers but only if they are even
1 2
if ( is_even(a) and is_even(b) )
sum = square(a)+square(b);
On a second read ¿wtf are you doing?
checkPositive() is checking parity,
x%1==0
is a tautology, you repeat almost the same code...
Simplify a little. Suppose that when you call
evenSquares() both numbers are even (actually they are), and that x<=y
Last edited on Mar 19, 2012 at 10:21pm UTC
Topic archived. No new replies allowed.