// 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