Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

problem: While loops with c++ string

$
0
0
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;
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>