Validating user entry not working..

Hey all,

I'm using the code below and similar to validate user entry but can't seem to get it to work...

NOTE_LENGTH is declared as an integer, previously..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// GO BACK HERE IF BAD ENTRY..
DURATION_AGAIN:

cout << "Note Length (1=BAR | 2=HALF | 4=1/4th (Beat) | 8=1/8th | 16=1/16th): ";
cin >> NOTE_LENGTH;

// **************************************************
// CHECK FOR CORRECT ENTRY (NOTE LENGTH)..
// **************************************************
// IF: CHECK LENGTH OF ENTERED NOTE VALUE..
if (NOTE_LENGTH != 1 || NOTE_LENGTH != 2 || NOTE_LENGTH != 4 || NOTE_LENGTH != 8 || NOTE_LENGTH != 16 || isalpha(NOTE_LENGTH))
{
	cout << "Not a correct value.." << endl;
	goto DURATION_AGAIN;
}
// END IF.. 


What's supposed to happen is allow only 1, 2, 4, 8 or 16 to be entered. But when either one of the correct responses is entered, it passes the if and displays the cout and then with the goto, cycles round again.

When a string or alpha character is entered, the whole thing blows up. The cout msg just loops madly up and down he screen and i have to close the program.

I can't see what could be wrong with the code though, although i'm still a rookie.

Any ideas? especially a better validation technique please :)

Can i declare NOTE_LENGTH as an array like:
int NOTE_LENGTH[5] = {1,2,4,8,16};
...or something like that and have the contents as a sort of allowed entry list?

Paul..
Last edited on
if (NOTE_LENGTH != 1 || NOTE_LENGTH != 2 || NOTE_LENGTH != 4 || NOTE_LENGTH != 8 || NOTE_LENGTH != 16 || isalpha(NOTE_LENGTH))

This will always always always come out as true. There is no value of NOTE_LENGTH for which every one of these OR'd statements comes out as false.
Hey again Moschops. Oh ok, thanks. So where am i going wrong in my code / logic? How i think i'm saying it is:

If its not: 1, 2, 4, 8 or 16 or its a letter then tell them and go back round.

And I think i've got that in my code but what do i need to change?
If it isn't 1, AND it isn't 2, AND it isn't 4...
Last edited on
haha, yeah of course it is. Brilliant. So simple haha. Thanks :)
Although, if a letter or character is entered, the program still goes crazy.

How can i guard against the user entering a letter? I would have thought the isalpha or !isdigit would work.
Topic archived. No new replies allowed.