Nov 19, 2014 at 5:35pm UTC
My program works, but when I run the program again it and type new input. The screen also prints the input from the previous iteration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string ans;
string uno;
string dos;
string tres;
int length ;
do {
cout << "Enter random words" << endl;
getline (cin , uno);
cout <<"Type your name" <<endl;
getline (cin , dos);
length = uno.length()-1;
for (int i=length; i >= 0; i--)
{ tres+=uno.at(i);
}
tres.insert(tres.size() / 2, dos);
cout<<tres<<endl;
cin.clear();
system ("cls" );
cout <<"Would you like to run this program again(yes/no)" <<endl;
cin>>ans;
}while (ans=="yes" );
cin.clear();
system ("pause" );
return 0;
}
Last edited on Nov 19, 2014 at 5:59pm UTC
Nov 19, 2014 at 5:59pm UTC
While I am unfamiliar with C++ console input, this
http://stackoverflow.com/questions/18786575/using-getline-in-c _may_ answer your question. Make sure you empty your strings at the beginning of the do {} loop. I'm not sure if getline() will.
I would also refrain from using system("") to things like cls and pause because they aren't portable. I had to comment out those lines to compile your code. Lastly, your check ans=="yes" might not work if someone types Yes or YES or YeS.
Last edited on Nov 19, 2014 at 6:03pm UTC
Nov 19, 2014 at 8:14pm UTC
The problem is that you're appending to tres without clearing it between passes through the loop. Put tres.clear();
at line 20.