Jump to content
  • Advertisement
Sign in to follow this  
xyuri

C++ shorthand "if then" issue

This topic is 4484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

why does the following not work ?
c==0?continue:break;
There is nothing behind of before it which is affecting the compilation. This is the error:
Quote:
xyuri@bsdmachine$ make g++ -c a1mstrncmp.cpp g++ -c mstrncmp.cpp mstrncmp.cpp: In function `int mstrncmp(const char*, const char*, size_t)': mstrncmp.cpp:29: error: expected primary-expression before "continue" mstrncmp.cpp:29: error: expected `:' before "continue" mstrncmp.cpp:29: error: expected primary-expression before "continue" mstrncmp.cpp:29: error: expected `;' before "continue" *** Error code 1 Stop in /usr/home/xyuri/code/se93a1.
is it not possible to use break / continue type statements in these things ? Edit: ok, for the sake of supplying sufficient information here is the whole code ... it is a replacement function for the built in one (strncmp) for an assignment ... this doesnt help me with me assignment because as you can see it is already done and working, i'm just tidying up syntax now like i have done for the other 5 functions we needed to write.
//|
//| mstrlen.cpp
//|
//| Author.: Michael Dawson
//| Date...: 10/03/2006
//|
//| Purpose: .
//|

#include "mstr.h"

//
// Purpose: simulate the effect of strncmp
//          function in standard library.
// Pre....: s1 and s2 must be a properly terminated c string.
// Post...: Returned value indicates difference.
//
int mstrncmp(const char *s1, const char *s2, size_t n)
{
	int c;

	for (int i=0 ; i<n ; i++) {
		if (*s1=='\0' || *s2=='\0') break;
		c = *s1++ - *s2++;
		//if (c == 0) { continue; } else { break; }
		c==0?continue:break;
	}

	return c;
}

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by DrEvil
What is 'c' ?

Check above, i added the whole file :) thank you for your input.

Share this post


Link to post
Share on other sites
The only thing i can suggest is putting then c==0 inside brackets, ie (c==0).

Dave

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
The only thing i can suggest is putting then c==0 inside brackets, ie (c==0).

Dave
That didnt help any ;)

Share this post


Link to post
Share on other sites
It appears keywords are not compatible as the operands in the ternary operator. Interesting.

I'd suggest just sticking with the if/then. It's easier to read anyways in most cases.

Share this post


Link to post
Share on other sites
The operator ?: is an expression, it's not a statement. You can use it to select between one of its subexpressions. You cannot use it to select between different statements.

Share this post


Link to post
Share on other sites
What's wrong with 'if (c) break;'? You write less, and it does the same thing...

[EDIT]
What nmi said is also true. What you're doing is the same as trying to assign a value to nothing, so obviously it wont work.

Share this post


Link to post
Share on other sites
Untrue, ?: is not limited to values. You can put statements in there just fine. In this case it appears that for some reason continue and break don't want to work.

Edit: some examples

MyFunc* are void return value functions

bool b = true, c = false;
b ? MyFunc1() : MyFunc2();

// Sometimes people nest them as well, in loop controls and such. It's pretty hideous but it works.
b ? c ? MyFunc1() : MyFunc2() : MyFunc3();

Share this post


Link to post
Share on other sites
Quote:
Original post by nmi
The operator ?: is an expression, it's not a statement. You can use it to select between one of its subexpressions. You cannot use it to select between different statements.


Quoted for emphasis.

The conditional operator is not a "shorthand". It serves a significantly different purpose from if statements.

Quote:
Original post by DrEvil
Untrue, ?: is not limited to values. You can put statements in there just fine. In this case it appears that for some reason continue and break don't want to work.


You can put expressions, not statements - a function call is an expression. continue and break simply are not appropriate here, and neither would blocks, while loops, if statements, goto nor any other kind of statement.

b ? MyFunc1() : MyFunc2() is an expression.
b ? MyFunc1() : MyFunc2(); is a statement.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!