1.11M Members

Recursive function to convert string to int

 
0
 

I am writing a recursive function to return an integer after converting a string. I am very close. This was already quite the brain teaser, and recursion is not my cup of tea. Perhaps somebody can help with this small little problem I am having. The last thing I need to do on this before I am completely satisfied is for the function to return with the integer when it detects a whitespace (' ').

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int convert(const string& s)
/*
* Precondition:string type object has been passed to function 
* containing only numbers.
* Postcondition: A numeric version of the string has been returned, and 
* the original string parameters have been unchanged.
*/
{
	int n = s.length() - 1;
	
	// the string is empty, the value should be zero
	if(s.empty()) 
		return 0;

	if(s[0] == '-')
		return -convert(s.substr(1, n--) ) ;	
		//recursive call - changes each subsequent numeric value to a negative number
		
	if ( s.length()  == 1)
	{
		if(isdigit(s[0]))
			return s[0]- '0'; // translate the character to its numeric value
		else 
			cout<<"Invalid numeric character: "<<s<<endl;
			return 0;
	}
	 return convert(s.substr(s.length() - 1,1)) + convert(s.substr(0, n--) ) * 10 ;	 //recursive call
}

int main()
{
	// Make some strings 
	string test1("1234");
	string test2("921");
	string test3("0");
	string test4("");
	string test5("0034");
	string test6("-4");
	string test7("-400001");
	string test8("123ABC");
	string test9("12 34 56");
	// test calls to convert
	cout<<"Value of \""<<test1<<"\" is " <<convert(test1)<<endl;
	cout<<"Value of \""<<test2<<"\" is " <<convert(test2)<<endl;
	cout<<"Value of \""<<test3<<"\" is " <<convert(test3)<<endl;
	cout<<"Value of \""<<test4<<"\" is " <<convert(test4)<<endl;
	cout<<"Value of \""<<test5<<"\" is " <<convert(test5)<<endl;
	cout<<"Value of \""<<test6<<"\" is " <<convert(test6)<<endl;
	cout<<"Value of \""<<test7<<"\" is " <<convert(test7)<<endl;
	cout<<"Value of \""<<test8<<"\" is " <<convert(test8)<<endl;
	cout<<"Value of \""<<test9<<"\" is " <<convert(test9)<<endl;

	return EXIT_SUCCESS;
}
 
0
 

So what is the program doing wrong?

(details please)

 
0
 

So what is the program doing wrong?

(details please)

Obviously, when I said:
"The last thing I need to do on this before I am completely satisfied is for the function to return with the integer when it detects a whitespace (' ')."

The problem would be needing help with when to return an integer when it detects a whitespace. Dont troll daniweb bud.

Came here to post that I solved my problem:

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int convert(const string& s)
//
//Precondition:string type object has been passed to function 
//containing only numbers.
//Postcondition: A numeric version of the string has been returned, and 
//the original string parameters have been unchanged. An error has been output for any 
//non-numeric characters, and all white spaces have been ignored.
//
{
   string temp;

//handle whitespaces
   if (s.find_first_of(' ')) // if there is a whitespace, so the program will use the same string without any whitespaces
   {
      for(int count = 0; count < s.length(); count++)
      {
         if(s[count] != ' ')
         {
            temp += s[count]; 
         }
      }
   }
   else 
   {
      string temp(s); // there are no whitespaces, so make the function will use the whole string
   }

   int n = temp.length() - 1; // size of the string (old or new)

// the string is empty, the value should be zero
   if(temp.empty()) 
   {
      return 0;
   }
// handle a negative at the beginning of the string
   if(temp[0] == '-')
   {
      return -convert(temp.substr(1, n--) ) ;	
   //recursive call -  return value is changed  for each 
   //subsequent numeric value to a negative number
   }
   if ( temp.length() == 1)
   {
   // stopping case
      if (isdigit(temp[0]))
      {
         return temp[0]- '0'; // translate the character to its numeric value
      }
      else
      {
         cout<<"Error: "<< temp <<" is not a valid numeric character. Conversion to 0"<<endl;
         return 0;
      }
   }
   return  convert(temp.substr(temp.length() - 1, 1 )) + convert(temp.substr(0, n--) ) * 10 ;	
//recursive call
}

int main()
{
// Make some strings 
   string test1("1234");
   string test2("921");
   string test3("0");
   string test4("");
   string test5("0034");
   string test6("-4");
   string test7("-400001");
   string test8("123ABC");
   string test9("12 34 56");
// test calls to convert
   cout<<"Value of \""<<test1<<"\" is " <<convert(test1)<<endl;
   cout<<"Value of \""<<test2<<"\" is " <<convert(test2)<<endl;
   cout<<"Value of \""<<test3<<"\" is " <<convert(test3)<<endl;
   cout<<"Value of \""<<test4<<"\" is " <<convert(test4)<<endl;
   cout<<"Value of \""<<test5<<"\" is " <<convert(test5)<<endl;
   cout<<"Value of \""<<test6<<"\" is " <<convert(test6)<<endl;
   cout<<"Value of \""<<test7<<"\" is " <<convert(test7)<<endl;
   cout<<"Value of \""<<test8<<"\" is " <<convert(test8)<<endl;
   cout<<"Value of \""<<test9<<"\" is " <<convert(test9)<<endl;

   return EXIT_SUCCESS;
}
Question Answered as of 2 Years Ago by WaltP
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: