program runs fine, but when it turn back to input again, the function stuck. I cannot use "cin >>" for c++string because it won't accept whitespace (" ")
#include <iostream>
#include <string>
using namespace std;
// Function of reverse
void reverse(string &Istr, int strlen)
{
int i;
char temp;
for (i=0; i < strlen /2; i++)
{
temp = Istr[i];
Istr[i] = Istr[strlen-i-1];
Istr[strlen-i-1] = temp;
}
}
int main()
{
char ans = 'y';
while(ans != 'n')
{
ans = 'y';
string str= "";
cout << "Enter string to reserve:" << endl; // input string
getline(cin, str);
int slength = str.length(); //length of the string
reverse(str, slength);
cout << "Reversed string: " << str << endl; // output the reversed string
cout << "Do you want to insert another string (Y/N): "<< endl;
cin >> ans;
}
return 0;
}