Logic error on user input?

Hey all,

Could anyone please check my code and more than likely, my logic for this please? :)

Basically, the user enters a musical note value in the format: A#5 or C7 etc..

The code below is a portion of an input validation function and this bit in inside a switch of the input string length, this one is in the case 3: bit...

NOTE_VALUE is the string the user inputs..
goto NOTE_AGAIN; refers to a marker (or what ever its called) above the cin so any bad input it catches cycles back to the cin..

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
// IF: CHECK FOR BAD ENTRY ON ALL CHARACTERS..
			if (!isalpha(NOTE_VALUE[0]) || toupper(NOTE_VALUE[0]) < 'A' || toupper(NOTE_VALUE[0]) > 'G')	// IF THE 1ST CHARACTER IS OUT OF RANGE..
			{
				cout << "First character needs to be a letter (A-G).." << endl;
				goto NOTE_AGAIN;
				break;
			}
			else if (NOTE_VALUE[1] == SHARP)	// IF THE 2ND CHARACTER IS #..
			{
				// IF: CHECK IF 1ST CHARACTER IS VALID NOTE FOR #..
				if (toupper(NOTE_VALUE[0]) == 'B' || toupper(NOTE_VALUE[0]) == 'E')	// IF #, THEN CHECK AGAINST INVALID NOTES..
				{
					cout << "Not a valid note letter with #.." << endl;
					goto NOTE_AGAIN;
					break;
				}
				// END IF..
			}
			else if (NOTE_VALUE[1] != SHARP)	// IF THE 2ND CHARACTER IS NOT #..
			{
				cout << "Second character needs to be a #.." << endl;
				goto NOTE_AGAIN;
				break;
			}
			else if (isalpha(NOTE_VALUE[NOTE_VALUE.length() - 1]))
			//else if (!isdigit(NOTE_VALUE[NOTE_VALUE.length() - 1]))	// IF THE LAST CHARACTER IS NOT NUMBER..
			//else if (isalpha(NOTE_VALUE[2]))	// IF THE 3RD CHARACTER IS ALPHA..
			//else if (NOTE_VALUE[NOTE_VALUE.length() - 1] != '0')	// IF THE LAST CHARACTER IS ALPHA..
			{
				cout << "Last character needs to be a number (0-9).." << endl;
				goto NOTE_AGAIN;
				break;
			}
			// END IF.. 


With everything but checking the last character, it works exactly as i want it to but when it checks the last character it lets anything through. I'm trying to catch anything other than 0-9

If i type A#A it allows the A. If i type A## it allows the hash.
However if i type A;A it catches it. So it seems to be everytime a # is the second element, it lets anything through.

I've tried all sorts of conditions in the last else if. There's a few i've tried commented out..

I'm sure it's a logical error but can't get my head round it..

Thanks for reading this far haha..

Any ideas? I'm stumped..

Paul..
Last edited on
oh and i forgot to say, SHARP is declared as:

const char SHARP = '#';
Nevermind, i sorted it. I basically moved the last else if statement. Works perfectly now:

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
// IF: CHECK FOR BAD ENTRY ON ALL CHARACTERS..
			if (!isalpha(NOTE_VALUE[0]) || toupper(NOTE_VALUE[0]) < 'A' || toupper(NOTE_VALUE[0]) > 'G')	// IF THE 1ST CHARACTER IS OUT OF RANGE..
			{
				cout << "First character needs to be a letter (A-G).." << endl;
				goto NOTE_AGAIN;
				break;
			}
			else if (NOTE_VALUE[1] == SHARP)	// IF THE 2ND CHARACTER IS #..
			{
				// IF: CHECK IF 1ST CHARACTER IS VALID NOTE FOR #..
				if (toupper(NOTE_VALUE[0]) == 'B' || toupper(NOTE_VALUE[0]) == 'E')	// IF #, THEN CHECK AGAINST INVALID NOTES..
				{
					cout << "Not a valid note letter with #.." << endl;
					goto NOTE_AGAIN;
					break;
				}
				else if (!isdigit(NOTE_VALUE[2]))	// IF THE LAST CHARACTER IS NOT A NUMBER..
				{
					cout << "Last character needs to be a number (0-9).." << endl;
					goto NOTE_AGAIN;
					break;
				}
				// END IF..
			}
			else if (NOTE_VALUE[1] != SHARP)	// IF THE 2ND CHARACTER IS NOT #..
			{
				cout << "Second character needs to be a #.." << endl;
				goto NOTE_AGAIN;
				break;
			}
Topic archived. No new replies allowed.