Conditional operator ? :)

This is a discussion on Conditional operator ? :) within the C++ Programming forums, part of the General Programming Boards category; Why doesn't the following line of code compile? number % primes[j] == 0 ? return 0 : , ; If ...

  1. #1
    ER
    Guest

    Unhappy Conditional operator ? :)

    Why doesn't the following line of code compile?

    number % primes[j] == 0 ? return 0 : , ;

    If the number % the prime is 0, I want to return 0 to main, else I want it to continue with the function. Here's the error it gives me:

    C:\My Documents\Visual Studio Projects\FunLoops\main.cpp(32) : error C2059: syntax error : 'return'

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    to do that you would have to do something like this....

    bool x= (number % primes[j] == 0 ) ? 1:0;
    if(x) return 0;
    // code here for what to do if not returning yet
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    ER
    Guest
    So I can't use the conditional operator?

    The reason I'm trying to use it is for efficiency; I'm in a contest to generate prime numbers

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well how about ....
    Code:
    bool IsPrime(int num)
    if(num==0||num==1) return true;
    int divisor;
    for(divisor=num/2;num%divisor!=0;divisor--);
    return (divisor==1);
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    22

    Thats not the :conditional: operator

    What your refering to is the tertiary operator (compact if statement)

    if (a < b)
    {
    return true;
    }
    else
    return false;

    is the equivelent of
    return (a < b) ? true : false;

    who ever gave you the idea of it being the "conditional operator?"

    as to why it doesnt compile...
    thats simple, your syntax is wrong.

    (i assume this is what you want)
    return (number % primes[j] == 0) ? 0 : 1;

    oh, and in terms of efficiency, both take just as many clock cycles to compute, its just less typing to use the tertiary operator, and programmers always like having to type less :-)


    -Vulcan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conditional Operator
    By arjunajay in forum C Programming
    Replies: 8
    Last Post: 07-10-2008, 09:17 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Help understanding conditional operator
    By Sereby in forum C Programming
    Replies: 7
    Last Post: 08-09-2004, 01:24 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM

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