My program is due today and I'm stuck at work trying to figure this out I have been using the web to get this done.

Instructions: 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.


This is what I have.

#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;

return 0;
}

I'm just not cofident at all about this!

i tested and it work.. which part you not confident?

I don't think you interpreted the requirements correctly. The way you've explained this program, it should take user input in a loop rather than calculate a range from user input:

#include <iostream>

bool user_input(int& value)
{
    std::cout<<"Enter a whole number: ";

    return std::cin>> value && value >= 0;
}

int main()
{
    int sum = 0;
    int value;

    while (user_input(value)) {
        // Add value to the sum if it's odd
    }

    std::cout<<"The total sum of odd numbers is "<< sum <<'\n';
}

The question seems like it should just stop if you enter a 0, like if I would enter 12304 it would give an error. I couldn't figure out how to do that and it doesn't seem like it would even fit into my structure.

You don't want a for loop in this program. You want a while loop. Narue's structure is essentially what you need (although you really should re-write it in a manner that's appropriate for your skill-level).

The part of the code that determines whether it's odd or not, and reacts accordingly, is up to you.

Hi Lokril!

Try out the following code:

#include <iostream>
using namespace std;

int main()
{
	int number=0;
	int sum=0;
	cout << "Please enter an odd number: ";
	cin >> number;
	while (number > 0)//stop input if 0 or a -ive number is entered
	{
		if (number%2 != 0)//if the number is not even i.e. it is odd
			sum = sum + number;//add number to the sum
		else
			cout << "It is an even number so will be ignored!\n";
		cout << "Please enter an odd number: ";
		cin >> number;
	}
	cout << "Sum of all the odd numbers you entered = " << sum << endl;
	system("pause");
	return 0;
}

If you think it does what you want to do then tell me and I shall explain to you in detail how it works..

OK???

commented: Do NOT do other's homework for them. You are not getting a grade and he doesn't deserve it! -3

If you think it does what you want to do then tell me and I shall explain to you in detail how it works..

OK???

So basically you've done his homework and you're willing to explain how your code works so he can successfully palm it off as his own? Good job. :)


Not!

So basically you've done his homework and you're willing to explain how your code works so he can successfully palm it off as his own? Good job. :)


Not!

Probably not!!

I actually thought he could not understand the problem correctly. If he compiles and runs a program and realizes that it is doing exactly what he wants to do than he will take more interest to match the code with problem statement and understand what he was actually supposed to do.

Anyhow if you think my post is urging plagiarism then I am really sorry.
I was not intending this!!!!!

As much as I'd love to assume the best in people, experience shows that giving away a complete solution results in the student simply taking it and turning it in rather than using it as a template for understanding and coming up with their own version. So around here we advocate leading people toward solutions without actually giving them away. This is a slower but more productive method of teaching because it forces the student to work.

Hi Lokril!

Try out the following code:

#include <iostream>
using namespace std;

int main()
{
	int number=0;
	int sum=0;
	cout << "Please enter an odd number: ";
	cin >> number;
	while (number > 0)//stop input if 0 or a -ive number is entered
	{
		if (number%2 != 0)//if the number is not even i.e. it is odd
			sum = sum + number;//add number to the sum
		else
			cout << "It is an even number so will be ignored!\n";
		cout << "Please enter an odd number: ";
		cin >> number;
	}
	cout << "Sum of all the odd numbers you entered = " << sum << endl;
	system("pause");
	return 0;
}

If you think it does what you want to do then tell me and I shall explain to you in detail how it works..

OK???

This is perfect and I do undterstand I had the math done for this method and I have a feeling I was suppose to do it this way but someone suggested otherwise. I was messing with (number%2 != 0) this ealier but someone suggest it wasn't the most efficent manner so which is why I wasn't confident...

Maybe not it doesn't seem to do what I want but I am going to try and adjust it.

I was messing with (number%2 != 0) this ealier but someone suggest it wasn't the most efficent manner so which is why I wasn't confident...

There's little need to micromanage that kind of thing. More often than not your compiler will optimize it into something equal to or better than the suggested alternative[*]. Use code that works and clearly expresses your intention, and consider optimizations only after you can prove with hard numbers that the current code is too slow.


[*] Which I assume was number & 1 rather than number % 2 . That's usually what the smarty pants micro-optimization folks recommend. ;)

This is perfect and I do undterstand I had the math done for this method and I have a feeling I was suppose to do it this way but someone suggested otherwise. I was messing with (number%2 != 0) this ealier but someone suggest it wasn't the most efficent manner so which is why I wasn't confident...

Maybe not it doesn't seem to do what I want but I am going to try and adjust it.

ok ... as you wish ...

good luck!!!!!

ok ... as you wish ...

good luck!!!!!

Why do you have two inputs for numbers?

The program asks me twice for a number not sure why you did that am I missing something?

Why do you have two inputs for numbers?

The program asks me twice for a number not sure why you did that am I missing something?

That would be the first number, then subsequent numbers. The first number is read outside of the loop because that particular variable is being used as a loop condition. It has to have some initial value.

I read my own assignment wrong and have been trying to do something that I wasn't.....

I got it sorted out thanks guy you have helped me a lot and the most important leasson I learned was to make sure I understand what I am trying to achive!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.