Write a program that adds the positive odd numbers you enter from the keyboard while ignoring the even numbers, and stops when a negative number or zero are entered.
Display the sum of the accepted odd numbers.

Below is not working : /

#include <iostream>
using namespace std;

int main(void)
{
int N = 0;
int sum = 0;

cout << "Enter a whole number:" << endl;
cin >> N;

if ( N < 0 ){
cout << "The number was not within the proper range." << endl;
return 1;
}

for( int i = 1; i <= N; i = i + 2){
sum = sum + i;
}
cout << "The total sum is: " << sum << endl;
system("PAUSE");
return EXIT_SUCCESS;
return 0;
}
Your program only reads one number. It seems like what the assignment wants you to do is read input in a loop and add it to a sum if it is odd.
 ``123`` ``````for( int i = 1; i <= N; i = i + 2){ sum = sum + i; }``````

This is nonsense. What are you doing? You're supposed to add the number inputted to the sum if it's positive and odd. This for loop is something completely different.

You're supposed to keep reading input until a negative number (or zero) is entered, but you read just one number. You've ignored the instructions.
Last edited on
 ``12345678910`` ``````#include int main() { int input = 1, sum = 0; while ( input > 0 ) { std::cin >> input; if ( input % 2 ) sum += input; } std::cout << sum; return 0; }``````
Last edited on
Stew, I think that code adds the final (bad) number if it's odd.
Last edited on
Topic archived. No new replies allowed.