Hi all,

I have a function that calculated two int values. I call the function only by using a string and calculated those two int values. I want to return that two int values from the function when I call it with a string.

Look at the following function,

bool CharWordCount(string rtf, int& word_count, int& char_count)
{
// Body of the function
// do the processing and find the number of chars and
// number of words of the string
}

One of my friend say i can do it as above. using the bool type return value. Can you guys give me a help how to do that.

Recommended Answers

All 10 Replies

the "&" means you get the parameter by name. for example:

...
int wcount, ccount;
CharWordCount("good luck", wcount, ccount);
//if the function is correct, wcount and ccount, now contain the require values.

Actually I want to call the function only using a string.

what do you mean?
another way you can do this fuction, is by returning an array -> int*;
the first place would be the word count, and the second would be the char count.
like this:

int* CharWordCount(string str)
{
     int ans[2];
     //....
     return ans;
}

what do you mean?

This is what I want.

I have a function that calculate two int values. User call this function with a string, and he gets two int values after doing all the processing inside the function. That mean I have to return two int values from the function. How can I do that?

You can't.

> That mean I have to return two int values from the function. How can I do that?
So how does your first post not achieve that?

Or you could contrive an answer with

struct twoint { int a, int b };
twoint func ( ) {
  twoint ans = { 1, 2 };
  return ans;
}

well both the methods are good....first with the integer array suggested by gabs and second with structure of two integers suggested by Slaem...

but i think it is better to use the method suggested by your friend...sending two integers by reference... i guess u dont know about the concept of sending the variables by reference...do you???

User call this function with a string,


user doesn't
call any function...it is the programmer who decides how to call the function....if you want to call the function by the string entered by the user you can still do that by sending it to the function with the two integers (pass by reference)...user will have no information about them....and how the function was called...

> That mean I have to return two int values from the function. How can I do that?
So how does your first post not achieve that?

Or you could contrive an answer with

struct twoint { int a, int b };
twoint func ( ) {
  twoint ans = { 1, 2 };
  return ans;
}

Yes, I've try the struct. Seems that is the best option to me. Use of bool return value seems useless.

well both the methods are good....first with the integer array suggested by gabs and second with structure of two integers suggested by Slaem...

Seems to me struct is good.

but i think it is better to use the method suggested by your friend...sending two integers by reference... i guess u dont know about the concept of sending the variables by reference...do you???

Handling variables either by reference or by value is not a question to me, may be I don't have better idea about them like people here.


user doesn't
call any function...it is the programmer who decides how to call the function....if you want to call the function by the string entered by the user you can still do that by sending it to the function with the two integers (pass by reference)...user will have no information about them....and how the function was called...

Ok, I got it. Actually user mean anyone who read my code. I want to hide information as much as possible.

sorry to bother you...
but i didn't get your point..

Actually user mean anyone who read my code. I want to hide information as much as possible.

how will you hide the information by using structure instead of passing it by reference...what's the advantage??...

the user(the person who will see your code) can see struct as well as parameters passed by reference...

confused...

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.