Hello.,
I'm very new in C++, and I have a task to solve.
I have a function:

void cio_display(const char *str, int row, int col, int len){
  int i;
  cio_move(row, col);
  if(len<=0) {
    cio_putstr(str);
  }
  else{
    for(i=0;i<len && str[i];i++)
      cio_putch(str[i]);
   
    for(;i<len;i++)
      cio_putch(' ');
  }

here iss what i have done:

void cio_display(const char *str, int row, int col, int len){
	
	int i;
	cio_move(row, col);
	for( i = 0; ((len <= 0) && cio_putstr(str)), (i < len && str[i]); i++){
	cio_putch(str[i]);
	}

	for(;i<len;i++)
	cio_putch(' ');
}

The problem id the function cio_putstr() returns void, thats why lazy evaluation is not possible. Any other thoughts would be greatly appreciated!

Thank you

Recommended Answers

All 9 Replies

I have no idea what you are talking about. What is "lazy evaluation" ? And why does it matter whether cio_putstr() returns void or something else ?

sorry was in a rush and forgot to ask., The question is how to rewrite the function w/o using if statement.
Any help would be appreciated

Um..."while" and "for" come to mind, just need to use a little creativity. Its not that hard.

What's a problem?

for (i = 0;
     len <= 0 && str[i] || str[i] && i < len;
     i++) {
    cio_putch(str[i]);
}
while (i++ < len)
    cio_putch(' ');

> To the OP: You say that this function - cio_putstr() returns a void . If this is so, then just why is it in your for loop as a test expression? And could you explain what it - cio_putstr() - is supposed to do?

> To Ancient Dragon: I read about "lazy evaluation" in some book, but I don't remember which one :( ... Anyway, here is wiki on it.

>just why is it in your for loop as a test expression?
That's a good question ;)
However there is an absolutely senseless condition with comma operator in that snippet, so it does not matter why ;)...

To Ancient Dragon: I read about "lazy evaluation" in some book, but I don't remember which one :( ... Anyway, here is wiki on it.

OMG I've been doing that for 50 years and always thought it was called procrastination. Now I know I was just plain lazy :)

a switch statement??

a switch statement??

What's a cool tip!
Which else C++ control statements are you familiar with?
;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.