// gettingEvenNumbers.cpp

// Purpose of this functions is to read in the numbers 1 - 20
// and print out only the even numbers using a loop of some kind.

#include <iostream>
#include <string>

int main() {
    
    // Numbers being used.
    int x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};

    // Header telling the user what the program does.
    std::cout << "This program takes the numbers 1 - 20 in and spits out only the even numbers.\n";
    
    // for loop to take each number in array and determine if it is even by dividing it by 2.
    for (int i = 0; i <= 20; ++i) {
        x[i] / 2;
        
        // If the result of division is any one of these even numbers, print that it is even.
        if ( i = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) {
             std::cout << i << "These are the even numbers";
             
             // If no even numbers found, type an error.
             } else {
                    std::cout << "There are no even numbers!\n";
                    return 1; // Return value of 1 indicating an error.
                    }
        }
        
    // Prompting user to exit program by pressing Enter or Return.
    std::cout << "Press Enter or Return to continue.\n";
    std::cin.get();
    return 0;
    
}

DO NOT RUN THIS PROGRAM - It's a neverending loop.
Hi everyone I've been getting into programming as a hobby and I've just finished reading my first C++ Book (C++ Programming - Visual Quickstart Guide). I am attempting some exercises that I found at:

http://www.devmaster.net/wiki/Game_programming_exercises

Obviously not a lot of the stuff I read stuck in my head (I plan on re-reading). I would like to know how I could get the program to run accordingly. I am trying print out the even numbers between 1 and 20 and just can't seem to get it right with the method I'm using.

What am I doing wrong? What is the better solution to writing this program? I do not expect a clear cut "Here's the code you want" answer, I only ask for the proper way in which I should approach this problem.
Thanks

An easier approach to testing if the number is even is to use the modulus operator %

int num = 10;

if(num % 2 == 0)
{
       cout << "even";
} 
else
{
      cout << "odd";
}

You should also look up some information on if statements because
if ( i = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) is wrong in a few ways

i = 1 is an assignment and not a test. You should use == instead.
Also you can't check for multiple conditions that way so check that out as well.

Member Avatar for jencas

...and what do you think what the statement

x[i] / 2;

will do???

I was thinking that x / 2 would take each number in the array and divide it by two. Then, if the resulting number from the division was equal to 1 - 10 it would be an even number because 2/2 =1, 4/2 = 2 etc. It seems a bit weird now that you mention it.

Don't worry I'll be going through the whole text another time, and doing an online tutorial for C++ that I've found. It all comes with time I guess, learning the proper approach to a problem that is.

Could I still use an array of numbers 1-20 for your way?


int num[] = {1....20}

Because I want it to check for all the numbers between 1 and 20.

// gettingEvenNumbers.cpp

// Purpose of this functions is to read in the numbers 1 - 20
// and print out which numbers are even and which are odd.

#include <iostream>

int main() {
    
    // Numbers being used.
    int x = 0;

    // Header telling the user what the program does.
    std::cout << "This program takes the numbers 1 - 20 in and tells the user which numbers are even and odd numbers.\n\n";
    
    // for loop to take each number in array and determine if it is even by dividing it by 2.
    for (int i = 0; i <= 20; ++i) {
        x = i;
        
        // If the result of division is any one of these even numbers, print that it is even.
        if ( i % 2 == 0) {
             std::cout << i << " This number is even.\n";
             
             // If no even numbers found, type an error.
             } else if ( i % 2 > 0) {
                    std::cout << i << " This number is odd.\n";
                    }
        }
        
    // Prompting user to exit program by pressing Enter or Return.
    std::cout << "\nPress Enter or Return to continue.\n";
    std::cin.get();
    return 0;
    
}

Problem solved. Thought of this while in the kitchen lol. Thanks for all the help so fast!

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.