2 Replies - 1487 Views - Last Post: 25 March 2008 - 03:21 PM Rate Topic: -----

#1 royjm248  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 18-January 08

multiple return values

Post icon  Posted 23 March 2008 - 08:30 PM

I have the following program that is supposed to compare two strings and return the values that indicate if the first is larger than the second, or the second is larger than the first, ect....that much I have done, i think. The final part is to modify the functions to take a third argument that will return the length of each string. I can get it to return one value or the other, but I need it to return both values. So far I have found no references to this problem. I think it is a simple matter, but I am getting frustrated, and this is making it hard to see the problem for what it is....simple. Any help would be greatly appreciated!


// Lab 1: stringCompare.cpp
#include <iostream> 
using std::cin; 
using std::cout; 
using std::endl; 


/* Write a function prototype for stringCompare1 */
int stringCompare1 (char s1[], char s2[], const size_t, const size_t);
/* Write a function prototype for stringCompare2 */
int stringCompare2 (char *, char *, char *, char *);
int main()
{
   char string1[ 100 ], string2[ 100 ];
   int str1 = 0;
   int str2 = 0;

   cout << "Enter two strings: ";
   cin >> string1 >> string2;


   cout << "The value returned from stringCompare1(\"" << string1
	  << "\", \"" << string2 << "\") is "
	  << stringCompare1(string1, string2, strlen(string1), strlen(string2))/* Write a function call for stringCompare1 */ 
	  << "\nThe value returned from stringCompare2(\"" << string1
	  << "\", \"" << string2 << "\") is "
	  << stringCompare2(string1, string2, string1, string2)/* Write a function call for stringCompare2 */ << '\n';

	system("pause");
   return 0;
} // end main

/* Write a function header for stringCompare1 */
int stringCompare1(char s1[],char s2[], const size_t, const size_t)
{
   int index;
	
   // array subscript notation
   /* Write a for loop that iterates through both strings
	  using array subscript notation, until index contains the
	  first index where the strings do not match or the index
	  of the null character */

	for (index = 0; s1[index] && s2[index] != '\0'; index++)
; // empty body

	//if (strcmp(s1, s2) == true)
	//	return -1;
	
	if ( s1[ index ] == '\0' && s2[ index ] == '\0' )
		return 0;

   /* Write code to return -1 or 1 based on which
	  of s1[ index ] and s2[ index ] is greater */

   if (s1[index] < s2[index])
	   return 1;
   else 
	   return -1;
 
} // end function stringCompare1

/* Write a function header for stringCompare2 */
int stringCompare2 (char *s1, char *s2, char *st1, char *st2)
{
   // pointer notation
   /* Write a for loop that iterates through both strings
	  using pointer notation, until both pointers point to
	  the first character where the strings do not match
	  or the index of the null character */
	 ; // empty statement
	
	  for (;*s1 && *s2 != '\0'; *s1++, *s2++ )
	; //empty body
		if ( *s1 == '\0' && *s2 == '\0' )
		return 0 <<	strlen(st1);

	//	int strcmp(const char *s1, const char *s2);
		if (*s1 < *s2)
			return 1;
		else
			return -1;
   /* Write code to return -1 or 1 based on which
	  of *s1 and *s2 is greater */

} // end function stringCompare2





Is This A Good Question/Topic? 0
  • +

Replies To: multiple return values

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: multiple return values

Posted 23 March 2008 - 09:35 PM

Turns out you can't return more than a single object.

But there are a number of ways to solve your problem.

#1 you can return a structure or object that encapsulates both values.
-- lets face it, this is not a great solution. If for no other reason than you have a structure that is just used to return values.

#2 you can pass in a pointer to a value. This way you can manipulate the value of a variable in the function and see the changes in the calling function. This is referred to as passing "by reference". -- This really isn't returning a value though.

#3 you can pass a variable as a reference. This is a cleaner version of #2 and can avoid some of the tricky syntax involved with passing complecated classes.

