![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
Problem with characters in if statement
'lo,
I tried to create a loop which was governed by a character: while(1) {
cout << "Large (L) or Small (S) computation?: ";
cin >> losc;
cin.get();
cout << "\n\n";
if( losc != ('L' || 'S') ) {
cout << "Invalid choice! Try again...\n\n";
}
else {
break;
}
}As you can see, if lost does not equal L or S, then it prints invalid choice and the loop reiterates, yet it doesn't work that way, if I insert ANYTHING, it still show "Invalid choice", is there a problem with the way I arranged the statement? Thanks, Haz |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4
![]() |
why don't you make it simpler by doing this
while(1) {
cout << "Large (L) or Small (S) computation?: ";
cin >> losc;
cin.get();
cout << "\n\n";
if( losc != 'L' || losc != 'S' ) {
cout << "Invalid choice! Try again...\n\n";
}
else {
break;
}
} |
|
|
|
|
|
#3 |
|
Expert Programmer
|
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#4 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
or, best of all, use a switch statemnt:
bool done = true;
while(done) {
cout << "(L)arge or (S)mall computation?: ";
cin >> losc;
cout << endl << endl;
switch(toupper(losc)) {
case L:
case S:
done = false;
break;
default:
cout << "invalid choice! Try again...\n\n";
break;
}
}make sure include the cctype header file for the toupper() function ![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
Shouldn't that be:
case 'L':
case 'S':
done = false;
break; |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
I would dispute the claim that a switch statement is 'best", but it is certainly an alternative in this case.
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Dec 2005
Posts: 57
Rep Power: 0
![]() |
this is a typical problem with c++; no typechecking! the binary operator "or" does not operate like this. the syntax for the binary operator is that it takes in two booleans and returns a boolean. you are giving this binary operator two characters; hence this will never work. anyway, lets look at the statement in question:
if( losc != ('L' || 'S') ) this is not logically equivalent to saying "if losc is not equal to L or S". it is actually logically equvaletn to "if losc is not equal to the binary operation (L or S). because the binary operator "or" returns a boolean, you are asking the evaluator if a boolean is not equal to a character. this is of course true. so thats why you get the error. otehrs posted the solution, but that is the explaination for the problem if you tried to program such a statement in a typesafe language such as SML or OCAML this would not even compile |
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
The issue has nothing to do type safety. The issue is related to the meaning of particular expressions.
The expression 'L' || 'S' (which is equivalent to 'L' or 'S') has a particular meaning in C++ and, in this case, yields a boolean result of true (which, in C/C++, is equivalent to an integer with a value of 1). The net effect is that the expression losc != ('L' || 'S') is testing if losc != 1, rather than testing if losc is not equal to one of 'L' or 'S'. The reason such an expression would not compile in SML or OACML is that the meaning of the expression would be different (in fact, invalid) in those languages. |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Dec 2005
Posts: 57
Rep Power: 0
![]() |
i was unaware of that...thank you for pointing that out. is there any particular reason why this is advantagous in c++? it seems as if allowing this expression to exist can only cause bugs and problems...
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
It's a side effect of other features of the language. While it does make for some problems like the one in this thread, it also offers some advantage. The advantage of it is that it allows chaining of expressions, and shortcuts.
int x, y;
// initialise x and y to something valid
int x_nonzero = (x != 0);
int y_nonzero = (y != 0);
int x_or_y_nonzero = x || y; // simpler than (x != 0) || (y != 0)It may seem that x || y is more cryptic than (x != 0) || (y != 0), despite the fact they have identifical meaning. But, if you think about it, the more wordy expression also takes more effort to interpret. Consider the difference if you have several integer variables, and you wanted to find out if at least one of them is non-zero |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|