Board index » delphi » problem with my case statement

problem with my case statement

Here is my program besides for the bad format and way of doing things
incorrectly what is the program with the case statements? :-) thanx
-yoyo2904

PROGRAM calculator;
USES crt;
VAR
   Num1 : double;
   Num2 : double;
   Operation : char;
BEGIN
     writeln( 'Welcome to Yonatans calculator' );
     writeln( 'Enter first number: ' );
     readln( Num1 );
     writeln( 'Enter Operation: ' );
     readln( Operation );
     writeln( 'Enter second number' );
     readln( Num2 );
        case operation of
        '+' : writeln(Num1+Num2);
        '-' : writeln(Num1-Num2);
        '/' : case num2 of
        '0' : writeln('ERROR: DIVIDE BY ZERO');
        else : writeln(num1/num2);
        end;
        '*' : writeln(num1*num2);
        else : writeln('ERROR');
        end;
        readkey;
     END.

--
To program or not to program
To program a simple game
To program for the love
To program from above
just make sure you aim to reach the goal
or else you will pay the toll
when your program is worse than bad food
Its all about the mood
Programming is fun I love it a ton
SO PROGRAM!!

Sent via Deja.com
http://www.deja.com/

 

Re:problem with my case statement


yoyo2904 <yoyo2...@aol.com> schrieb folgenden Unsinn:
Quote
> Here is my program besides for the bad format and way of doing things
> incorrectly what is the program with the case statements? :-) thanx
> -yoyo2904

> PROGRAM calculator;
> USES crt;
> VAR
>    Num1 : double;
>    Num2 : double;
>    Operation : char;
> BEGIN
>      writeln( 'Welcome to Yonatans calculator' );
>      writeln( 'Enter first number: ' );
>      readln( Num1 );
>      writeln( 'Enter Operation: ' );
>      readln( Operation );
>      writeln( 'Enter second number' );
>      readln( Num2 );
>    case operation of
>         '+' : writeln(Num1+Num2);
>            '-' : writeln(Num1-Num2);
>         '/' : case num2 of
>         '0' : writeln('ERROR: DIVIDE BY ZERO');

          ^^^
ERROR 1: type mismatch. Unlike in C, '0' is not an integer, and in C, it
would be 48.
ERROR 2: please indent correctly. (Correctly means: same level of
structure, same indentation, and each level has at least one space at the
beginning more that the last one)

Quote
>         else : writeln(num1/num2);
>         end;
>         '*' : writeln(num1*num2);
>         else : writeln('ERROR');
>    end;
>         readkey;
>      END.

--
Nuper erat medicus, nunc est vispillo, Diaulus:
Quod vispillo facit, fecerat et medicus.

Re:problem with my case statement


In article <slrn96vck9.po.rpol...@rebounce.rpolzer-lx>,

Quote
  grauezel...@gibts.net wrote:
> yoyo2904 <yoyo2...@aol.com> schrieb folgenden Unsinn:
> > Here is my program besides for the bad format and way of doing
things
> > incorrectly what is the program with the case statements? :-) thanx
> > -yoyo2904

> > PROGRAM calculator;
> > USES crt;
> > VAR
> >    Num1 : double;
> >    Num2 : double;
> >    Operation : char;
> > BEGIN
> >      writeln( 'Welcome to Yonatans calculator' );
> >      writeln( 'Enter first number: ' );
> >      readln( Num1 );
> >      writeln( 'Enter Operation: ' );
> >      readln( Operation );
> >      writeln( 'Enter second number' );
> >      readln( Num2 );
> >       case operation of
> >         '+' : writeln(Num1+Num2);
> >       '-' : writeln(Num1-Num2);
> >         '/' : case num2 of
> >         'instead of '0' I should put '48' here?' : writeln('ERROR:
DIVIDE BY ZERO');
>           ^^^
> ERROR 1: type mismatch. Unlike in C, '0' is not an integer, and in C,
it
> would be 48.
> ERROR 2: please indent correctly. (Correctly means: same level of
> structure, same indentation, and each level has at least one space at
the
> beginning more that the last one)
> >         else : writeln(num1/num2);
> >         end;
> >         '*' : writeln(num1*num2);
> >         else : writeln('ERROR');
> >       end;
> >         readkey;
> >      END.

> --
> Nuper erat medicus, nunc est vispillo, Diaulus:
> Quod vispillo facit, fecerat et medicus.

--
To program or not to program
To program a simple game
To program for the love
To program from above
just make sure you aim to reach the goal
or else you will pay the toll
when your program is worse than bad food
Its all about the mood
Programming is fun I love it a ton
SO PROGRAM!!

Sent via Deja.com
http://www.deja.com/

Re:problem with my case statement


You cannot use a case clause to test Num2 for zero
because Num2 is not an ordered type variable.
Besides it would be very poor programming to use
"case" to test for just one special case (Num2 = 0).

Instead you should either code
if num2 = 0 then ..... else ..... ;
or preferably you should get used to the try .... except .... ;
statement structure:

From the online help (do look it up!):

Exceptions are handled within try...except statements. For example,

try

  X := Y/Z;
except
  on EZeroDivide do HandleZeroDivide;
end;

This statement attempts to divide Y by Z, but calls a routine named
HandleZeroDivide if an EZeroDivide exception is raised.

good luck!
Sven

Quote
"yoyo2904" <yoyo2...@aol.com> wrote in message

news:94odhr$l05$1@nnrp1.deja.com...
Quote
> In article <slrn96vck9.po.rpol...@rebounce.rpolzer-lx>,
>   grauezel...@gibts.net wrote:
> > yoyo2904 <yoyo2...@aol.com> schrieb folgenden Unsinn:
> > > Here is my program besides for the bad format and way of doing
> things
> > > incorrectly what is the program with the case statements? :-) thanx
> > > -yoyo2904

