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

Username:   Password: 
 RegisterRegister   
 Negative # reverse
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
con.focus




PostPosted: Mon Nov 29, 2004 6:22 pm   Post subject: Negative # reverse

im trying to get this code to work with megative number

code:
function reverseNumber (rev : real) : real
    var snum : string
    var srevnum2 : string := ""
    snum := realstr (rev,0)
    for decreasing i : length (snum) .. 1
        srevnum2 += snum (i)
    end for
    end for
    result strreal (srevnum2)
end reverseNumber
 
 var rnum : real
put "Enter an number"
get rnum
put "The reverse of your number is: ", reverseNumber (rnum)


i keep getting this error string passed to 'strreal' is not in correct format do i need to add another for deacresing statement if so wut
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Mon Nov 29, 2004 6:38 pm   Post subject: (No subject)

Well, if you want the negative sign (-) to be before the reversed number, try this:

code:

function reverseNumber (rev : real) : real
    var snum : string
    var srevnum2 : string := ""
    snum := realstr (abs (rev), 0)
    if sign (rev) = -1 then     %you could just use if rev < 0 but this is more fun ;)
        srevnum2 += "-"
    end if
    for decreasing i : length (snum) .. 1
        srevnum2 += snum (i)
    end for

    result strreal (srevnum2)
end reverseNumber

var rnum : real
put "Enter an number"
get rnum
put "The reverse of your number is: ", reverseNumber (rnum)


Otherwise, if you want it at the end (as if everything were reversed), you're going to need to output a string. For that, try this:

code:

function reverseNumber (rev : real) : string
    var snum : string
    var srevnum2 : string := ""
    snum := realstr (rev, 0)
    for decreasing i : length (snum) .. 1
        srevnum2 += snum (i)
    end for

    result srevnum2
end reverseNumber

var rnum : real
put "Enter an number"
get rnum
put "The reverse of your number is: ", reverseNumber (rnum)


EDIT: notice my post is a little hard to read? It's because everything has been shoved to the right due to your over-sized avatar. Please, shrink it down.
EDIT EDIT: much better Wink
con.focus




PostPosted: Mon Nov 29, 2004 6:50 pm   Post subject: (No subject)

thx alot for all u help btw how did u become a mod
Cervantes




PostPosted: Mon Nov 29, 2004 6:56 pm   Post subject: (No subject)

You're most welcome Wink
con.focus wrote:
btw how did u become a mod

That's a question better suited for a Posted Image, might have been reduced in size. Click Image to view fullscreen..
con.focus




PostPosted: Mon Nov 29, 2004 8:56 pm   Post subject: (No subject)

k sry bout that im new here
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  [ 5 Posts ]
Jump to:   


Style:  
Search: