Author |
Message |
naqibaba
|
Posted: Thu Dec 03, 2009 10:21 pm Post subject: THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.? |
|
|
In this program I am having to quiz a user about the capital city of each province in Canada. The Program should allow the user only 3 guesses only.
The program is getting way too long if I do it my way. THIS IS JUST FOR CAPITAL OF ONTARIO. WATCH.
var guess1, guess2, guess3 : string
loop
put " What is the capital city of Ontario. "
get guess1
if guess1 = "Toronto" or guess1 = "toronto" then
put " Correct answer. "
exit
else
put " Wrong."
end if
put " "
put " What is the capital city of Ontario. "
get guess2
if guess2 = "Toronto" or guess2 = "toronto" then
put " Correct answer. "
exit
else
put " Wrong."
end if
put " "
put " What is the capital city of Ontario. "
get guess3
if guess3 = "Toronto" or guess3 = "toronto" then
put " Correct answer."
exit
else
put " Wrong. No more Try Outs. Bye Bye."
exit
end if
end loop
Is there any way I can shorten it so I can do the other 10 provinces in a short amount of time. I would appreciate any kind of help. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Thu Dec 03, 2009 10:33 pm Post subject: RE:THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.? |
|
|
code: |
var guess1, guess2, guess3 : string
|
see arrays. Though you don't really need to keep all 3 answers anyway, so one variable should be enough.
code: |
if guess1 = "Toronto" or guess1 = "toronto" then
|
Normalize the capitalization.
code: |
if to_upper_case(guess) = "TORONTO" then
|
You are repeating too much code
code: |
put " What is the capital city of Ontario. "
...
put " What is the capital city of Ontario. "
|
Use loops.
If you think of copy-pasting the code 10 times, for all the provinces, then you are repeating code. Think about a way of using generic code that can ask about any province, and put _that_ inside a loop that runs 10 times.
(loading data from arrays or files is a typical choice). |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
naqibaba
|
Posted: Thu Dec 03, 2009 10:44 pm Post subject: Re: THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.? |
|
|
I just saw it and I dont think that its of my level. Can there be an example just to make it easy? |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
|
|
|
![](images/spacer.gif) |
Kharybdis
![](http://compsci.ca/v3/uploads/user_avatars/111642592477ec7e78e574.gif)
|
Posted: Thu Dec 03, 2009 11:30 pm Post subject: RE:THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.? |
|
|
Try starting with how to use for loops and loops before you get into arrays and records though. |
|
|
|
|
![](images/spacer.gif) |
Murphman
|
Posted: Fri Dec 04, 2009 1:54 pm Post subject: Re: THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.? |
|
|
You can shorten your code by alot. You only need to ask the question once in the loop.
Make a Variable called count and declare it as a int,
var count : int
Now make the count equal 0 out side of the loop
count := 0
now i am gonna let you figure the rest out on your own. this is all i will tell you. You need an exit when, and you also need to make count a counter somewhere in the if.
exit when count = 3
count := count + 1 <--- this will add 1 to count everytime the loop is ran[/list] |
|
|
|
|
![](images/spacer.gif) |
mirhagk
|
Posted: Sat Dec 05, 2009 8:59 pm Post subject: RE:THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.? |
|
|
also count+=1 does the same thing as count:=count +1.
and you should really look into arrays becuase you could just make 10 string variables and cycle through them all to ask all 10 questions or w/e |
|
|
|
|
![](images/spacer.gif) |
naqibaba
|
Posted: Mon Dec 07, 2009 8:15 pm Post subject: Re: THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.? |
|
|
why cant anybody just tell me how to do it. I am newbe in turing. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Mon Dec 07, 2009 8:35 pm Post subject: RE:THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.? |
|
|
Tell you how to do what? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
Superskull85
|
Posted: Mon Dec 07, 2009 10:56 pm Post subject: RE:THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.? |
|
|
Because you need to be able to do the work on your own. You don't learn much if people just give you the solution without studying it. |
|
|
|
|
![](images/spacer.gif) |
|