Of course, if you just wanted it in function form, there are again other methods, including the above and passing p1, p2, p3 and returning it to a new string.
Read the task carefully. This task is not to concatenate three strings.
First, it's better than your original post (which you've been advised against) reading, "Send me a PM."
Second, the task isn't outlined well at all, so there's absolutely nothing wrong with my solution. Is it supposed to be one string split three times? How many characters per split? Are there certain characters that should initiate a newline? Is it three strings with formatting? Are we starting with these as literals, or are we accepting input?
The instructions aren't clear enough to make a definitive statement as to what is and isn't allowed, and as a result, I'm free to interpret it in any way that appears to solve the problem.
Going back to the above: THAT'S why you were told not to solicit through private messages; maybe you have some insight the rest of us would like to see.
The instructions aren't clear enough to make a definitive statement as to what is and isn't allowed, and as a result, I'm free to interpret it in any way that appears to solve the problem.
No, it is more than clear. If you have any problems about it, go back to study C++ for a few more months until you understand this simple homework assignment.
Well since you seem to be going to great lengths to be less than useless by providing criticism whilst offering no actual solutions, OP, if you require one string input, and we're assuming you need newlines where spaces are present and nothing more, you can format it into another string:
#include <iostream>
#include <string>
#include <cctype>
usingnamespace std;
int main()
{
string input;
getline(cin, input);
for (size_t i = 0; i != input.size(); ++i) {
if (isspace(input[i])) {
cout << endl;
continue;
}
cout << input[i];
}
cout << endl;
return 0;
}
I'm not a fan of continue statements, but they get the job done, if nothing else. If you want to pass the string to functions, you can pass the first with your input string and a target string reference, and for the second, you can just pass the input string.
#include <iostream>
#include <string>
#include <vector>
int main( )
{
std::cout << "input: ";
std::size_t length{};
std::vector<std::string> strings{};
for( std::string input; std::cin >> input; ) {
/*
the first string entered determines the length of
the rest of the strings
*/
if( strings.empty( ) ) length = input.length( );
if( input.length( ) != length ) break;
strings.push_back( input );
}
std::cout << "\n""output: \n";
// outer loop is for each of the characters in user input
for( std::size_t i{}; i < length; i++ ) {
// the inner loop is for each element(user input) in the vector
for( std::size_t j{}; j < strings.size( ); j++ ) {
/*
we want to loop through all the elements(j) and print
the ith character
*/
std::cout << strings.at( j ).at( i );
}
std::cout << "\n";
}
}