944,159 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1626
  • C++ RSS
Nov 10th, 2009
0

How to capture a string into variable in a recursive function in C++?

Expand Post »
I tried to print all the possible combination of members of several vectors. Why
the function below doesn't return the string as I expected?

  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7. string EnumAll(const vector<vector<string> > &allVecs, size_t vecIndex, string
  8. strSoFar)
  9. {
  10. string ResultString;
  11. if (vecIndex >= allVecs.size())
  12. {
  13. //cout << strSoFar << endl;
  14. ResultString = strSoFar;
  15. return ResultString;
  16. }
  17. for (size_t i=0; i<allVecs[vecIndex].size(); i++) {
  18. strSoFar=EnumAll(allVecs, vecIndex+1, strSoFar+allVecs[vecIndex][i]);
  19. }
  20. ResultString = strSoFar;
  21. return ResultString;
  22.  
  23. }
  24.  
  25.  
  26. int main ( int arg_count, char *arg_vec[] ) {
  27.  
  28. vector <string> Vec1;
  29. Vec1.push_back("T");
  30. Vec1.push_back("C");
  31. Vec1.push_back("A");
  32.  
  33. vector <string> Vec2;
  34. Vec2.push_back("C");
  35. Vec2.push_back("G");
  36. Vec2.push_back("A");
  37.  
  38. vector <string> Vec3;
  39. Vec3.push_back("C");
  40. Vec3.push_back("G");
  41. Vec3.push_back("T");
  42.  
  43.  
  44. vector <vector<string> > allVecs;
  45.  
  46. allVecs.push_back(Vec1);
  47. allVecs.push_back(Vec2);
  48. allVecs.push_back(Vec3);
  49.  
  50.  
  51.  
  52.  
  53. string OutputString = EnumAll(allVecs,0,"");
  54.  
  55. // print the string or process it with other function.
  56. cout << OutputString << endl; // This prints nothing why?
  57.  
  58. return 0;
  59. }
The expected output is:

C++ Syntax (Toggle Plain Text)
  1. TCC
  2. TCG
  3. TCT
  4. TGC
  5. TGG
  6. TGT
  7. TAC
  8. TAG
  9. TAT
  10. CCC
  11. CCG
  12. CCT
  13. CGC
  14. CGG
  15. CGT
  16. CAC
  17. CAG
  18. CAT
  19. ACC
  20. ACG
  21. ACT
  22. AGC
  23. AGG
  24. AGT
  25. AAC
  26. AAG
  27. AAT
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gundalav is offline Offline
3 posts
since Jan 2009
Nov 10th, 2009
0
Re: How to capture a string into variable in a recursive function in C++?
C++ Syntax (Toggle Plain Text)
  1. cout << OutputString << endl; // This prints nothing why?
It prints.
That's the output I got, when I launched your program:
C++ Syntax (Toggle Plain Text)
  1. TCCGTGCGTACGTCCCGTGCGTACGTACCGTGCGTACGT
Reputation Points: 11
Solved Threads: 17
Junior Poster in Training
pecet is offline Offline
95 posts
since Oct 2009
Nov 10th, 2009
0
Re: How to capture a string into variable in a recursive function in C++?
Click to Expand / Collapse  Quote originally posted by pecet ...
C++ Syntax (Toggle Plain Text)
  1. cout << OutputString << endl; // This prints nothing why?
It prints.
That's the output I got, when I launched your program:
C++ Syntax (Toggle Plain Text)
  1. TCCGTGCGTACGTCCCGTGCGTACGTACCGTGCGTACGT
I mean it doesn't print the output as I expected, i.e. list of 27 length 3 strings.

I have corrected the comment in the code.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gundalav is offline Offline
3 posts
since Jan 2009
Nov 10th, 2009
0
Re: How to capture a string into variable in a recursive function in C++?
The problem is the setting of strSoFar to whatever EnumAll returns and changing what the previous function needs to have in that variable.

Not setting strSoFar in your for loop gives you this effect:

Initial function call: strSoFar = ""
1st recurse: strSoFar = "T"
2nd recurse: strSoFar = "TC"
1st 3rd recurse: strSoFar = "TCC" (print from if statement)
Fall back to 2nd w/out setting strSoFar: strSoFar = "TC"
2nd 3rd recurse: strSoFar = "TCG" (print from if statement)
Fall back to 2nd w/out setting strSoFar: strSoFar = "TC"
3rd 3rd recurse: strSoFar = "TCT" (print from if statement)

Sorry if my numbering is confusing. It's early and I couldn't come up with anything better =)

Recursion is really tricky when you're first starting out, but keep working at it and it will become almost second nature.

Hope that helps you.
Reputation Points: 103
Solved Threads: 42
Posting Whiz in Training
Sodabread is offline Offline
287 posts
since Nov 2009
Nov 10th, 2009
0
Re: How to capture a string into variable in a recursive function in C++?
Double posted. My bad.
Last edited by Sodabread; Nov 10th, 2009 at 9:21 am. Reason: Double Post
Reputation Points: 103
Solved Threads: 42
Posting Whiz in Training
Sodabread is offline Offline
287 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: My code is giving lots of errors
Next Thread in C++ Forum Timeline: Double conditional on a pointer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC