My assignment: Write a program that prompts the user to enter 3 strings. The program then outputs 6 diffente ways to arrange the words.
Example: a, b, c can be arrange as
abc
cba
I created my program but it only reads sigle words like hi, hello but when i enter somenthing like
"Hello there"
"How are you"
then progam fails. What is the problem when entering this words separeated by space. How come the computer cannot read them.
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
using namespace std;
int main()
{
string word1;
string word2;
string word3;
cout %26lt;%26lt; "Enter a word: ";
cin %26gt;%26gt; word1;
cout %26lt;%26lt; "Enter a second word: ";
cout %26lt;%26lt; "Enter a second word: ";
cin %26gt;%26gt; word2;
cout %26lt;%26lt; "Enter a third word: ";
cin %26gt;%26gt; word3;
cout %26lt;%26lt; "The permutation of the words you have entered are: " %26lt;%26lt; endl;
cout %26lt;%26lt; word1 %26lt;%26lt;"-" %26lt;%26lt; word2 %26lt;%26lt; "-" %26lt;%26lt; word3 %26lt;%26lt; endl;
cout %26lt;%26lt; word2 %26lt;%26lt;"-"%26lt;%26lt; word3%26lt;%26lt; "-" %26lt;%26lt;word1 %26lt;%26lt;endl;
cout %26lt;%26lt; word3 %26lt;%26lt;"-"%26lt;%26lt; word2 %26lt;%26lt;"-"%26lt;%26lt; word1 %26lt;%26lt;endl;
please help!
Help with C++ program?
rather than
cin%26gt;%26gt;word1;
use
getline(cin,word1);
it will read the entire line and assign to word1...
Reply:to input words with space use
cin.getline(char , int size ,in delimiter character) function
this does not work with normal cin because it is delimited with space
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment