Computer Science Canada

help - calender

Author:  whoareyou [ Fri Mar 04, 2011 9:53 am ]
Post subject:  help - calender

What is it you are trying to achieve?
To create a user-generated calender using repetition commands.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Turing:


var nodays:int
var day:string

loop
put "How many days are there?"
get nodays

if nodays > 31 then
put "Please try again!"
end if
exit when nodays = 31 or nodays = 30 or nodays = 29 or nodays = 28

end loop

loop
put "What day does the month start on?"
get day

if day not = "Monday" or "Tuesday" or "Wednesday" or "Thursday" or "Friday" then




Please specify what version of Turing you are using
4.1.1

Author:  DemonWasp [ Fri Mar 04, 2011 10:29 am ]
Post subject:  RE:help - calender

You forgot the part where you're supposed to tell us what problem you're having. Without that, we have no idea what you want help with.

Author:  whoareyou [ Fri Mar 04, 2011 3:26 pm ]
Post subject:  RE:help - calender

i tried testing that code that i have there before i move on, but it says operands of bollean operators must be boolean and "Tuesday" is highlighted.

Author:  Tony [ Fri Mar 04, 2011 3:45 pm ]
Post subject:  RE:help - calender

what does
code:

"Tuesday" or "Wednesday"

evaluate to?

Author:  whoareyou [ Fri Mar 04, 2011 4:00 pm ]
Post subject:  Re: RE:help - calender

Tony @ Fri Mar 04, 2011 3:45 pm wrote:
what does
code:

"Tuesday" or "Wednesday"

evaluate to?


its a string, so if the user inputs Tuesday, then it should store than in "day"

Author:  Tony [ Fri Mar 04, 2011 4:03 pm ]
Post subject:  RE:help - calender

I meant as appears in your code
code:

if "Tuesday" or "Wednesday" then
   put "what is this?"
end if

Author:  whoareyou [ Fri Mar 04, 2011 4:06 pm ]
Post subject:  RE:help - calender

if the user enters monday, or tuesday, or wednesday ... etc, then it should store it in the variable day.

if not, i have to add in that it is invalid, and it should repeat that part until the user enters one of the 7 possible values for days.

Author:  Insectoid [ Fri Mar 04, 2011 4:13 pm ]
Post subject:  RE:help - calender

That's not the way conditionals work. You need to compare each individual day to the input.

code:

if day != "monday" and day != "tuesday" and...

Author:  Tony [ Fri Mar 04, 2011 4:17 pm ]
Post subject:  RE:help - calender

what's strange is that boolean operators were used correctly above
code:

exit when nodays = 31 or nodays = 30 or nodays = 29 or nodays = 28

Author:  whoareyou [ Fri Mar 04, 2011 4:19 pm ]
Post subject:  RE:help - calender

maybe its because i used a string. i think i need to change it to an integer, and associate each day with a number.

Author:  DemonWasp [ Fri Mar 04, 2011 4:21 pm ]
Post subject:  RE:help - calender

What Tony is hinting at is that that condition isn't doing what you think it is. The error message is actually pretty direct here. It's telling you that the operator "or" only works on booleans. That means that the value to the left and the value to the right must BOTH be booleans.

When Turing looks at your code file, it sees it like this:

if - starting an if-statement, good
day not= "Monday" - conditional, returns boolean, good
or "Tuesday" - what's that supposed to mean?!

What you really mean is this:
if not ( day = "Monday" or day = "Tuesday" or ... ) then

This gives you the logic you are after. First, you compare the value in day against each of "Monday"..."Friday"; if it matches any of those values, then the contents of the parenthesis returns true. You then negate that result with "not" and end up with true if and only if the value in day is NOT one of "Monday"..."Tuesday".

Author:  whoareyou [ Fri Mar 04, 2011 5:26 pm ]
Post subject:  Re: RE:help - calender

DemonWasp @ Fri Mar 04, 2011 4:21 pm wrote:
What Tony is hinting at is that that condition isn't doing what you think it is. The error message is actually pretty direct here. It's telling you that the operator "or" only works on booleans. That means that the value to the left and the value to the right must BOTH be booleans.

When Turing looks at your code file, it sees it like this:

if - starting an if-statement, good
day not= "Monday" - conditional, returns boolean, good
or "Tuesday" - what's that supposed to mean?!

What you really mean is this:
if not ( day = "Monday" or day = "Tuesday" or ... ) then

This gives you the logic you are after. First, you compare the value in day against each of "Monday"..."Friday"; if it matches any of those values, then the contents of the parenthesis returns true. You then negate that result with "not" and end up with true if and only if the value in day is NOT one of "Monday"..."Tuesday".


THAT WAS CORRECT! :O
Thank you!

So by using your technique, my code now works and looks like this. However, it is not finished. If i run into more problems, i'll be sure to post back here!

Turing:


var numberof:int
var day:string

loop
put "How many days are there?"
get numberof

if numberof > 31 or numberof < 28 then
put "Please try again!"
end if
exit when numberof = 31 or numberof = 30 or numberof = 29 or numberof = 28

end loop

loop
put "What day does the month start on?"
get day

if day = "Monday" or day = "Tuesday" or day = "Wednesday" or day = "Thursday" or day = "Friday" then
else
put "That is not a valid day!"

end if
exit when day = "Monday" or day = "Tuesday" or day = "Wednesday" or day = "Thursday" or day = "Friday"
end loop



Very long just for the beginning!

Author:  whoareyou [ Fri Mar 04, 2011 6:08 pm ]
Post subject:  RE:help - calender

how should align the numbers so that they appear under each appropriate day ?

Author:  Tony [ Fri Mar 04, 2011 6:23 pm ]
Post subject:  RE:help - calender

Using spaces. Documentation for put might have a shortcut for that, but it's the same idea.

Author:  whoareyou [ Fri Mar 04, 2011 6:38 pm ]
Post subject:  Re: help - calender

My code so far:

Turing:


setscreen ("text")

var nodays:int
var day:string
var daycount:int:=0
loop
put "How many days are there?"
get nodays

if nodays > 31 or nodays < 28 then
put "Please try again!"
end if
exit when nodays = 31 or nodays = 30 or nodays = 29 or nodays = 28

end loop

loop
put "What day does the month start on?"
get day

if day = "Monday" or day = "Tuesday" or day = "Wednesday" or day = "Thursday" or day = "Friday"
 or day = "monday" or day = "tuesday" or day = "wednesday" or day = "thursday" or day = "friday"
then
else
put "That is not a valid day!"

end if
exit when day = "Monday" or day = "Tuesday" or day = "Wednesday" or day = "Thursday" or day = "Friday"
or day = "monday" or day = "tuesday" or day = "wednesday" or day = "thursday" or day = "friday"
end loop


put skip
var d1, d2, d3, d4, d5, d6, d7 : string
d1 := "SUN"
d2 := "MON"
d3 := "TUES"
d4 := "WED"
d5 := "THURS"
d6 := "FRI"
d7 := "SAT"

put d1, " ":10, d2, " ":10, d3, " ":10, d4, " ":10, d5, " ":10, d6, " ":10, d7, " ":10

if day = "Monday" or day = "monday" then
for i: 1 .. nodays
put " ", i:10..
daycount += 1



if (daycount mod 7 = 0) then
put ""
daycount := 0
end if

end for
end if



I have it set up so that it will run when i enter 30 days and the start date is on monday.
however, the numbers won't allign under the right date properly.

i dont underestand how i can use spaces, because then each number will get spaces and it will go off the calender.
also, it won't go onto the next line once it reaches saturday.

Author:  ihsh [ Fri Mar 04, 2011 8:00 pm ]
Post subject:  Re: help - calender

You mean that when the dates have different amount of digits, they will mess up the overall alignment?

Since the numbers only go from 1-31, you can use a simple if-structure to determine how many digits the number has and adjust the spacing according the length of the number . A code example (this is right aligned):
Turing:

for i : 1 .. nodays
        if i < 10 then
            put " " : 12, i ..
        else
            put " " : 11, i ..
        end if

        %rest of for loop....
        %...
end for


Also, keep in mind that the widths of the columns of your calender are inconsistent. For example, the "THURS" clolumn has two more characters than the "MON" column. This will make it harder to align as you'll need more if structures.

You might consider changing the numerical values of this line to make sure that the widths of the columns are the same :
Quote:
put d1, " " : 10, d2, " " : 10, d3, " " : 10, d4, " " : 10, d5, " " : 10, d6, " " : 10, d7, " " : 10

Author:  whoareyou [ Fri Mar 04, 2011 9:23 pm ]
Post subject:  RE:help - calender

it still isnt skipping to the next line. it just goes straight across.

