Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,578 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,932 people online right now. Registration is fast and FREE... Join Now!




C++

 
Reply to this topicStart new topic

C++, Help in assignment

bori_77
3 Oct, 2008 - 02:23 PM
Post #1

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 27

I am taking a c++ course and I am new at this. I am doing an assignment that supposed to read a set of 5 integers and then finds and prints the sum of even and odd integers. I somewhat wrote the program but it still has errors. I am not sure that I have all the components in for I still don't get this course. Here is what I have. Can someone look at it and give some advice in what I should do next for i have no clue?

cpp
#include <iostream>
#include <iomanip>

using namespace std;

using namespace std;

const int N = 5;

int main(void)
{

//Variable Declaration
int counter; //loop control variable
int number; //variable to store the new number
int zeros = 0;
int odds = 0;
int evens = 0;
int sum_evens = 0;
int sum_odds = 0;

cout << "Please enter " << N << " 5 integers. " << endl;

cout << "The numbers you entered are:" << endl;

for (counter = 1; counter <= N; counter++)
{
cin >> number;
cout << number << " ";

switch (number % 2 )
{ if (number % 2 == 0)
sumEvens = sumEvens + number; // update even number
else
sumOdds = sumOdds + number; // update odd sum

}
}

cout << endl;

cout << "The even sum is: " << sumEvens
<< endl;

cout << "The odd sum is: " << sumOdds;
<< end;

cin.ignore();//causes dos screen to pause
return 0;

}

MOD EDIT: Please code.gif
Thanks, gabehabe smile.gif
User is offlineProfile CardPM
+Quote Post

Sadaiy
RE: C++
3 Oct, 2008 - 02:45 PM
Post #2

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 38



Thanked: 1 times
My Contributions
i think if you are using a loop to do this with a counter then you need to use an array like so:

CODE

#include <iostream>

using namespace std;

const int X = 5;

int main()
{
    int num[5];

    int sumEven = 0, sumOdd = 0;

    cout << "Please enter 5 integers" << endl;
        
    for(int counter = 0; counter < X; ++counter)
    {
        cin >> num[counter];
    }

    cout << "The numbers you entered are: " << endl;

    for(int counter = 0; counter < X; ++counter)
    {
        cout << num[counter] << " ";
    }

    for(int counter = 0; counter < X; ++counter)
    {
        if(num[counter] % 2 == 0)
            sumEven += num[counter];
        else
            sumOdd += num[counter];
    }

    cout << "The sum of the even numbers is: " << sumEven << endl;
    cout << "The sum of the odd numbers is: " << sumOdd << endl;

    return 0;
}

User is offlineProfile CardPM
+Quote Post

bori_77
RE: C++
4 Oct, 2008 - 04:03 PM
Post #3

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 27

QUOTE(Sadaiy @ 3 Oct, 2008 - 03:45 PM) *

i think if you are using a loop to do this with a counter then you need to use an array like so:

CODE

#include <iostream>

using namespace std;

const int X = 5;

int main()
{
    int num[5];

    int sumEven = 0, sumOdd = 0;

    cout << "Please enter 5 integers" << endl;
        
    for(int counter = 0; counter < X; ++counter)
    {
        cin >> num[counter];
    }

    cout << "The numbers you entered are: " << endl;

    for(int counter = 0; counter < X; ++counter)
    {
        cout << num[counter] << " ";
    }

    for(int counter = 0; counter < X; ++counter)
    {
        if(num[counter] % 2 == 0)
            sumEven += num[counter];
        else
            sumOdd += num[counter];
    }

    cout << "The sum of the even numbers is: " << sumEven << endl;
    cout << "The sum of the odd numbers is: " << sumOdd << endl;

    return 0;
}




ok so would I take out the case and make it into an array or do I incorporate into my program and place it where my case statement is?
User is offlineProfile CardPM
+Quote Post

bori_77
RE: C++
4 Oct, 2008 - 04:51 PM
Post #4

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 27

another question when i try to run it it saids that the project is not complete. why is that? how do i get to the screen that i can input 5 integers and see if it adds the odd and even numbers?

This post has been edited by bori_77: 4 Oct, 2008 - 04:55 PM
User is offlineProfile CardPM
+Quote Post

UG Cyber
RE: C++
4 Oct, 2008 - 05:06 PM
Post #5

D.I.C Head
**

Joined: 24 Jul, 2008
Posts: 185



Thanked: 3 times
My Contributions
The question with that is what compilier you are using?? and wich code are you using, your orig or sadaiy's code
User is offlineProfile CardPM
+Quote Post

bori_77
RE: C++
5 Oct, 2008 - 01:13 PM
Post #6

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 27

I finished what i think is the program that i'm supposed to do but I have 6 errors that i can't get rid of. can someone look at it and tell me what is wrong with it because i can't seem to see it?

[/code
#include <iostream>
#include <conio.h> //for getch()

using namespace std;

const int N = 5;

int main(void)
{
//Variable Declaration
int counter; //loop control variable
int number = 0;

int sum_evens = 0;
int sum_odds = 0;

cout << "Please enter " << N << "5 integers. " << endl;

for (int counter = 0; counter < N; ++counter)
{
cin >> number[counter];
}
cout << "The numbers you entered are:" << endl;

for (int counter = 0; counter < N; ++counter)
{

cout << number[counter] << " ";
}
for (int counter = 0; counter < N; ++counter)
{
if (number[counter] % 2 == 0)
sum_evens = sum_evens + number[counter]; // update even number
else
sum_odds = sum_odds + number[counter]; // update odd sum
}

cout << endl;

cout << "The even sum is: " << sum_evens
<< endl;

cout << "The odd sum is: " << sum_odds
<< endl;

{
std::cout <<"Press any key to continue" <<std::endl;
getch();

return 0;

}
}


]

User is offlineProfile CardPM
+Quote Post

bori_77
RE: C++
5 Oct, 2008 - 01:29 PM
Post #7

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 27

Thanks anyways ..I figured it out!!!!! Now let see whats the next program.....for next time. This is a very helpful website. I would reccomend it.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:20AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month