Help fixing an infinite loop

I'm trying to write a function for checking whether the user wishes to continue, and then another function to check whether the input was valid. For some reason these functions either don't complete or are skipping over user input and cause an infinite loop. I have tried putting in dummy return values to find the problem, and noted my results as comments in the code as such:

 
//************NOTES ON DUMMY RETURN VALUES 


Also - I realize this should really be implemented in a class, but my C++ class isn't on to classes yet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//=======================================MAIN=================================
void main()
{
	do
	{
	//
	//
	//
	//This requires the user to press enter prior to ending the program.
	cout << "Press <enter> to continue";
	cin.ignore(100, '\n');
	}while(again() == 1);
	return;
}


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
//	
//
//
//This function checks whether the user wishes to continue or not.
//************Currently this function will output "Would you like to continue? Please enter either yes or no : \n" and then
//************infinitely loop "It seems the character(s) you entered was not valid, please try again: \n" without even waiting
//************for cin.get(check, strlen(check), '\n');
bool again()
{
	//************If return true; is placed here, the loop from main is reset and "Press <enter> to continue" is output again, 
	//************and another character will need to be entered. 

	//************If return false; is placed here, the program ends after the user
	//************press <enter> the first time.
	int test = 0;
	char check[10] = {'\0'};
	do
	{		
		cout << "Would you like to continue? Please enter either yes or no : \n";
		cin.get(check, strlen(check), '\n'); //Why does the program ignore recieving input for this array?
		cin.ignore(100, '\n');
		do
		{
			cout << "It seems the character(s) you entered was not valid, please try again: \n"; 
			cin.get(check, strlen(check), '\n'); //This just requests the user re-enters their input if they entered an invalid character.
			cin.ignore();
		}while(2 == yorn(check));
		
		if(1 == yorn(check))
		{
			return true;
		}
		else if(0 == yorn(check))
		{
			return false;
		}
	} while(2 == yorn(check));
}


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
//
//
//
//This function checks for whether a user entered any iteration of Yes or No.
int yorn(char check[])
{
//************If return 1; is placed here, the program loops through outputting "Would you like to continue? Please enter either
//************yes or no : \n",  "It seems the character(s) you entered was not valid, please try again: \n" and \
//************"Press <enter> to continue." 

//************If return 0; is placed here, the program will output "Would you like to continue? Please enter either
//************yes or no : \n" and "It seems the character(s) you entered was not valid, please try again: \n" once
//************and then end right away.
	if(strcmp(check, "y") == 0 || strcmp(check, "Y") == 0 || strcmp(check, "yes") == 0 || 
		strcmp(check, "yeS") == 0 || strcmp(check, "yEs") == 0 || strcmp(check, "yES") == 0 || 
		strcmp(check, "Yes") == 0 || strcmp(check, "YeS") == 0 || strcmp(check, "YES") == 0)
	{
		return 1;
	}else if(strcmp(check, "n") == 0 || strcmp(check, "N") == 0 || strcmp(check, "no") == 0 || 
		strcmp(check, "nO") == 0 || strcmp(check, "No") == 0 || strcmp(check, "NO") == 0)
	{
		return 0;
	}else
	{
		return 2;
	}
}
Last edited on
I believe it's int main()

I suggest you use std::string instead and your function seems to return a bool, would it not be best to compare to a boolean in the main function. }while(again() == true);
line 16 char check[10] = {'\0'}; You fill the array with null characters

line 20 cin.get(check, strlen(check), '\n'); You call strlen on your check array. You need to remember that you filled this array with null characters, and strlen counts the elements until it reaches a null character. Which means strlen(check) evaluates to zero, therefor zero chars are input from cin.

line 22 - 27 you don't wan't a do while loop here because you only wan't the user to enter data a second time if there was a problem with the first input.

if you add a statement such as this check[0] = toupper(check[0]);
you can simplify your your yorn function

something like this
1
2
3
4
5
6
        if(check[0] == 'Y')
		return 1;
	else if(check[0] == 'N')
		return 0;
	else
		return 2;
Last edited on
Thanks guys! That got it working!
Topic archived. No new replies allowed.