Hi all, Im am studying pascal/delphi at AS Level and just started.
I need some help putting an 'If' function inside a 'case' function:
case Month of
1: MonthName:='January'; if Date>31 then writeln('This is not possible')
2: MonthName:= 'February'; if Date>29 then writeln('This is not possible')
I just want to ask how I would correct this for it to work. Basically I am trying to create a program that will display an error message if say '32' for Date was entered, since there aren't 32 days in any month.
I get the errors of 'Missing Operator or semicolon' and 'Constant expression expected', but I dont get it:P
Any help appreciated.
Hi, Im a newb. Got a quick question...
Started by 2710, Sep 26 2008 08:57 AM
10 replies to this topic
#1
Posted 26 September 2008 - 08:57 AM
|
|
|
#2
Posted 26 September 2008 - 10:32 AM
Use begin/end tags around your code for each case.
#3
Posted 27 September 2008 - 06:30 AM
case Month of
begin
1: MonthName:='January';if Date>31 then writeln('This is not possible')
2: MonthName:= 'February'; if Date>31 then writeln('This is not possible')
......
else
Begin
writeln('Month Number has to be in the range of 1 and 12');
error:=true;
end;
end; {case}
Still doesnt work....
begin
1: MonthName:='January';if Date>31 then writeln('This is not possible')
2: MonthName:= 'February'; if Date>31 then writeln('This is not possible')
......
else
Begin
writeln('Month Number has to be in the range of 1 and 12');
error:=true;
end;
end; {case}
Still doesnt work....
#4
Posted 27 September 2008 - 09:06 AM
Here ya go, buddie:D
case Month of
1: begin
MonthName:='January';
if Date>31 then writeln('This is very possible');
end;
2: begin
MonthName:= 'February';
if Date>31 then writeln('This is absolutely possible');
end;
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#5
Posted 27 September 2008 - 01:35 PM
Thanks ^^ It works. But now I got another problem...sorry ^^'
Anyways:
case Month of
1:MonthName:='January';
2:begin;
MonthName:= 'February';
if Date>29 then writeln('You cannot have more than 29 days in February');
error:=true
end;
3:MonthName:= 'March';
4:begin
MonthName:= 'April';
if Date>30 then writeln('You cannot have more than 30 days in April');
error:=true
end;
As you can see. I have added the error:=true. This is to prevent the program running the rest of the program even though it has displayed the error message. It works well...if I enter 31 days in February, it will display the message and terminate the program. HOWEVER, if I enter a valid date, as in 25th Feb...it doesnt show up.
I am assuming that when I type in 'error:=true' it is taking all the statement to be an error from the 'begin' to the 'end':
4:begin
MonthName:= 'April';
if Date>30 then writeln('You cannot have more than 30 days in April');
error:=true
end;
So I try various diff. ways to seperate the right form wrong:
11:MonthName:='November' ;
begin
if Date>30
then writeln('You cannot have more than 30 days in November');
error:=true
end;
and
11:begin
MonthName:='November' ;
if Date>30
then writeln('You cannot have more than 30 days in November');
error:=true
else
error:=false
end;
...but they dont work ><.
Thanks. Any help appreciated.
Anyways:
case Month of
1:MonthName:='January';
2:begin;
MonthName:= 'February';
if Date>29 then writeln('You cannot have more than 29 days in February');
error:=true
end;
3:MonthName:= 'March';
4:begin
MonthName:= 'April';
if Date>30 then writeln('You cannot have more than 30 days in April');
error:=true
end;
As you can see. I have added the error:=true. This is to prevent the program running the rest of the program even though it has displayed the error message. It works well...if I enter 31 days in February, it will display the message and terminate the program. HOWEVER, if I enter a valid date, as in 25th Feb...it doesnt show up.
I am assuming that when I type in 'error:=true' it is taking all the statement to be an error from the 'begin' to the 'end':
4:begin
MonthName:= 'April';
if Date>30 then writeln('You cannot have more than 30 days in April');
error:=true
end;
So I try various diff. ways to seperate the right form wrong:
11:MonthName:='November' ;
begin
if Date>30
then writeln('You cannot have more than 30 days in November');
error:=true
end;
and
11:begin
MonthName:='November' ;
if Date>30
then writeln('You cannot have more than 30 days in November');
error:=true
else
error:=false
end;
...but they dont work ><.
Thanks. Any help appreciated.
#6
Posted 27 September 2008 - 01:40 PM
Shouldn't you use a global variable to set the error, or is that what you're doing? Post the whole program, will you?
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#7
Posted 27 September 2008 - 02:22 PM
Ok sorry:
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
Suffix, MonthName: String;
Date: Integer;
Month: Integer;
Year: Integer;
Year2:string;
error:boolean;
begin
{ TODO -oUser -cConsole Main : Insert code here }
error:= false;
writeln('Please Enter Date: ');
Readln(Date);
writeln('Please Enter Month Number: ');
Readln(Month);
Writeln('Please Enter last two digits of Year: ');
Readln(Year);
case Date of
4..20, 24..30:Suffix:='th';
1,21,31:Suffix:='st';
2,22:Suffix:='nd';
3,23:Suffix:='rd'
else
begin
writeln('Date has to be within the range of 1 and 31');
error:= true;
end;
end;
case Month of
1:MonthName:='January';
2:begin;
MonthName:= 'February';
if Date>29 then writeln('You cannot have more than 29 days in February');
error:=true
end;
3:MonthName:= 'March';
4:begin
MonthName:= 'April';
if Date>30 then writeln('You cannot have more than 30 days in April');
error:=true
end;
5:MonthName:= 'May';
6:begin
MonthName:= 'June' ;
if Date>30 then writeln('You cannot have more than 30 days in June');
error:=true
end;
7:MonthName:= 'July' ;
8:MonthName:= 'August' ;
9:begin
MonthName:= 'September';
if Date>30 then writeln('You cannot have more than 30 days in September');
error:=true
end;
10:MonthName:='October' ;
11:begin MonthName:='November' ;
if Date>30
then writeln('You cannot have more than 30 days in November');
error:=true
end;
12:MonthName:='December'
else
Begin
writeln('Month Number has to be in the range of 1 and 12');
error:=true;
end;
end; {case}
case Year of
00..30:Year2:='20';
31..99:Year2:='19'
end;
if error = false
then
begin
if Year < 10
then writeln (Date, Suffix, Monthname,Year2 ,'0',Year)
else
if Year >=10
then writeln (Date, Suffix, Monthname,Year2 ,Year)
else writeln('Last 2 Digits has to be within 00 and 99');
end;
readln;
end.
Edited by Jaan, 29 September 2008 - 11:49 AM.
Use code tags when you're posting your codes! (I hope I added 'em right)
#8
Posted 27 September 2008 - 02:40 PM
First:
I'm quite sure I know what you're doing wrong. You never get "error = false":
Like this:
"writeln('You cannot...');"
Your poor "error:=true;" statement is always executed... See?
case Month of
1:MonthName:='January';
2:begin;
No ";" after begin :pI'm quite sure I know what you're doing wrong. You never get "error = false":
if error = false
then
begin
if Year < 10
then writeln (Date, Suffix, Monthname,Year2 ,'0',Year)
else
if Year >=10
then writeln (Date, Suffix, Monthname,Year2 ,Year)
else writeln('Last 2 Digits has to be within 00 and 99');
end;So this last piece of code will never be done, eh? First of all I would initialize error to false. Then I would seriously consider putting begin and end in those if statements up in your case-of... ;)Like this:
case Month of
1:MonthName:='January';
2:begin;
MonthName:= 'February';
if Date>29 then
[COLOR=Red]begin[/COLOR]
writeln('You cannot have more than 29 days in February');
error:=true
[COLOR=Red]end;[/COLOR]
end;
The way your code is written now, if Date>29 then the computer only does "writeln('You cannot...');"
Your poor "error:=true;" statement is always executed... See?
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#9
Posted 28 September 2008 - 02:05 PM
THANKS!!! :D Works ^^
#10
Posted 29 September 2008 - 08:37 AM
2710 said:
I am assuming that when I type in 'error:=true' it is taking all the statement to be an error from the 'begin' to the 'end':
4:begin
MonthName:= 'April';
if Date>30 then writeln('You cannot have more than 30 days in April');
error:=true
end;
4:begin
MonthName:= 'April';
if Date>30 then writeln('You cannot have more than 30 days in April');
error:=true
end;
Here is your problem
4:[B]begin[/B]
MonthName:= 'April';
if Date>30 then writeln('You cannot have more than 30 days in April');
error:=true
[B] end;[/B]
error:=true is NOT part of the if/then statement. You need it to be:
4:[B]begin[/B]
MonthName:= 'April';
if Date>30 then
begin
writeln('You cannot have more than 30 days in April');
error:=true
end;
[B] end;[/B]
#11
Posted 01 October 2008 - 06:08 AM
Uh, yeah. :P Just what I said, but yeah!
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa


Sign In
Create Account


Back to top









