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
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);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started