#include <iostream>
#include <string>
using namespace std;
char rerun;
string reverse(string x){
string x_out = x;
for(int i = 0; i < x.size(); i++){
if(i == x.size() - 1){
x_out[i] = x[0];
} else {
x_out[i] = x[x.size() - i - 1];
}
}
return x_out;
}
int main(){
do {
string in_string;
cout << endl;
cout << "This program will reverse any string you input." << endl;
cout << "-----------------------------------------------" << endl;
cout << "Enter a string: ";
getline(cin, in_string);
cout << "Your reversed string is:" << endl;
cout << endl;
cout << reverse(in_string);
cout << endl;
cout << "RUN PROGRAM AGAIN? (Y/N): ";
cin >> rerun;
} while (rerun == 'y' || rerun == 'Y');
}
Hey, I am having problems with this do-while loop. I'm trying to accept user input (the variable rerun) to determine whether to run the program again or not. What it is supposed to do is run int main() again if the user inputs 'Y' or 'y'. But for some reason, it won't run the reverse() function when it is called. Below is what the program actually runs like on my terminal. Anyone have any tips? Thanks.
This program will reverse any string you input.
-----------------------------------------------
Enter a string: Woah wait
Your reversed string is:
tiaw haoW
RUN PROGRAM AGAIN? (Y/N): Y
This program will reverse any string you input.
-----------------------------------------------
Enter a string: Your reversed string is:
RUN PROGRAM AGAIN? (Y/N):