DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Trouble with loops - Calculate sum of even and odd integers (http://www.daniweb.com/forums/thread149422.html)

ShadowOfBlood Oct 6th, 2008 3:43 am
Trouble with loops - Calculate sum of even and odd integers
 
As a homework assignment, I was asked to write a program in C++ that calculates the sum of the even and odd integers in a list of integers given by the user. I think I'm on the right track, but I can't seem to get my loop right. Here's what I have so far:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
        int num=0, even=0, odd=0, i = 0;
        char stop = 'a';

    cout << "Enter a list of integers followed by the '$' sign: " << endl;               
    cin >> num;
        stop = num;

                while(stop != '$')
                {
                        if(num % 2 == 0) //If the number is even
                                even = even + num; //Add it to the current sum of even integers
                        else if(num % 2 != 0) //if the number is odd
                                odd = odd + num; //Add it to the current sum of odd integers
                        else
                        {
                                i = 1; //Otherwise, make i equal to 1
                        }

                        cin >> num;
                        stop = num;
                }

        if(i == 1)
                cout << "Please enter only integers" << endl;
        else if(i == 0)
        {
                cout << "The sum of the even integers is: " << even << endl;
                cout << "The sum of the odd integers is: " << odd << endl;
        }

        return 0;
}

Thanks for the help :)

chococrack Oct 6th, 2008 3:52 am
Re: Trouble with loops - Calculate sum of even and odd integers
 
Look at line 13

 stop = num;

Now ask yourself, "self, whats stop?"
"a character."
"self, whats num?"
"an integer."

Whats probably going down is that the the char (stop) is being assigned an integer, which in effect is tossing your loop off.

Are you only interested in positive integers? Because then you could ask for a negative value to terminate that list of integers.

while(num != -1)

ShadowOfBlood Oct 6th, 2008 4:20 am
Re: Trouble with loops - Calculate sum of even and odd integers
 
The question states: Write a program that reads a set of integers, and then finds and prints the sum of the even and odd integers.

I'm assuming it means positive and negative integers. Any ideas?

ShadowOfBlood Oct 6th, 2008 4:26 am
Re: Trouble with loops - Calculate sum of even and odd integers
 
Nvm, I was going about it wrong. I figured it out. Here's what I have:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
        int even = 0, odd = 0, dig, num;

        cout << "Please enter a list of numbers (ex. - 12345)" << endl;
        cin >> num;

        while (num > 0)
        {
                dig = num % 10;
                num = num / 10;
               
                if(dig % 2 == 0)
                        even = even + dig;
                else
                        odd = odd + dig;
        }

        cout << "The sum of the even integers is: " << even << endl;
        cout << "The sum of the odd integers is: " << odd << endl;

        return 0;
}

Thanks for the help :)

Sky Diploma Oct 6th, 2008 7:14 am
Re: Trouble with loops - Calculate sum of even and odd integers
 
Dont You Think it is giving you the wrong answer?

ShadowOfBlood Oct 6th, 2008 8:43 am
Re: Trouble with loops - Calculate sum of even and odd integers
 
It was giving me right answers when I checked it O.o

Is something wrong with it?

bhoot_jb Oct 6th, 2008 10:14 am
Re: Trouble with loops - Calculate sum of even and odd integers
 
Quote:

Originally Posted by ShadowOfBlood (Post 706281)
Nvm, I was going about it wrong. I figured it out. Here's what I have:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
        int even = 0, odd = 0, dig, num;

        cout << "Please enter a list of numbers (ex. - 12345)" << endl;
        cin >> num;

        while (num > 0)
        {
                dig = num % 10;
                num = num / 10;
               
                if(dig % 2 == 0)
                        even = even + dig;
                else
                        odd = odd + dig;
        }

        cout << "The sum of the even integers is: " << even << endl;
        cout << "The sum of the odd integers is: " << odd << endl;

        return 0;
}

Thanks for the help :)


what is quite confusing is the 'num' integer.
You said that you are taking input of a list of integers while you have just asked for a single integer.
I assume that you have taken the input of "list of integers" in a single integer 'num' itself, i.e., you have considered the value of 'num' as a list of integers (digit by digit) :|
if this is what you have assumed, i think this is a wrong approach. You would have rather used an array. :)
Think upon it.

chococrack Oct 6th, 2008 12:37 pm
Re: Trouble with loops - Calculate sum of even and odd integers
 
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
        int even = 0, odd = 0, dig, num;

        cout << "Please enter a list of numbers (-7777 to terminate)" << endl;
        cin >> num;

        while (num != -7777)  // off the wall check value
        {
               
                // dig = num % 10;
                // num = num / 10;          <--- what are these for?


               
                if(dig % 2 == 0)      // if the number is even
                        even += num; // add it to the previous even sum                                               
                else
                        odd += num;  // otherwise add it to the odd sum

                cout << "Enter next number: ";
                cin >> num;
        }

        cout << "The sum of the even integers is: " << even << endl;
        cout << "The sum of the odd integers is: " << odd << endl;

        return 0;
}

ShadowOfBlood Oct 6th, 2008 2:14 pm
Re: Trouble with loops - Calculate sum of even and odd integers
 
I haven't learned arrays yet :\

And what if the user wants to input -7777 as part of the list?

I dunno what I should do with this program :\

ShadowOfBlood Oct 6th, 2008 2:49 pm
Re: Trouble with loops - Calculate sum of even and odd integers
 
K, class has arrived. Thanks for the help =)

I decided to use the example with the -7777 terminating value =)


All times are GMT -4. The time now is 10:57 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC