Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Hi, Im a newb. Got a quick question...

pascal

  • Please log in to reply
10 replies to this topic

#1 2710

2710

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 110 posts

Posted 26 September 2008 - 08:57 AM

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.
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 26 September 2008 - 10:32 AM

Use begin/end tags around your code for each case.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 2710

2710

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 110 posts

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....
  • 0

#4 marwex89

marwex89

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2857 posts

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;

  • 0
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#5 2710

2710

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 110 posts

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.
  • 0

#6 marwex89

marwex89

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2857 posts

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?
  • 0
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#7 2710

2710

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 110 posts

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)

  • 0

#8 marwex89

marwex89

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2857 posts

Posted 27 September 2008 - 02:40 PM

First:
case Month of
    1:MonthName:='January';
    2:begin;
No ";" after begin :P

I'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?
  • 0
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#9 2710

2710

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 110 posts

Posted 28 September 2008 - 02:05 PM

THANKS!!! :D Works ^^
  • 0

#10 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 29 September 2008 - 08:37 AM

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;


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]

  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#11 marwex89

marwex89

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2857 posts

Posted 01 October 2008 - 06:08 AM

Uh, yeah. :P Just what I said, but yeah!
  • 0
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa





Also tagged with one or more of these keywords: pascal

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download