954,164 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Help with evaluation

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

atman
Junior Poster in Training
50 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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 ?

Ancient Dragon
Retired & Loving It
Team Colleague
30,043 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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

atman
Junior Poster in Training
50 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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

kenji
Junior Poster
145 posts since May 2008
Reputation Points: 11
Solved Threads: 11
 

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(' ');
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

> To the OP: You say that this function - cio_putstr() returns a void . If this is so, then justwhy 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.

amrith92
Junior Poster
188 posts since Jul 2008
Reputation Points: 130
Solved Threads: 23
 

>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 ;)...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 
>[/B] 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 :)

Ancient Dragon
Retired & Loving It
Team Colleague
30,043 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

a switch statement??

jloundy
Newbie Poster
8 posts since May 2009
Reputation Points: 5
Solved Threads: 0
 
a switch statement??


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

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You