Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 10th, 2005, 9:43 PM   #1
Haz
Programmer
 
Haz's Avatar
 
Join Date: Feb 2005
Location: England
Posts: 37
Rep Power: 0 Haz is on a distinguished road
Send a message via ICQ to Haz Send a message via AIM to Haz Send a message via MSN to Haz Send a message via Yahoo to Haz
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
__________________
MY BLOG :eek:

AND WEBSITE :eek:
Haz is offline   Reply With Quote
Old Dec 10th, 2005, 9:58 PM   #2
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4 Master is on a distinguished road
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;
		}
	}
Master is offline   Reply With Quote
Old Dec 10th, 2005, 10:12 PM   #3
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
or you could do,

if(losc!='L' && losc!='S')
__________________
"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
Kilo is offline   Reply With Quote
Old Dec 10th, 2005, 10:46 PM   #4
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
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!
Jessehk is offline   Reply With Quote
Old Dec 11th, 2005, 4:59 AM   #5
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
Shouldn't that be:
case 'L':
case 'S':
    done = false;
    break;
?
UnKnown X is offline   Reply With Quote
Old Dec 11th, 2005, 5:05 AM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
I would dispute the claim that a switch statement is 'best", but it is certainly an alternative in this case.
grumpy is offline   Reply With Quote
Old Dec 11th, 2005, 5:17 AM   #7
metsfan
Programmer
 
Join Date: Dec 2005
Posts: 57
Rep Power: 0 metsfan is an unknown quantity at this point
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
metsfan is offline   Reply With Quote
Old Dec 11th, 2005, 7:51 AM   #8
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
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.
grumpy is offline   Reply With Quote
Old Dec 11th, 2005, 8:03 AM   #9
metsfan
Programmer
 
Join Date: Dec 2005
Posts: 57
Rep Power: 0 metsfan is an unknown quantity at this point
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...
metsfan is offline   Reply With Quote
Old Dec 11th, 2005, 8:15 AM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
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)
One characteristic of characters in C/C++ is that they have an integral value, which is why the last line above works if x and y are of type char.

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
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC