Thread: What's wrong with my turnary ? statement?

  1. #1
    Registered User
    Join Date
    Mar 2006

    Thumbs up What's wrong with my turnary ? statement?

    Have a look at my code below.

    #include <iostream>
    #include <cstdlib>
    using namespace::std;
    struct node {
    	int data;
    	struct node* left;
    	struct node* right;
    void printInOrder(node* myNode);
    int countNodes(node* myNode);
    // a quick function to simulate a tree structure.
    struct node* getTree() {
    		// allocate space for six nodes.
    	struct node* root = new (struct node);
    	struct node* two = new (struct node);
    	struct node* three = new (struct node);
    	struct node* four = new (struct node);
    	struct node* five = new (struct node);
    	struct node* six = new (struct node);
    	root->data  = 1;
    		two->data   = 2;
    		three->data = 3;
    		four->data  = 4;
    		five->data  = 5;
    		six->data   = 6;
    	root->left = two;
    		root->right = three;
    		two->left = four;
    		two->right = five;
    		three->right = six;
    		return root;
    int main() {
    	node *tree = getTree();
    	cout << "This tree has " << countNodes(tree) << " nodes." << endl;
    void printInOrder(node* myNode){
    	if(myNode != 0){
    		cout << myNode->data << endl;
    int countNodes(node* myNode){
    	/*if(myNode != 0){
    		return (1 + countNodes(myNode->left) + countNodes(myNode->right));	
    	} else
    		return 0;*/
    	//this is the statement in question
    	myNode != 0 ? return(1 + countNode(myNode->left) + countNode(myNode->right)) : return 0;
    I know the syntax is correct because I checked it against another program I have that uses the operator and it works but this doesn't for some reason. My compiler tells me that it expects a primary expression before the return statement.
    Last edited by xmltorrent; 03-21-2006 at 01:27 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    The return should go at the head of the statement, followed by the comparison and then the value to return if true and the value to return if false. It is not like an if statement where you execute a command if the boolean expression is true. Instead the ternary expression only evaluates to a value depending on the boolean comparison. So you put the return in front so that you return that value.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Thanks for the help.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Since ?: is just and if/else for expressions, you may as well save some confusion by writing
    if ( myNode != 0 ) {
      return(1 + countNode(myNode->left) + countNode(myNode->right));
    } else {
      return 0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with my if statement
    By joker_tony in forum C Programming
    Replies: 6
    Last Post: 05-10-2008, 02:11 AM
  2. what is wrong with my if statement
    By joker_tony in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 12:26 AM
  3. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 05:06 AM
  4. What's wrong with my success statement??
    By cool_dude07 in forum C Programming
    Replies: 7
    Last Post: 07-21-2007, 11:22 PM
  5. Something wrong with this if statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-30-2002, 05:19 PM