My try Again Loop is not working correctly

My programs test to see if the user input is a well formed formula. My try again loop in my code does the loop but it skips the user input and goes straight to saying it is a invalid WFF. I need some help as to why it is doing that.Thanks for any help.

Code:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <string>
using namespace std;

int main()
{
	char Y;
    string connectors = "!V^->";
	string statements = "ABCDEFGHIJKLMNOPQRSTUWXYZabcdefghijklmnopqrstuwxyz";
	string input;
	
	bool quit = false;
	bool lastWasAlpha = false;
	bool lastWasNot = false;
	bool lastWasConnector = false;
	bool isValidWff = true;
	
	

	while (quit == false)
	{
		cout << "Enter a string: ";
		getline(cin, input);

		for (unsigned int i = 0; i < input.length(); i++)
		{

			char c = input[i];
			cout << c << " ";
			if (c == ' ' || c == '-' || c == '(' || c == ')')
			{
				continue;
			}
			if (c == '!' || c == 'v' || c == '^' || c == '>')
			{
				if (c == ')' && i > 0)
				{
					if (input[i - 1] == '(')
					{
						lastWasConnector = true;
						lastWasAlpha = false;
						continue;
					}
					else
					{
						isValidWff = false;
						break;
					}

				}


				if (c == '>' &&  i > 0)
				{
					if (input[i - 1] == '-')
					{
						lastWasConnector = true;
						lastWasAlpha = false;
						continue;
					}
					else
					{
						isValidWff = false;
						break;
					}

				}

				if (c == '!')
				{
					if (!lastWasAlpha)
					{
						isValidWff = false;
						break;
					}
					lastWasConnector = false;

				}
				else
				{
					if (lastWasAlpha)
					{
						isValidWff = false;
						break;
					}
					lastWasNot = true;

				}
				lastWasAlpha = false;
			}
			else if (isalpha(c))
			{
				if (lastWasAlpha)
				{
					isValidWff = false;
					break;
				}
				lastWasAlpha = true;
				lastWasConnector = false;
				continue;
			}
			else
			{
				isValidWff = false;
			}
		}

		cout << (isValidWff ? "That is a WFF" : "NOT Valid WFF") << endl;
		
        cout << " Would you like to quit Y/N" << endl;
		auto answer = getchar();

			if (answer == 'Y')
			{
				quit = true;
			}
			else
			{
				system("cls");
			}

		

		} 
	
}





Last edited on
else if (isalpha(c)). isalpha takes an int not char else if (isalpha(int(c)))
see documentation: http://www.cplusplus.com/reference/cctype/isalpha/

- Also all the break; and continue; are by passing some variables.
you might want to re-write them.
else if (isalpha(c)). isalpha takes an int not char else if (isalpha(int(c)))

A char can always be safely cast into an int, int is just a bigger char integer, you don't need the explicit int conversation. Internally, the character is converted into the integer value corresponding to its ASCII value when passed.

nightsky, please edit your post and format your code! Edit your post, highlight your code, and press the "<>" button under "Format:". Thank you.

What input reproduces your problem?
Last edited on
Sorry yea i forgot to format it. so after the first input and the output given back to the user, then it prompts the user to choose if they want to stop the program Y/N and the Y input works but the N does not it lets the user input but it does not give the correct output.
Last edited on
Night what output are you expecting when 'N' is input?
so the output is suppose to just rerun the program asking for another input then repeating the process again
Topic archived. No new replies allowed.