odd or even integers

I need to write a program that prints out all the odd or even integers between any two integers (inclusive) entered by the user. The user should be able to select whether odd or even integers are to be printed. Use a while loop to force correct entry for the odd/even choice (use type char for choice).

The program should contain two ways for printing integers, and automatically produce two outputs (which obviously must be identical); these two ways are in seperate functions.

a. The first way your code should contain the odd/even conditional statements before a while loop, and use a loop increment of two.

b. In the second way the odd/even conditional statement(s) should be inside the while loop, which should use a loop increment of one.
Ok. I don't see a question.
I have started with this, I just need a nudge in the right direction please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <iostream>
#include <conio.h>
using namespace std;

void printInteger( char );
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{   
   int myInteger1;
   int myInteger2;
   
   cout << "Enter any 2 integers: " << endl << endl;
   cin >> myInteger1;
   
   cout << "... and the next one: " << endl << endl;
   cin >> myInteger2;
    
   _getch();
   return 0;
}

Try first writing a loop that prints out all of the numbers between the two the user entered.
I've got this thus far:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

//-----------------------------------------------------------------------------
//Programmer: Charles Syms
//Date: 2nd August 2011
//Folder: H:\My Documents\Programming Assignments\Assignment 3
//Description: Question 9 of Assignment 3

#include <iostream>
#include <conio.h>
using namespace std;

void printInt1( char choice );
void printInt2( char  );
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{   
   int myInteger1;
   int myInteger2;
   
   cout << "Enter any 2 integers: " << endl << endl;
   cin >> myInteger1;
   
   cout << endl << "... and the next one: " << endl << endl;
   cin >> myInteger2;
   
   printInt1( myInteger1 );
    
   _getch();
   return 0;
}
//-----------------------------------------------------------------------------
void printInt1( char choice, int int1, int int2 )
{
   for(; int1 <= int2; int1++)
   {
      if(choice == 'o' || choice == 'O')
      {
      if(int1 % 2 != 0)
      cout << int1 << endl;
      }
       
      else if(choice == 'e' || choice == 'E')
      {
      if(int1 % 2 == 0)
      cout << int1 << endl;
      }
   }
}

Your call to printInt1

printInt1( myInteger1 );

does not match your implementation

void printInt1( char choice, int int1, int int2 )

You are trying to pass in only one integer. It expects a char and two ints.
I don't know if you have any more information than that, but I don't completely understand what you mean
You made a fcuntion called printInt1.

You made it so that to call it you have to pass it a char, and two int values.

This means that when you call it, you must pass it a char, and two int values.

For example,

1
2
3
4
char a = 'a';
int b = 7;
int c = 8;
printInt1(a, b, c);


You cannot pass it just one int, like this:
printInt1( myInteger1 );

Last edited on
Your declaration of printInt1 is :

void printInt1( char choice );

While your implementation is :

void printInt1( char choice, int int1, int int2 ){...}

Then in your code you call

printInt1( myInteger1 );

Which has no body at all. Does this even compile?
hey guys,
had a real good stab at this last night, ended up getting so tired you can see where it got me...

I've gotten here thus far:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

//-----------------------------------------------------------------------------
//Programmer: Charles Syms
//Date: 2nd August 2011
//Folder: H:\My Documents\Programming Assignments\Assignment 3
//Description: Question 9 of Assignment 3

#include <iostream>
#include <conio.h>
using namespace std;

void printInt1( char choice, int int1, int int2 );
void printInt2( char choice, int int1, int int2 );
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{   
   int myInteger1;
   int myInteger2;
   char choice;
   
   cout << "Enter any 2 integers: " << endl << endl;
   cin >> myInteger1;
   
   cout << endl << "... and the next one: " << endl << endl;
   cin >> myInteger2;
   
   do 
   {
   cout << endl << "Print odd (o) or even (e) numbers?: " << endl << endl;
   cin >> choice;
   }
   while ( ! ( choice == 'o' || choice == 'O' || choice == 'e' || choice == 'E'));
   
   system("cls");
   
   printInt2( choice, myInteger1, myInteger2 );

   getch();
   return 0;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void printInt2( char choice, int int1, int int2 )
{
   cout << "Your " << choice << " numbers are displayed:" << endl << endl;
   for(; int1 <= int2; int1++)
   {
      if(choice == 'o' || choice == 'O')
      {
      if(int1 % 2 != 0)
      cout << int1 << endl;
      }
       
      else if(choice == 'e' || choice == 'E')
      {
      if(int1 % 2 == 0)
      cout << int1 << endl;
      }
   }
}


I have one mroe part to complete and am struggling slightly with how to make it work - I need to put the odd/even statements before a while loop and use a loop increment of two. Any help would be so so grateful!
and use a loop increment of two


for(; int1 <= int2; int1++)
becomes

for(; int1 <= int2; int1=int1 + 2)
or, for the lazy typer...

for(; int1 <= int2; int1 += 2)
Topic archived. No new replies allowed.