Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
Compare individual characters in Alice?
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default Compare individual characters in Alice? - 04-10-2007, 11:42 AM

I would like to emulate the following C++ code in Alice:

//=======================================
void doSomething(){
char temp = 0;
cout << "Enter an upper-case letter: ";
cin >> temp;

while(!((temp >= 'A') && (temp <= 'Z'))){
//Outside allowable character range
cout << "Bad input" << endl;
cout << "Enter an upper-case letter: ";
cin >> temp;
}//end while loop
//End end loop structure

//Good input, display msg and terminate.
cout << "Thanks for the " << temp << endl;

}//end doSomething function
//====================================

Since there is no char type in Alice, and apparently no built-in functions to compare strings, the solution is not obvious to me.

Any suggestions would be appreciated.

Thanks,
Dick Baldwin
   
Reply With Quote
Old
DrJim
Guest
 
Status:
Posts: n/a
Default 04-10-2007, 08:47 PM

The attached is the best that you can do as far as I know (other than possibly by enabling Jython scripting). The only compare functions supported for strings are equal and not equal and the only string function is concatination.
Attached Files
File Type: a2w Match a Letter.a2w (169.7 KB, 372 views)
   
Reply With Quote
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default 04-10-2007, 10:28 PM

Thanks Jim, that is a lot more than I could come up with.

I'm really surprised that the == operator can be used to compare strings. That is such an absolute no-no in Java that I didn't even consider trying it.

In Java if you try to use the == operator to compare two objects (String is an object in Java) the result will only be true if the two objects are actually the same object. If they are different String objects containing exactly the same characters, the result of the comparison will be false.

I'll have to think about this to see what it would take to use == and != operators to accomplish what I want to accomplish.

Thanks again.
Dick Baldwin
   
Reply With Quote
Follow-up Question
Old
DrJim
Guest
 
Status:
Posts: n/a
Question Follow-up Question - 04-11-2007, 09:17 AM

Along this vein, does anyone have a reference for which operators in Alice work with which data types and how? This is one area that I’ve only been able to figure out by trial and error – and in some cases, such as a “Transformable” data type, not even then.
   
Reply With Quote
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default 04-11-2007, 11:57 AM

Continuing with this vein, can someone provide an example of using the Transformable data type. I used the search button to search for Transformable in the forums and didn't find any links to the word (but maybe I didn't set up the search correctly).

Thanks,
Dick Baldwin
   
Reply With Quote
Reply

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



Copyright ©2019, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.