Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ combinations -- trying to write output to string. I can display the output c

ID: 3862783 • Letter: C

Question

C++ combinations -- trying to write output to string.

I can display the output correctly using cout statement, but cannot place it into a string for each iteration in the loop.

the cout statement is std::cout << prefix + str[j] << std::endl;

it displays he correct results. However, I would like to get that information into a string so that I can pass it into another function. can i do this with a string? do i have to write the cout statement to a file and then read it back in in order to get it into a string?

I have tried to use:

string output = prefix +str[j]

but get identiifier j is unidentidfied.

. I would like to save it to a string instead. How do I do this? the full code is below

std::cout << prefix + str[j] << std::endl; // this works

                    // string output = prefix + str[j]; //unable to write the output to string on each loop

#include

#include

#include

using namespace std;

void print_str(const char*, std::string, const int, const int);

int main()

{

       int length = 4;

       char str[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

       int n = sizeof str;

       print_str(str, "", n, length);

       return 0;

}

void print_str(const char str[], std::string prefix, const int n, const int length)

{

       if (length == 1)

       {

              for (int j = 0; j < n; j++)

                      std::cout << prefix + str[j] << std::endl; // this works

                    // string output = prefix + str[j]; //unable to write the output to string on each loop

       }

       else

       {

              for (int i = 0; i < n; i++)

                      print_str(str, prefix + str[i], n, length - 1);

       }

}

Explanation / Answer

I didn't get your question

1)If you want to store each permutation in a string and print it then use first one

2)if you want to store entire output in a string "result" and print it the use second one


#include <iostream>

#include <string>

using namespace std;

void print_str(const char*, std::string, const int, const int);

int main()

{

       int length = 3;

       char str[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

       int n = sizeof str;

       print_str(str, "", n, length);

       return 0;

}

void print_str(const char str[], std::string prefix, const int n, const int length)

{

       if (length == 0)

       {
           cout<<prefix<<endl; //print prefix
       }
       else

       {

              for (int i = 0; i < n; i++)

                      print_str(str, prefix + str[i], n, length - 1);

       }

}


#include <iostream>

#include <string>

using namespace std;

void print_str(const char*, std::string, const int, const int);

string result=""; //global varaible to store result

int main()

{

       int length = 2;

       char str[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

       int n = sizeof str;

       print_str(str, "", n, length);
       cout<<result;
       return 0;

}

void print_str(const char str[], std::string prefix, const int n, const int length)

{  
      

       if (length == 0)

       {
           result=result+prefix+" ";
       }
       else

       {

              for (int i = 0; i < n; i++)

                      print_str(str, prefix + str[i], n, length - 1);

       }

}

I didn't get your question

1)If you want to store each permutation in a string and print it then use first one

2)if you want to store entire output in a string "result" and print it the use second one


#include <iostream>

#include <string>

using namespace std;

void print_str(const char*, std::string, const int, const int);

int main()

{

       int length = 3;

       char str[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

       int n = sizeof str;

       print_str(str, "", n, length);

       return 0;

}

void print_str(const char str[], std::string prefix, const int n, const int length)

{

       if (length == 0)

       {
           cout<<prefix<<endl; //print prefix
       }
       else

       {

              for (int i = 0; i < n; i++)

                      print_str(str, prefix + str[i], n, length - 1);

       }

}


#include <iostream>

#include <string>

using namespace std;

void print_str(const char*, std::string, const int, const int);

string result=""; //global varaible to store result

int main()

{

       int length = 2;

       char str[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

       int n = sizeof str;

       print_str(str, "", n, length);
       cout<<result;
       return 0;

}

void print_str(const char str[], std::string prefix, const int n, const int length)

{  
      

       if (length == 0)

       {
           result=result+prefix+" ";
       }
       else

       {

              for (int i = 0; i < n; i++)

                      print_str(str, prefix + str[i], n, length - 1);

       }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote