#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Convert user input to a string variable?


    An exercise in my C++ class requires me writing a program that prompts the user to enter 3 strings. I'm then to output the 6 permutations of those strings (i.e. the strings "a" "b" "c" would be acb, bca, bac, cab, cba). This is the code I've come up with:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    	string a, b, c;
    	cout << "Enter the first string:";
    	cin >> a;
    	cout << endl;
    	cout << "Enter the second string:";
    	cin >> b;
    	cout << endl;
    	cout << "Enter the third string:";
    	cin >> c;
    	cout << endl;
    
    	cout << a << b << c << endl;
    	cout << a << c << b << endl;
    	cout << b << c << a << endl;
    	cout << b << a << c << endl;
    	cout << c << a << b << endl;
    	cout << c << b << a << endl;
    	return 0;
    }
    I'm also asked to rewrite the code so that each string can store a line of text.
    Does this mean the user's input becomes a variable of the string data type? I'm unfamiliar with achieving this if so, and I'd appreciate any advice that points me in the right direction. Thanks in advance.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    The user's input is already of string type.

    I'm assuming that you mean that you are to create the string that is to be output before you actually output it. If that is not correct then please state the requirement clearly. Please understand my confusion, since "rewrite the code so that each string can store a line of text" doesn't really say much.

    Based on my assumption, declare a string to hold the output line. Then concatenate the three strings together and assign that to that output-line string. Then output that output string. If you are supposed to create all the strings before actually outputting any of them, then declare an array of output strings (or a vector of strings).

IMN logo majestic logo threadwatch logo seochat tools logo