I'm have a really hard time with this program and am totally lost. I'm trying to write a program that reads a set of integers and then finds the sum of the even and odd integers. So far this is all I have come up with, but I'm sure it's wrong.

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <fstream>

using namespace std;

const int N = 20;

int main()
{
  int counter;
  int number;
  int even;
  int odd;
  double sumEven;
  double sumOdd;
  ifstream infile;

  infile.open("C:\\input.txt");

  for (counter = 1; counter <= N; counter++)
  {
    cin >> number;
    sumEven = even + number;

    switch (number % 2)
    {
    case 0: even++;
    case 1:
    case -1: odd++;
    }
  }
  cout << endl;

  cout << "EvenOdd output by " << endl;
  cout << "Number of values in file: " << number << endl;
  cout << "Number of even values: " << even << endl;
  cout << "Sum of even values: " << sumEven << endl;
  cout << "Number of odd values: " << odd << endl;
  cout << "Sum of odd values: " << sumOdd << endl;

  system("PAUSE");
  return 0;
}

Code tags added. -Narue

Recommended Answers

All 4 Replies

Compare and contrast:

#include <iostream>

using namespace std;

int main()
{
  int even = 0, odd = 0;
  int input;

  cout<<"Enter a set of numbers: ";
  while ( cin>> input ) {
    if ( input % 2 == 0 )
      even += input;
    else
      odd += input;
  }

  cout<<"Even: "<< even <<'\n'
      <<"Odd: "<< odd <<endl;
}

I have to use a for loop and then get the integers from a file created...which I have as named input.txt.

>I have to use a for loop and then get the integers from a file
>created...which I have as named input.txt.
That's nice, but I wasn't doing your homework for you. I was giving you example code so that you can figure out what you're doing wrong.

I'm sorry I didn't mean to give you the intention that I wanted you to do my homework. I've just never done a counter-controlled for loop so I'm a bit confused. Again I apologize.

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.