Author:  Tony [ Fri Mar 04, 2011 9:30 pm ]
Post subject:  RE:help - calender

You can use multiple put statements. Use .. at the end to keep everything on the same line. put an empty string "" to go to the next line.

Author:  whoareyou [ Fri Mar 04, 2011 10:04 pm ]
Post subject:  Re: help - calender

i dont seem to be doing something right. maybe im not interpretting your suggestions right ...?
here is my code, so you can take a look.

Turing:


setscreen ("text")

var nodays:int
var day:string
var daycount:int:=0
loop
put "How many days are there?"
get nodays

if nodays > 31 or nodays < 28 then
put "Please try again!"
end if
exit when nodays = 31 or nodays = 30 or nodays = 29 or nodays = 28

end loop

loop
put "What day does the month start on?"
get day

if day = "Monday" or day = "Tuesday" or day = "Wednesday" or day = "Thursday" or day = "Friday"
 or day = "monday" or day = "tuesday" or day = "wednesday" or day = "thursday" or day = "friday"
then
else
put "That is not a valid day!"

end if
exit when day = "Monday" or day = "Tuesday" or day = "Wednesday" or day = "Thursday" or day = "Friday"
or day = "monday" or day = "tuesday" or day = "wednesday" or day = "thursday" or day = "friday"
end loop


put skip
var d1, d2, d3, d4, d5, d6, d7 : string
d1 := "SUN"
d2 := "MON"
d3 := "TUES"
d4 := "WED"
d5 := "THURS"
d6 := "FRI"
d7 := "SAT"

put d1:10, d2:12, d3:14, d4:14, d5:14, d6:14, d7:12

if day = "Monday" or day = "monday" then
for i: 1 .. 6
put i:11..
end for
put skip

for i: 7 .. nodays

put i:10..
daycount += 1
if (daycount = 7) then
put skip
daycount := 0



end if
end for
end if



also, i've included a sample program of how the program should output. im testing mine with the days set to 30 and the starting date is monday.
see how it goes from 1-6 in the first row, and then starts from 7 AT sunday in the second row? i don't understand why my program isn't doing that.

Author:  ihsh [ Fri Mar 04, 2011 10:30 pm ]
Post subject:  RE:help - calender

That doesn't align because the amount of spacing for the Sunday column is different.

On all the other days, the spacing should be 10-15, whereas on Sundays the spacing is 3.

Therefore if you want to use the spacing command, you will need to use an if-structure to check if it's a Sunday. If so, you'll have to use a different amount of spacing. In your case, you will just need to check if daycount=1

Author:  whoareyou [ Sat Mar 05, 2011 2:34 pm ]
Post subject:  Re: help - calender

now, i've got the sunday column all lined up, but when the numbers enter double digits, they get out of order.
also, the first row of numbers dont line up with the rest Sad
i have to fix the column heads too; they aren't lined up with the rest of the numbers.

Turing:


setscreen ("text")

var nodays:int
var day:string
var daycount:int:=0
loop
put "How many days are there?"
get nodays

if nodays > 31 or nodays < 28 then
put "Please try again!"
end if
exit when nodays = 31 or nodays = 30 or nodays = 29 or nodays = 28

end loop

loop
put "What day does the month start on?"
get day

if day = "Monday" or day = "Tuesday" or day = "Wednesday" or day = "Thursday" or day = "Friday"
 or day = "monday" or day = "tuesday" or day = "wednesday" or day = "thursday" or day = "friday"
then
else
put "That is not a valid day!"

end if
exit when day = "Monday" or day = "Tuesday" or day = "Wednesday" or day = "Thursday" or day = "Friday"
or day = "monday" or day = "tuesday" or day = "wednesday" or day = "thursday" or day = "friday"
end loop


put skip
var d1, d2, d3, d4, d5, d6, d7 : string
d1 := "SUN"
d2 := "MON"
d3 := "TUES"
d4 := "WED"
d5 := "THURS"
d6 := "FRI"
d7 := "SAT"

put d1:10, d2:12, d3:14, d4:14, d5:14, d6:14, d7:12

if day = "Monday" or day = "monday" then
for i: 1 .. 6
put " ", i:10..
end for
put skip

for i: 7 .. nodays
daycount += 1

if (daycount = 1) then
put i..
end if

if (daycount mod 7 = 0) then
put i:10..
put skip
daycount := 0
end if

if (daycount > 1) and (daycount < 7) then
put i:10..
end if




end for
end if



: