Trying to exit program.

I just need a little help with my code.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
using namespace std;

int main()
{
    char sal[120];
    cout<<"[A] Check the palindrome\n\n";
    cout<<"Please input a word(s): ";
    cin.getline(sal, 120);
    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;
    if (redo=='y'||redo=='Y')
    {
      system("cls");
      cin.ignore();
      main();
      }
    else if (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;
}
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

You said if it is not 'y' or it is not 'Y' .. etc

It will always be either not 'y' or not 'Y'

You need to say not 'y' AND not 'Y' etc...

Here's the modified code

Also you needed to put cin.clear() after getline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
using namespace 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
		  }
		else if (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;
}
Thank you.

Actually, this is going to be a function as a part of a bigger program. I'm just trying to do the functions piece by piece.

Will the while loop also work there?
Last edited on
I'm not sure how you're going to implement it into your function, but I'm thinking yes.
use the getchar(); function in the cstdio header file instead of system("pause")
use the getchar(); function in the cstdio header file instead of system("pause")


no. best to see here:
http://www.cplusplus.com/forum/beginner/1988/


OP: do you have to use nasty c-style arrays or can you use std::string?
Last edited on
Topic archived. No new replies allowed.