Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 if statement question
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JR




PostPosted: Wed Apr 13, 2005 8:08 am   Post subject: if statement question

Hey i got a binary convertion program and i was wondering how do i check if the number that was entered by the user only has 1's and 0's ?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Apr 13, 2005 8:17 am   Post subject: (No subject)

convert to string, then loop though characters.
Turing:

var text : string := intstr (1001200)
for i : 1 .. length (text)
    if text (i) = "1" or text (i) = "0" then
        put text (i)
    else
        put "foo"
    end if
end for

now obviously instead of saying "foo", you'll simply quit the loop and show an error.
jamonathin




PostPosted: Wed Apr 13, 2005 8:20 am   Post subject: (No subject)

use a for loop to check every character in the string/int variable. And in that for loop use an if statement that says if the character at the for loop number is equal to 1 or 0 then do whatever.
p.s. run the for loop for the legnth of the binary. if your binary is int, use length(intstr(binary))
EDIT: man i type slow. .
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: