[output error] Does cout operator works with && (AND) operator in this format?

Hi,

I tried to use && operator in order to execute a function() and cout statement.
In this way, (unit(num/100) && (cout << "HUNDRED\t"))
But, I couldn't get the output "HUNDRED" printed on the screen with the output of unit() function invoked.
Can you please tell if I have written this in a wrong way or this kind of expected output can be seen via some other method ?

Please check the line 22.

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
  #include <iostream>
//#include <math.h>
using namespace std;

char one[10][10] = {"ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"};
char two[10][10] = {"ELVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINTEEN" };
char tendigits[10] [10] = {"TEN", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINTY"};

int unit (int num)
{
	num ? (cout << one[num-1]):(cout << "ZERO");
	return 0;
}
int tens (int num)
{
	(num>10 && num<20) ? (cout << two[num-11]) : ((cout << tendigits [num/10-1]) && ((num%10) ? (unit(num%10)):1));
	return 0;
}

int hundred (int num)
{
	(num/100)? (unit(num/100) && (cout << "HUNDRED\t")):1;
	(num % 100)? (tens(num%100)):1;
}

int main ()
{
	//cout << one[0];
	//unit(3);
	//tens(25);
	hundred(201);
 //	return 0;	
}
typically you use logical operator to evaluate boolean conditions. This kind of thing:
cout << "HUNDRED\t"

Does not give you back a one or a zero.
I just found out, the line 12 is return 0;
So, that's why the cout operator didn't work with && (AND) operator.
In order to get the output I can use either use || (OR) operator or change the return value to 1.



Thanks for the help, @mutexe
Is there any better approach than mine? Since, I want to adhere to the coding standards. If this approach is wrong, pls correct me.
Do not overuse ternary operator.
Write simple linear code:

1
2
3
4
5
6
7
8
9
int hundred (int num)
{
    if (num/100) {
        unit(num/100);
        std::cout << "HUNDRED\t";
    }
    if(num % 100)
        tens(num%100);
}
@MiiNiPaa Thanks for the suggestion, will keep that in mind.
Topic archived. No new replies allowed.