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

Problem: cannot rerun a program which includes c string

$
0
0
The program runs fine however, when it's asked to input again, it's stuck.

#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()
{
	start:

	cout << "Enter string to reserve:" << endl;		// input string
	string str;
	getline(cin, str);
	
	int slength = str.length();		//length of the string
	reverse(str, slength);
	cout << "Reversed string: " << str << endl;		// output the reversed string	
	again:
	cout << "Do you want to insert another string (Y/N): "<< endl;
	char ans;
	cin >> ans;
	if (ans == 'y')
	{	
		str.clear();
		goto start;
	}
	else if (ans != 'n')
		goto again;

	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>