Hey everyone,

I am new to C++ and I was wondering if anyone could help me start off or by guiding me to which method of steps I should take first in solving the following problem:

write a c++ program that with a loop structure that reads in 5 intergers one by one,
calculate the sum of the even values that are read and display the final result.

Any suggestion or comments in regards to the starting point of this problem would be much apprieciated.

Dani AI

Generated

Short, practical summary and fixes you can use immediately.

The task is simple: read five integers one at a time, keep a running total that starts at zero, test each input for evenness, add only even numbers to the running total, then print the total. Do that inside a single loop — you do not need five separate variables or precomputed remainders.

What went wrong in the posted attempt (so you can fix it):

  • The loop counter is never updated, so the loop never ends.
  • Using bool EvenNum = 1 and comparing remainders to that ends up selecting odd numbers (remainder 1) instead of evens (remainder 0).
  • Computing all remainders first and then trying to check them inside a loop makes the logic fragile. Add numbers to the sum as you read them.
  • Assigning the total inside the loop to the sum of all five inputs ignores the parity checks and defeats the purpose.

Beginner-friendly approach (no arrays required):

  1. Initialize a sum variable to 0.
  2. Run a loop exactly five times. Each iteration: prompt and read one integer, check whether the remainder after dividing by 2 is zero, and if so add it to sum.
  3. After the loop print sum.
    Also add minimal input validation: if extraction fails, clear the stream and re-prompt.

Notes on other replies: suggests the natural array/generalized version — good once you learn arrays. had the right idea but omitted the counter increment (infinite loop); ’s reply includes the increment and is correct; shows a do-while variant. Start with the single-variable loop, test with positive, zero and negative inputs, then try the array/generalized version as a follow-up.

Recommended Answers

All 7 Replies

Have you tried anything yet? We tend to expect at least a tiny amount of code that proves you've made an honest attempt.

Yeh I have attempted of the code, but I seem to can't get the code to work here is attempted code:

int main(){
   int count = 1, FirstNum = 0, SecondNum = 0, ThirdNum = 0, FourthNum = 0, FifthNum = 0;
   int FirstTotal, SecondTotal , ThirdTotal , FourthTotal, FifthTotal, SumTotal;
   bool EvenNum = 1;

    cout << "Please input a interger: ";
    cin >> FirstNum >> SecondNum >> ThirdNum >> FourthNum >> FifthNum;

    FirstTotal = FirstNum % 2;
    SecondTotal = SecondNum % 2;
    ThirdTotal = ThirdNum % 2;
    FourthTotal = FourthNum % 2;
    FifthTotal = FifthNum % 2;

 if (count >=1)
  {
    while (count <= 5)
      {
        if (FirstTotal == EvenNum || SecondTotal == EvenNum || ThirdTotal == EvenNum || FourthTotal == EvenNum || FifthTotal == EvenNum)
           {
             SumTotal = FirstNum + SecondNum + ThirdNum + FourthNum + FifthNum;
           }       
      }
  }
system("pause");
return 0;
}

try using an array instead of making 5 variable...

if you still dont get it then read on for the code...


int x[5],sum=0; // creates an array to store 5 integers

for(int i=0;i<5;i++) // loop to take 5 values and evaluate them
{
cin>>x;
if(x%2==0) // checks if the nubmer is even or not
{ sum=sum+x;
}
}
cout<<sum;

Hope this helps ;)

I haven't been shown using an array as yet. I have only been shown the following techniques, and they are:
1. If statement, nested statement (i.e else if) and briefly on the following loop (for, while and do) as well as a bit of increment i.e number++

Could some one please suggest a technique I could use? i.e. for or while loop or something.

Just a rearrangement and modification of your original code. Look and learn.

#include <iostream>
using namespace std;
int main()
{
    int count = 1, number ; 
    int Total = 0 ;
    while (count <= 5)
    {
        cout << "Please input  interger " << count << " : "; 
        cin >> number ;
        if ( number % 2 == 0 )// % is the Modulus, it gives the remainder after dividing by 2.
        {
              Total = Total + number ;
        }
    }
    cout << "Even Number Total is " << Total << endl ;
    system("pause");
    return 0;
}
#include <iostream>
using namespace std;
int main()
{
    const int MAXNUMS = 5;
    int count;
    int num, total;

cout << "\nThis program will ask you to enter " << MAXNUMS << " numbers.\n";
count = 1;
total = 0;
    while (count <= MAXNUMS)
    {
        cout << "Please input integer " << count << " : "; 
        cin >> num;
        if ( num % 2 == 0 )// % is the Modulus, it gives the remainder after dividing by 2.
        {
              total = total + num ;    
        }
    count++;
}
    cout << "Even Number Total is " << total << endl ;   
             
    system("pause");
    return 0;
}

this should help you out, only calculates the even numbers out of 5 inputs, displaying the total of the even numbers at the end.

you can thank me later :mrgreen:

#include <iostream>
using namespace std;

int main()
{
	
	int num;
	cout << "enter a number: ";
	cin >> num;
	int count = 0;
	int sum = 0;
	do
	{
		if(num % 2 == 0)
		sum += num;
		count++;
		cout << "enter a number again: ";
		cin >> num;
	}while(count < 5);
	cout << sum << endl;
	return 0;
}
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.