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

Username:   Password: 
 RegisterRegister   
 elseif problem
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Sausage49




PostPosted: Tue Oct 18, 2016 3:58 pm   Post subject: elseif problem

What is it you are trying to achieve?
Create a program that collects basic information about the user


What is the problem you are having?
For my computer science class we are required to make a program that collects basic info about the user. When making an elseif list the program wont compile if there is an else statement inside of an elseif statement.


Describe what you have tried to solve this problem
I tried making each item (month in this case) a seperate if list with an "endif" at the end of each item. The program compiled but didn't do what was expected. I also tried the program with only one if item (month = 01) and therefore no need for an elseif statement and the program worked flawlessly.



Turing:


if month = 01 and (day < 32) then
   
    else
        put "Error: you entered an invalid day. Re-enter day: "..
   
        get day
   
elseif month = 02 and (day < 29) then
   
    else
        put "Error: you entered an invalid day. Re-enter day: "..
   
        get day
   
elseif month = 03 and (day < 32) then
 
    else
        put "Error: you entered an invalid day. Re-enter day: "..
   
        get day
   
elseif month = 04 and (day < 31) then
   
    else
        put "Error: you entered an invalid day. Re-enter day: "..
   
        get day
   
elseif month = 05 and (day < 32) then
   
    else
        put "Error: you entered an invalid day. Re-enter day: "..
   
        get day
   
elseif month = 06 and (day < 30) then
   
    else
        put "Error: you entered an invalid day. Re-enter day: "..
   
        get day
   
elseif month = 07 and (day < 32) then
   
    else
        put "Error: you entered an invalid day. Re-enter day: "..
   
        get day
   
elseif month = 08 and (day < 32) then
   
    else
        put "Error: you entered an invalid day. Re-enter day: "..
   
        get day
       
elseif month = 09 and (day < 31) then
   
    else
        put "Error: you entered an invalid day. Re-enter day: "..
   
        get day
   
elseif month = 10 and (day < 32) then
   
    else
        put "Error: you entered an invalid day. Re-enter day: "..
   
        get day
   
elseif month = 11 and (day < 31) then
   
    else
        put "Error: you entered an invalid day. Re-enter day: "..
   
        get day
   
elseif month = 12 and (day < 32) then
   
    else
        put "Error: you entered an invalid day. Re-enter day: "..
   
        get day

endif



Please specify what version of Turing you are using
<Open turing 4.1.2>



Assignment2_ Sean.t
 Description:
File in question

Download
 Filename:  Assignment2_ Sean.t
 Filesize:  2.85 KB
 Downloaded:  119 Time(s)


Assignment2_ Sean.t
 Description:
Program in question

Download
 Filename:  Assignment2_ Sean.t
 Filesize:  2.85 KB
 Downloaded:  117 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Sausage49




PostPosted: Tue Oct 18, 2016 4:01 pm   Post subject: Re: elseif problem

Basically i want to know why it wont compile now and what i can do to fix it.
Insectoid




PostPosted: Tue Oct 18, 2016 4:47 pm   Post subject: RE:elseif problem

You can only have one else per if. It must be at the end of the if.
Sausage49




PostPosted: Tue Oct 18, 2016 5:10 pm   Post subject: Re: elseif problem

I figured it our, if anyone else wants to know i made each item a separate if statementand it seems to work, ill copy and paste if below

loop
exit when day < 32

if month = 01 and (day < 32) then

else
put "Error: you entered an invalid day. Re-enter day: "..

get day
end if
end loop

loop
exit when day < 29

if month = 02 and (day < 29) then

else
put "Error: you entered an invalid day. Re-enter day: "..

get day

end if

end loop

loop
exit when day < 32

if month = 03 and (day < 32) then

else
put "Error: you entered an invalid day. Re-enter day: "..

get day

end if

end loop

loop
exit when day < 31

if month = 04 and (day < 31) then

else
put "Error: you entered an invalid day. Re-enter day: "..

get day

end if

end loop

loop
exit when day < 32

if month = 05 and (day < 32) then

else
put "Error: you entered an invalid day. Re-enter day: "..

get day

end if

end loop

loop
exit when day < 31

if month = 06 and (day < 31) then

else
put "Error: you entered an invalid day. Re-enter day: "..

get day

end if

end loop

loop
exit when day < 32

if month = 07 and (day < 32) then

else
put "Error: you entered an invalid day. Re-enter day: "..

get day

end if

end loop

loop
exit when day < 31

if month = 08 and (day < 31) then

else
put "Error: you entered an invalid day. Re-enter day: "..

get day

end if

end loop

loop
exit when day < 31

if month = 09 and (day < 31) then

else
put "Error: you entered an invalid day. Re-enter day: "..

get day

end if

end loop

loop
exit when day < 32

if month = 10 and (day < 32) then

else
put "Error: you entered an invalid day. Re-enter day: "..

get day

end if

end loop

loop
exit when day < 31

if month = 11 and (day < 31) then

else
put "Error: you entered an invalid day. Re-enter day: "..

get day

end if

end loop

loop
exit when day < 32

if month = 12 and (day < 32) then

else
put "Error: you entered an invalid day. Re-enter day: "..

get day
TipsyCzar




PostPosted: Wed Oct 19, 2016 11:42 am   Post subject: Re: elseif problem

You made a syntax error. The command is spelled elsif, while you spelled it as elseif (there's no e after the s).
Insectoid




PostPosted: Wed Oct 19, 2016 2:01 pm   Post subject: RE:elseif problem

You've got a lot of ifs in there that do nothing.

code:
if month = 11 and (day < 31) then

else
put "Error: you entered an invalid day. Re-enter day: "..

get day

end if


That if does nothing if it returns true. Technically it's not a bug since you don't want it to do anything unless it returns false, which is why you have the else. This is logically correct, but extremely messy. Using de morgan's law, you can re-write this so that it does not require an else.

You want to 'put error' ONLY WHEN the month is NOT eleven OR the day is GREATER THAN 31. It should look something like this when it's done:

code:
if <fill this in> then
    print "Invalid day"
    get day
end if


That's a lot neater now isn't it? Half as many line of code and inifinitely more legible!

There's a lot more you can do to clean up this code, but we'll keep it simple.
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: