Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design And Implementation

Authors: KROENKE DAVID M.

1st Edition

8120322258, 978-8120322257

More Books

Students also viewed these Databases questions

Question

What is Working Capital ? Explain its types.

Answered: 1 week ago

Question

=+ what roles should government play in them ?

Answered: 1 week ago

Question

=+ Why do we have markets and, according to economists,

Answered: 1 week ago