Non existing output

This simple code should determine if a two digit number contain odd digits only mixed numbers or even numbers only.
I couldn`t test it because the output doesn`t even an option on the code,to the value 19 he prints the word "only" which is just a part of a sentence.
What I did wrong and how thats happened?

Thanks

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

  #include<iostream>
using namespace std;
int main()
{

	int num;//The number of user unput.

	int a;//Temp first digit
	int b;//Temp middle digit


	cout << "enter a number:";
	cin >> num;

	a = num / 10;
	b = num % 10;

	if (num > 100 || num == 0 || num < 0)
		cout << "ERROR";
	else
	{
	
		if ((a % 2 ==0) && (b % 2==0))
			cout << "even digits only \n" + (a*b);

		else if ((a % 2) && (b % 2))
			cout << "odd digits only \n" + (a + b);

		else
			cout << "mixed numbers" << endl;
	}
	return 0;
}
Last edited on
This is the problem:
1
2
3
4
			cout << "even digits only \n" + (a*b); // Use << instead

		else if ((a % 2) && (b % 2))
			cout << "odd digits only \n" + (a + b); // Use << instead 
You stumbled upon an interesting behavior.
String literals are actually just pointers at run time. "even digits only \n" + (a*b) actually means some_pointer + (a*b) which is, itself, some pointer to something. Most of the time, this pointer will end up in the memory region around that particular string. Often in the middle of a string.

To print more than one thing, you need to do
 
std::cout <<"even digits only \n" <<(a*b) <<std::endl;
Thanks to both of you !
Topic archived. No new replies allowed.