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

Username:   Password: 
 RegisterRegister   
 How do I Use Rand.Int with a String?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
barney




PostPosted: Mon Jun 05, 2006 4:28 pm   Post subject: How do I Use Rand.Int with a String?

I have to make a mastermind game except with a different theme (in this case flowers) for my final assignment, and so here is the code. But the problem is that I can't store my ans1, ans2... as strings because I need them to be random integers. Is there any way I can fix this problem? Is there any way I can store my ans1, ans2... in another variable that is a string rather than 'int'?

ps. If you find any unneccessary bits of ocde in there, don't mind, I've been playing around with it a lot. And please dont mind about the 'unfinished' if statement at the end... I counldn't really finish it because this problem was bothering me. Please help! Confused

code:
var guess : int
var ans1 := Rand.Int (1, 5)
var ans2 := Rand.Int (1, 5)
var ans3 := Rand.Int (1, 5)
var ans4 := Rand.Int (1, 5)
var continue_char : string (1)

put ans1, ans2, ans3, ans4

color (103)
put "\t\t\t\tFLORAL MASTERMIND"
put "\nEnter your answer using the following coding scheme: \n\t* Dandelion - 1 \n\t* Daisy - 2 \n\t* Lily - 3 \n\t* Rose - 4 \n\t* Tulip - 5"
put "\nEnter any key to continue."
getch (continue_char)
put "\nEnter your answer: " ..
get guess

    if guess (1) = ans1 then
        put "*"
    elsif guess (2) = ans1 then
        put "*"
    elsif guess (3) = ans1 then
        put "*"
    elsif guess (4) = ans4 then
        put "*"
    end if
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Mon Jun 05, 2006 5:51 pm   Post subject: (No subject)

Why not use the function intstr. It converts an integer to a string. It would look something like this:
code:
var ans1 := intstr (Rand.Int (1, 5))

Why do you have those subscripts next to guess in your if statements? They should only be used if you're using one character from a string, or when you calling up a separate element in an array. Speaking of arrays, seeing as you have multiple variables of the same type and name, you should replace all those with a single array. If you're not sure what arrays are or how to use them, you can read about them in this tutorial.
Delos




PostPosted: Mon Jun 05, 2006 5:53 pm   Post subject: (No subject)

Hello.
To answer your question, you'll want to look at strint(). Now, for a minor critique of your code:
- use arrays instead of 5 seperate variables.
- instead of explicitely defining each clause of your if structure, use a for loop.
- check out the [Turing Walkthrough] for comprehensive explanations on these, and other, topics.

BTW, playing around is good! You'll make lots of errors and you'll learn why and how to avoid them. For instance, notice how I didn't tell you how to use strint(). This was intentional. Figure it out yourself, you'll learn far more that way! (And in this case it's one of those things that really isn't too difficult to get around to begin with).
Guest




PostPosted: Mon Jun 05, 2006 6:10 pm   Post subject: (No subject)

Yeah, all you need to do is look up on strint, it will take whatever string strint is and make it into an interger, or something of that matter.
HellblazerX




PostPosted: Mon Jun 05, 2006 6:40 pm   Post subject: (No subject)

Actually, what he's looking for is how to store an randomly generated integer as an string, which means he needs to convert that integer to a string, so he needs intstr, not strint.
barney




PostPosted: Mon Jun 05, 2006 8:50 pm   Post subject: (No subject)

Thanks HellblazerX! It worked! Very Happy
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  [ 6 Posts ]
Jump to:   


Style:  
Search: