Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
How to make a loop that loops a specific number of times ?
Author Message
sdn121019

Posted: Sat Apr 18, 2020 3:48 pm   Post subject: How to make a loop that loops a specific number of times ?

Hello, this is my first post.
I have a school project where I have to make a rock paper scissors assignment that lets the player play the game a specific number of times by entering how many points they want to play to against a computer.
Currently the program runs perfectly fine but when the player plays the game the game just keeps looping forever.
Pls help..

Here is the code I currently have:

var randNum : int
var myNum : int
var playagain : char
var playername : string
var score : int

randNum := Rand.Int (1, 3)

const ROCK : int := 1
const PAPER : int := 2
const SCISSORS : int := 3

put "Hello Player! What's your name?"
get playername
put "Welcome ", playername, "! How many points do you want to play to?"
get score

delay (100)
delay (3500)
cls

loop

put "Choose:"
put ""
put " 1. ROCK"
put " 2. PAPER"
put " 3. SCISSORS"

get myNum

if myNum = 1 then
put "YOU CHOSE ROCK"
elsif myNum = 2 then
put "YOU CHOSE PAPER"
elsif myNum = 3 then
put "YOU CHOSE SCISSORS"
end if

if randNum = 1 then
put "COMPUTER CHOSE ROCK"
elsif randNum = 2 then
put "COMPUTER CHOSE PAPER"
elsif randNum = 3 then
put "COMPUTER CHOSE SCISSORS"
end if

if myNum = 1 and randNum = 1 then
put "YOU TIED!"
end if
if myNum = 1 and randNum = 2 then
put "YOU LOSE!"
end if
if myNum = 1 and randNum = 3 then
put "YOU WIN!"
end if

if myNum = 2 and randNum = 1 then
put "YOU WIN!"
end if
if myNum = 2 and randNum = 2 then
put "YOU TIED!"
end if
if myNum = 2 and randNum = 3 then
put "YOU LOSE!"
end if

if myNum = 3 and randNum = 1 then
put "YOU LOSE!"
end if
if myNum = 3 and randNum = 2 then
put "YOU WIN!"
end if
if myNum = 3 and randNum = 3 then
put "YOU TIED!"
end if

end loop

scholarlytutor

Posted: Thu May 28, 2020 11:17 pm   Post subject: RE:How to make a loop that loops a specific number of times ?

Sorry that no one got to this earlier. What your loop needs is a counter. Just before your loop starts, write:

var counter : int := 0

Second, after you write loop, you need an exit statement.

exit when counter = score

(personally I would use another name for the variable 'score', such as num_of_games)

Finally, just before you write end loop, you will type:

counter += 1

Now your program will keep track of how many times it has looped. When the counter matches the game limit the user entered, the loop will exit.

I know you've likely finished this by now but you or any one else with a similar question can use this for future reference.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 2 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: