for loop

Hello I am horrible in c++ and need help
my class is suppose to create a program that prints only even numbers between 2 integers entered from the keyboard
my prog is a train wreck please help




#include <iostream>

using namespace std;

int print_even (int a, int b)
{for (a=0; a<=b; a++);
{if (a%2!=0)
a= a+1;
}}


int main()
{int a, b;

cout<< "Enter Your Beginning and Ending numbers...."<< endl;
cin>> a, b;

cout <<print_even(a, b)<<" "<<endl;

system("PAUSE");
return 0;
}
Hello there try this

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>
using namespace std;

void print_even (int a, int b)
{
	
   for (int i = a; i<=b; i++)
   {
	if(i % 2 == 0)
     std::cout<< i <<std::endl;
       
   }
}


int main()
{
	int a = 0, b = 0;

cout<< "Enter Your Beginning and Ending numbers...."<< endl;
std::cout<<"First number: ";
cin>> a;
std::cout<<std::endl;
std::cout<<"Second number";
cin>> b;

	print_even(a, b);

system("PAUSE");
return 0;
}


the first misstake you did was putting ; infront of the for loop ; means that the statement ends,
do not try to initialize 2 variables in the same cin>>, the function is called print_even then print all your numbers in there and set the return type to void so it wont return anything, you assigned a in the loop the value 0 so it will start from 0 and up everytime.

edit: the modulo operator gives the rest of a division if the rest is 0 it's even so check for == 0
hope it helped :)
Last edited on
Thanks it DID!!!!
@Tamao

Please don't do their homework for them... it only serves to make them terrible programmers.
Topic archived. No new replies allowed.