Need Help with "for loop"


I need to make a "for loop" that adds up only the odd numbers entered by the user. The even numbers would need to be ignored. The program would let the user only enter a certain number of entries, let’s say 4 entries. They would be prompted to enter a number and then enter one in. This would continue for each number until they reach 4 numbers entered. After the 4th is entered they would get the total for only the odd numbers that were entered. I know this seems simple but for some reason I can’t figure this out. Any help or suggestions is appreciated – Thanks.
What do you have so far?

If the trouble is determining whether a number is odd or even, the traditional answer is to use the % operator, which by default gives the remainder you'd get from doing a division. If a division by 2 would give a remainder of 0, it's even. If a division by 2 would give a remainder of 1, it's odd. Makes sense?

If the issue is with for loops, see if this helps:
http://www.cplusplus.com/doc/tutorial/control/#for

-Albatross
Below is an example Pseudocode for your problem

- Identify the variables
       - Number the user needs to input
       - The number of Entries
       - The running total
- Ask the user to type the number of entries to add the numbers
- Create a for loop that will not loop more than the number of entries
- Inside the for loop
       - Ask the user to enter an odd number
       - Use the if statement to validate that the number is odd or even
       - If the number is odd, then add it to the running total
       - If the number is even, then let the user know the number is not odd and post-decrement the count of the loop
- Close the loop and notify the user of the total of the odd numbers addition


Hope it gives you an idea. Good luck.
Last edited on
- If the number is even, then let the user know the number is not odd and post-decrement the count of the loop


I dont think decrementing is necessary. The OP says a certain number of numbers will be entered and the odds will be added while the evens will be ignored.

The loop will increment no matter the number is odd or even.

You brought a good point. I use the post-decrement to let the user to enter another value that is an odd number and does not count towards the number of entries. So it validates the input of the user. Nevertheless, I over-did it.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

int main()
{

    int number{ 0 };
    unsigned int numberOfEntries{ 0 };
    int total{ 0 };

    std::cout << "Type the number of entries to add the odd numbers: ";
    std::cin >> numberOfEntries;

    for (unsigned int count = 0; count < numberOfEntries; count++)
    {
	   std::cout << "Enter the odd number [" << count + 1 << "] to add: ";
	   std::cin >> number;

	   if (number % 2 != 0)
		  total += number;

	   else if (number % 2 == 0)
	   {
		  std::cout << "The number " << number << " is even.\n";
		  count--;
	   }
	   
    }

    std::cout << "The total is [" << total << "]\n";
    
    return 0;
}


Last edited on
Thank you for all the helpful comments. I put what I have so far below. I got it to add up the separate input entries and total it after the 5th entry. Now I just need it to only add the odd numbers entered and ignore the even. Resulting in a total/sum of the odd numbers entered. I know %2 != 0 should look for the odd entries but I can’t seem to get it all to work?


#include <iostream>
using namespace std;


int main() {

int sum = 0;
int num[4];

for (int counter = 0; counter <= 4; counter++)
{
cout << "Enter a Number: ";
cin >> num[counter];
sum = sum + num[counter];
}

cout << "sum: " << sum << endl;

system("pause");
return 0;
}
Read chicofeo's answer to see how to test if a number is even.
@ Drewstar13

Below is an example of how to check the number for even or odd. This is to give you an idea so you can finish your code.

1
2
3
4
5
6
7
// To check if the number is even
if (num[counter] % 2 == 0)
   cout << "The number " << num[counter] << " is even\n";

// To check if the number is odd
else if (num[counter] % 2 != 0)
   cout << "The number " << num[counter] << " is odd\n";
Thanks for the reply chicofeo. I did try adding that code but it still just adds up every number I enter in. I need to have it just add up the odd numbers. Below
is an example of entering in the numbers and how it is working now. As you can see it is adding up everything still.

Enter a Number: 10
Enter a Number: 33
Enter a Number: 17
Enter a Number: 41
Enter a Number: 66
sum: 167

Your for loop looks like this:
for (int counter = 0; counter <= 4; counter++)

You will have a run-time error by accessing variables outside of the allocated memory.

However, it should be like this:
for (int counter = 0; counter < 4; counter++)

Below is some little adjustments to give you some guidance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;


int main() {

    int sum = 0;
    int num[4];

    for (int counter = 0; counter < 4; counter++)
    {
	   cout << "Enter a Number: ";
	   cin >> num[counter];
	   if (num[counter] % 2 != 0)
		  sum = sum + num[counter];
    }

    cout << "sum: " << sum << endl;

    system("pause");
    return 0;
}


The output:
Enter a Number: 10 // It does not add this even number.
Enter a Number: 33
Enter a Number: 17
Enter a Number: 41
sum : 91
Last edited on
Thank you. I think part of my problem is I am over thinking it. I can see I am making things more complicated than they really are:)
Okay, I got everything running smoothly and needed to add one last thing. I thought it would be easy but its not working out that way. I need to use the continue statement for this. I have tried many variations but the program is still counting 7 when adding the sum.

#include <iostream>
using namespace std;


int main() {

int sum = 0;
int num[5];

for (int counter = 0; counter < 5; counter++){
if (counter == 7) {
continue;
}

cout << "Enter a Number: ";
cin >> num[counter];
if (num[counter] % 2 != 0)
sum = sum + num[counter];
}

cout << "Sum of Odd Numbers is: " << sum << endl;

system("pause");
return 0;
}
Last edited on
Please use code tags when posting. Highlight your code and click the <> button to the right of the edit window.

Where you have if (counter == 7), the condition will never be true. Counter goes from 0 to 4 as controlled by the loop right above it.

Think of continue as saying "okay, I don't need to do anymore in this iteration of the loop." You want to use it if the number is even.
1
2
3
4
5
6
7
for (int counter = 0; counter < 5; counter++) {
    ....
    if (num[counter] % 2 == 0) {
        continue;
    }
    sum = sum + num[counter];
}


By the way, line 6 above can be shortened to sum += num[counter]; Also, notice that after you read a number and potentially add it to sum, you don't use it again. That means you don't actually need to keep it around. So instead of having an array of numbers, you could have a single int num and each time through the loop, overwrite it with the new number from the user.
What needs to happen is if the number 7 (which happens to be an odd number) is inputted (from keyboard). The program would then ignore it and not add it to the sum of all odd numbers entered. The continue statement also needs to be used.
Topic archived. No new replies allowed.