here are some examples:
#include <iostream>
#include <string>
using namespace std;

typedef struct {
	string str;
	int number;
} Combine;

//Return a structure encapsulating the data --
//   not the best technique, but can be useful if you
//   build a proper infrastructure. Not recommended.
Combine retCombine(string a, string B)/> {
	Combine retVal; 
	if (a.size() > b.size()) {
		retVal.str = a;
		retVal.number = a.size();
	} else {
		retVal.str = b;
		retVal.number = b.size();
	}
	return retVal;
}

//Pass in a pointer...
bool gr(string a, string b, int* len) {
	 if (a.size() > b.size()) {
		 *len = a.size();
		 return true;
	 } else {
		 *len = b.size();
		 return false;
	 }
}

//Pass in a referance...
bool sm(string a, string b, int& len) {
	 if (a.size() < b.size()) {
		 len = a.size(); //Note the syntax is cleaner
		 return true;
	 } else {
		 len = b.size();
		 return false;
	 }
}

int main() {
	string str1 = "Hello World!!!";
	string str2 = "It was the best of times, it was the worst of times";
	int len = 0;
	int *ptr = &len;
	Combine a = retCombine(str1, str2);
	cout << "a.str: " << a.str << "\na.number: " << a.number << endl;
	cout << "gr(str1, str2, len): " << gr(str1, str2, &len);
	cout << "\nLength: " << len << endl;
	cout << "sm(str1, str2, len): " << sm(str1, str2, len);
	cout << "\nLength: " << len << endl;
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 royjm248  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 18-January 08

Re: multiple return values

Posted 25 March 2008 - 03:21 PM

View PostNickDMax, on 23 Mar, 2008 - 09:35 PM, said:

Turns out you can't return more than a single object.

But there are a number of ways to solve your problem.

#1 you can return a structure or object that encapsulates both values.
-- lets face it, this is not a great solution. If for no other reason than you have a structure that is just used to return values.

#2 you can pass in a pointer to a value. This way you can manipulate the value of a variable in the function and see the changes in the calling function. This is referred to as passing "by reference". -- This really isn't returning a value though.

#3 you can pass a variable as a reference. This is a cleaner version of #2 and can avoid some of the tricky syntax involved with passing complecated classes.

here are some examples:
#include <iostream>
#include <string>
using namespace std;

typedef struct {
	string str;
	int number;
} Combine;

//Return a structure encapsulating the data --
//   not the best technique, but can be useful if you
//   build a proper infrastructure. Not recommended.
Combine retCombine(string a, string B)/> {
	Combine retVal; 
	if (a.size() > b.size()) {
		retVal.str = a;
		retVal.number = a.size();
	} else {
		retVal.str = b;
		retVal.number = b.size();
	}
	return retVal;
}

//Pass in a pointer...
bool gr(string a, string b, int* len) {
	 if (a.size() > b.size()) {
		 *len = a.size();
		 return true;
	 } else {
		 *len = b.size();
		 return false;
	 }
}

//Pass in a referance...
bool sm(string a, string b, int& len) {
	 if (a.size() < b.size()) {
		 len = a.size(); //Note the syntax is cleaner
		 return true;
	 } else {
		 len = b.size();
		 return false;
	 }
}

int main() {
	string str1 = "Hello World!!!";
	string str2 = "It was the best of times, it was the worst of times";
	int len = 0;
	int *ptr = &len;
	Combine a = retCombine(str1, str2);
	cout << "a.str: " << a.str << "\na.number: " << a.number << endl;
	cout << "gr(str1, str2, len): " << gr(str1, str2, &len);
	cout << "\nLength: " << len << endl;
	cout << "sm(str1, str2, len): " << sm(str1, str2, len);
	cout << "\nLength: " << len << endl;
	return 0;
}



Thank you, I am going to give it a try and see how it works out. Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1