> > > PROGRAM calculator;
> > > USES crt;
> > > VAR
> > >    Num1 : double;
> > >    Num2 : double;
> > >    Operation : char;
> > > BEGIN
> > >      writeln( 'Welcome to Yonatans calculator' );
> > >      writeln( 'Enter first number: ' );
> > >      readln( Num1 );
> > >      writeln( 'Enter Operation: ' );
> > >      readln( Operation );
> > >      writeln( 'Enter second number' );
> > >      readln( Num2 );
> > > case operation of
> > >         '+' : writeln(Num1+Num2);
> > >      '-' : writeln(Num1-Num2);
> > >         '/' : case num2 of
> > >         'instead of '0' I should put '48' here?' : writeln('ERROR:
> DIVIDE BY ZERO');
> >           ^^^
> > ERROR 1: type mismatch. Unlike in C, '0' is not an integer, and in C,
> it
> > would be 48.
> > ERROR 2: please indent correctly. (Correctly means: same level of
> > structure, same indentation, and each level has at least one space at
> the
> > beginning more that the last one)
> > >         else : writeln(num1/num2);
> > >         end;
> > >         '*' : writeln(num1*num2);
> > >         else : writeln('ERROR');
> > > end;
> > >         readkey;
> > >      END.

> > --
> > Nuper erat medicus, nunc est vispillo, Diaulus:
> > Quod vispillo facit, fecerat et medicus.

> --
> To program or not to program
> To program a simple game
> To program for the love
> To program from above
> just make sure you aim to reach the goal
> or else you will pay the toll
> when your program is worse than bad food
> Its all about the mood
> Programming is fun I love it a ton
> SO PROGRAM!!

> Sent via Deja.com
> http://www.deja.com/

Re:problem with my case statement


Sven Pran <Sven.P...@alcatel.no> schrieb folgenden Unsinn:

Quote
> You cannot use a case clause to test Num2 for zero
> because Num2 is not an ordered type variable.

Sorry, I did not look at the var declarations... double is not integer!

Quote
> Besides it would be very poor programming to use
> "case" to test for just one special case (Num2 = 0).

Of course.

Quote
> Instead you should either code
> if num2 = 0 then ..... else ..... ;

The best possibility when try...except is unavailable

Quote
> or preferably you should get used to the try .... except .... ;
> statement structure:

> From the online help (do look it up!):

> Exceptions are handled within try...except statements. For example,

> try

>   X := Y/Z;
> except
>   on EZeroDivide do HandleZeroDivide;
> end;

> This statement attempts to divide Y by Z, but calls a routine named
> HandleZeroDivide if an EZeroDivide exception is raised.

But only in Delphi or FPC. He will see if he has the correct version in
the help (except is there => he can use this).

--
Nuper erat medicus, nunc est vispillo, Diaulus:
Quod vispillo facit, fecerat et medicus.

Re:problem with my case statement


yoyo2904 <yoyo2...@aol.com> schrieb folgenden Unsinn:

Quote
> In article <slrn96vck9.po.rpol...@rebounce.rpolzer-lx>,
>   grauezel...@gibts.net wrote:
> > >         '0' : writeln('ERROR: DIVIDE BY ZERO');
> >           ^^^
> > ERROR 1: type mismatch. Unlike in C, '0' is not an integer, and in C,
> it
> > would be 48.
> Instead of '0' should I write 48 here?

No. You should instead use 0, without the 's, just as number! I meant that
your error is caught by the compiler, why a C compiler would generate a
program using 48 for '0' which should not be what you want. You are lucky
that you have Pascal which catches such errors.

BTW, this should be done in an if statement instead.

--
Nuper erat medicus, nunc est vispillo, Diaulus:
Quod vispillo facit, fecerat et medicus.

Re:problem with my case statement


Quote
"Rudolf Polzer" <rpol...@web.de> wrote in message

news:slrn97084p.nc.rpolzer@rebounce.rpolzer-lx...

Quote
> Sven Pran <Sven.P...@alcatel.no> schrieb folgenden Unsinn:
> > You cannot use a case clause to test Num2 for zero
> > because Num2 is not an ordered type variable.

> Sorry, I did not look at the var declarations... double is not integer!

> > Besides it would be very poor programming to use
> > "case" to test for just one special case (Num2 = 0).

> Of course.

> > Instead you should either code
> > if num2 = 0 then ..... else ..... ;

> The best possibility when try...except is unavailable

Certainly! I am so used to Delphi I simply forgot many features
are not available in Pascal as such. (Anyway I did point to the if
alternative first).

Thanks for removing this possible confusion

regards Sven

Quote

> > or preferably you should get used to the try .... except .... ;
> > statement structure:

> > From the online help (do look it up!):

> > Exceptions are handled within try...except statements. For example,

> > try

> >   X := Y/Z;
> > except
> >   on EZeroDivide do HandleZeroDivide;
> > end;

> > This statement attempts to divide Y by Z, but calls a routine named
> > HandleZeroDivide if an EZeroDivide exception is raised.

> But only in Delphi or FPC. He will see if he has the correct version in
> the help (except is there => he can use this).

> --
> Nuper erat medicus, nunc est vispillo, Diaulus:
> Quod vispillo facit, fecerat et medicus.

Other Threads