Question:
can someone help me fix this?
wolverine12389
2009-11-15 21:38:32 UTC
i need help debugging this


#include

int main() {
float val, sum, incr;
int ok,num;

ok = 0;
while (!ok) {
cout << "Enter an even number: ";
cin >> num;
if (num % 2 == 2)
cout << "The number " << num << " is not a positive even number." << endl;
else cout << num << " is even!" << endl << endl;
}

incr = 1 / num;

for (val = 20.0F; val < 21.0; val += incr) {
cout << val;
sum += val;
}

cout << "The sum is " << sum << endl;
return 0;

}


to give this

Enter an even number: 23
The number is not a positive even number.
Enter an even number: -6
The number is not a positive even number.
Enter an even number: 4

20 20.25 20.50 20.75 21
The sum is 102.5
Three answers:
Mike M
2009-11-15 22:18:39 UTC
Just wondering... what is the error you are getting? Also you are missing "using namespace std; at top before main(). Also you are not exiting your while loop. Also, when you do incr = 1/num; num is an int value. I believe what is happening is since incr is a float and num is an int upon execution, incr is being converted to the lowest accuracy variable within that function ie incr is being converted to an int. so within the for loop when it is incrementing val it is val = val + 0; so you are getting stuck there. Also if a number is even the mod will return a 0, and if it is odd will return a 1. So you are never really checking for even/odd. Below is the fixed code... Compare mine to yours and make sure you understand your errors. It is the only way you will learn.. Good Luck!



#include



using namespace std;



int main() {

float val, sum, incr, num;

int ok, check;



ok = 0;

while (!ok)

{

cout << "Enter an even number: ";

cin >> num;

check = int(num);

if (check%2 == 1)

{

cout << "The number " << check << " is not a positive even number." << endl;

}

else

{

cout << check << " is even!" << endl << endl;

ok = 1;

}

}



incr = 1/num;



for(val = 20.0; val < 21.0; val+= incr) {

cout << val << endl;

sum += val;

}



cout << "The sum is " << sum << endl;

cin >> incr ;

return 0;



}
Rati Goldberg
2009-11-15 22:21:16 UTC
1. Checking for even and odd -> even number are as follows num%2 == 0



2. Include the for loop inside the while loop so that the sum can be printed out so place it inside the if that is for even numbers and after printing the sum set it to zero.

3. Type cast incr so the 1/num if float else its integer division so incr wil be zero and the for loop will run forever.

4. Num shud be greater than zero else its an error and if num is zero u knw what will happen 1/0???



ANS



int main() {

float val, sum, incr;

int ok,num;

ok = 0;

while (!ok) {

cout << "Enter an even number: ";

cin >> num;

if (num % 2 == 0 && num > 0){

cout << num << " is even!" << endl << endl;

incr = (float)1 / num;

cout << incr << endl;



for (val = 20.0; val <= 21.0; val += incr) {

cout << val << " ";

sum += val;

}



cout << "The sum is " << sum << endl;

sum = 0;

}

else{

cout << "The number " << num << " is not a positive even number." << endl;

}



}
Manoj Patel
2009-11-15 22:27:24 UTC
first thing is the execution will never come out of while loop coz ok=0 and while(!ok) is always True



and there is mistake in if condition



FOLLOWING IS THE CORRECT PROGRAM



#include



int main() {

float val, sum, incr;

int ok,num;



ok = 0;

while (!ok) {

cout << "Enter an even number: ";

cin >> num;

if (num % 2 == 0 && num > 0){

cout << num << " is even!" << endl << endl;

ok=1

};

else

cout << "The number " << num << " is not a positive even number." << endl;

}



incr = 1 / num;



for (val = 20.0F; val < 21.0; val += incr) {

cout << val;

sum += val;

}



cout << "The sum is " << sum << endl;

return 0;



}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...