If I execute the program and it runs successfully, it asks me if I want to try the program again. It has no issues exiting if I went through the program once, but when I try to exit the program after more than one retry, it just loops back to the "Would you like to try again" part times the number of times I retry the program (E.G. it would ask me 5 times if I retry the program 5 times).
Any suggestions and help would be greatly appreciated.
It's because you're calling your main function so it's treating it like a recursive function, so instead of exiting, it's returning to the last main function.
Solution: Don't call main from within main
instead do a while loop
Also, your do while logic was wrong, it will always loop with that logic
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
char sal[120];
while (true)
{
cout<<"[A] Check the palindrome\n\n";
cout<<"Please input a word(s): ";
cin.getline(sal, 120);
cin.clear();
char salChange[120];
strcpy(salChange,sal);
strrev(salChange);
if(strcmpi(sal,salChange)==0)
cout<<"The word(s) "<<sal<<" is a palindrome!\n";
else
cout<<"The word(s) "<<sal<<" is NOT a palindrome!\n";
char redo;
do{
cout<<"Would you like to try again?(y/n): ";
cin>>redo;
cin.ignore();
if (redo=='y'||redo=='Y')
{
system("cls"); //Since we have a while loop, we don't need to call main, after this if else we will loop again unless N is entered in which the function returns
}
elseif (redo=='n'||redo=='N')
{
return 0;
}
else
cout<<"You have entered an incorrect response!\n\n";
}while(redo!='y'&&redo!='Y'&&redo=='n'&&redo=='N');
}
system("pause");
return 0;
}