quick answer please

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
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
It would be better to use std::cin >> std::ws and not ignore()
http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction

I'm not sure if getline() will.
It will. getline overwrites string.
The problem is that you're appending to tres without clearing it between passes through the loop. Put tres.clear(); at line 20.
Topic archived. No new replies allowed.