Recent

Author Topic: Problem "Case For", after '9' Help!  (Read 8412 times)

Genofiend

  • Newbie
  • Posts: 5
Problem "Case For", after '9' Help!
« on: June 27, 2016, 12:18:07 am »
Code: Pascal  [Select][+][-]
  1. PROGRAM CICLE_CASE_FOR_MONTHS;
  2.  
  3. USES
  4.     CRT;
  5. VAR
  6.    OPTION:CHAR;
  7.    I:INTEGER;
  8.  
  9. BEGIN
  10. FOR I:= 1 TO 4 DO
  11.     BEGIN
  12.     CLRSCR;
  13.     GOTOXY(32,3);
  14.     WRITE('MONTHS OF THE YEAR');
  15.     GOTOXY(5,5);
  16.     WRITE('WRITE A NUMBER FROM 1 TO 12: ');
  17.     READLN(OPTION);
  18.     CASE OPTION OF
  19.     '1':
  20.         BEGIN
  21.         CLRSCR;
  22.         GOTOXY(32,12);
  23.         WRITE('JUNARY');
  24.         END;
  25.     '2':
  26.         BEGIN
  27.         CLRSCR;
  28.         GOTOXY(32,12);
  29.         WRITE('FEBRUARY');
  30.         END;
  31.     '3':
  32.         BEGIN
  33.         CLRSCR;
  34.         GOTOXY(32,12);
  35.         WRITE('MARCH');
  36.         END;
  37.     '4':
  38.         BEGIN
  39.         CLRSCR;
  40.         GOTOXY(32,12);
  41.         WRITE('APRIL');
  42.         END;
  43.     '5':
  44.         BEGIN
  45.         CLRSCR;
  46.         GOTOXY(32,12);
  47.         WRITE('MAY');
  48.         END;
  49.     '6':
  50.         BEGIN
  51.         CLRSCR;
  52.         GOTOXY(32,12);
  53.         WRITE('JUNE');
  54.         END;
  55.     '7':
  56.         BEGIN
  57.         CLRSCR;
  58.         GOTOXY(32,12);
  59.         WRITE('JULY');
  60.         END;
  61.      '8':
  62.         BEGIN
  63.         CLRSCR;
  64.         GOTOXY(32,12);
  65.         WRITE('AUGUST');
  66.         END;
  67.     '9':
  68.         BEGIN
  69.         CLRSCR;
  70.         GOTOXY(32,12);
  71.         WRITE('SEPTEMBER');
  72.         END;
  73.     '10':
  74.         BEGIN
  75.         CLRSCR;
  76.         GOTOXY(32,12);
  77.         WRITE('OCTOBER');
  78.         END;
  79.     '11':
  80.         BEGIN
  81.         CLRSCR;
  82.         GOTOXY(32,12);
  83.         WRITE('NOVEMBER');
  84.         END;
  85.     '12':
  86.         BEGIN
  87.         CLRSCR;
  88.         GOTOXY(32,12);
  89.         WRITE('DECEMBER');
  90.         END;
  91.      END;
  92.      READLN;
  93.      END;
  94. END.
  95.  
  96. ERRORS:
  97. 73 / 9 mesesd~1.pas
  98.  Error: Constant and CASE types do not match
  99. 73 / 9 mesesd~1.pas
  100.  Error: Ordinal expression expected
  101. 85 / 9 mesesd~1.pas
  102.  Error: duplicate case label
  103. 91 / 5 mesesd~1.pas
  104.  Fatal: String exceeds line
  105.  
  106.  
  107.  
  108.  
« Last Edit: June 27, 2016, 12:27:17 am by Genofiend »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Problem "Case For", after '9' Help!
« Reply #1 on: June 27, 2016, 12:23:35 am »
Does '10' fit a char for you?

Genofiend

  • Newbie
  • Posts: 5
Re: Problem "Case For", after '9' Help!
« Reply #2 on: June 27, 2016, 12:30:59 am »
Does '10' fit a char for you?

I really don't know what i have to do, i am new doing this.

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Problem "Case For", after '9' Help!
« Reply #3 on: June 27, 2016, 12:32:47 am »
Does '10' fit a char for you?

I really don't know what i have to do, i am new doing this.

change
 OPTION:CHAR;

to
OPTION: STRING;

Genofiend

  • Newbie
  • Posts: 5
Re: Problem "Case For", after '9' Help!
« Reply #4 on: June 27, 2016, 12:38:03 am »
Does '10' fit a char for you?

I really don't know what i have to do, i am new doing this.

change
 OPTION:CHAR;

to
OPTION: STRING;

I tried that before, but now i have 18 errors  %)

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Problem "Case For", after '9' Help!
« Reply #5 on: June 27, 2016, 12:44:24 am »
If that is your full source code I have no errors-

You're using Free Pascal Compiler right?

If you have more lines of code please attach the full source code.

Genofiend

  • Newbie
  • Posts: 5
Re: Problem "Case For", after '9' Help!
« Reply #6 on: June 27, 2016, 12:51:26 am »
If that is your full source code I have no errors-

You're using Free Pascal Compiler right?

If you have more lines of code please attach the full source code.
yes im using dev-pas 1.9.2(Freepascal), and yes that is my full code

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Problem "Case For", after '9' Help!
« Reply #7 on: June 27, 2016, 01:03:26 am »
And you must use that or you can switch to, for example Lazarus?

In Lazarus you can do Project > New Project > Simple program and paste your code.

I have no errors in that environment.

I've downloaded dev-pas and seems that is using an old FPC that does not support case with strings. So you need to switch to Lazarus better.

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Problem "Case For", after '9' Help!
« Reply #8 on: June 27, 2016, 01:05:22 am »
Or try this, instead of using string use integer.

Code: Pascal  [Select][+][-]
  1. PROGRAM CICLE_CASE_FOR_MONTHS;
  2.  
  3. USES
  4.     CRT;
  5. VAR
  6.    OPTION:INTEGER;
  7.    I:INTEGER;
  8.  
  9. BEGIN
  10. FOR I:= 1 TO 4 DO
  11.     BEGIN
  12.     CLRSCR;
  13.     GOTOXY(32,3);
  14.     WRITE('MONTHS OF THE YEAR');
  15.     GOTOXY(5,5);
  16.     WRITE('WRITE A NUMBER FROM 1 TO 12: ');
  17.     READLN(OPTION);
  18.     CASE OPTION OF
  19.     1:
  20.         BEGIN
  21.         CLRSCR;
  22.         GOTOXY(32,12);
  23.         WRITE('JUNARY');
  24.         END;
  25.     2:
  26.         BEGIN
  27.         CLRSCR;
  28.         GOTOXY(32,12);
  29.         WRITE('FEBRUARY');
  30.         END;
  31.     3:
  32.         BEGIN
  33.         CLRSCR;
  34.         GOTOXY(32,12);
  35.         WRITE('MARCH');
  36.         END;
  37.     4:
  38.         BEGIN
  39.         CLRSCR;
  40.         GOTOXY(32,12);
  41.         WRITE('APRIL');
  42.         END;
  43.     5:
  44.         BEGIN
  45.         CLRSCR;
  46.         GOTOXY(32,12);
  47.         WRITE('MAY');
  48.         END;
  49.     6:
  50.         BEGIN
  51.         CLRSCR;
  52.         GOTOXY(32,12);
  53.         WRITE('JUNE');
  54.         END;
  55.     7:
  56.         BEGIN
  57.         CLRSCR;
  58.         GOTOXY(32,12);
  59.         WRITE('JULY');
  60.         END;
  61.      8:
  62.         BEGIN
  63.         CLRSCR;
  64.         GOTOXY(32,12);
  65.         WRITE('AUGUST');
  66.         END;
  67.     9:
  68.         BEGIN
  69.         CLRSCR;
  70.         GOTOXY(32,12);
  71.         WRITE('SEPTEMBER');
  72.         END;
  73.     10:
  74.         BEGIN
  75.         CLRSCR;
  76.         GOTOXY(32,12);
  77.         WRITE('OCTOBER');
  78.         END;
  79.     11:
  80.         BEGIN
  81.         CLRSCR;
  82.         GOTOXY(32,12);
  83.         WRITE('NOVEMBER');
  84.         END;
  85.     12:
  86.         BEGIN
  87.         CLRSCR;
  88.         GOTOXY(32,12);
  89.         WRITE('DECEMBER');
  90.         END;
  91.      END;
  92.      READLN;
  93.      END;
  94. END.

Genofiend

  • Newbie
  • Posts: 5
Re: Problem "Case For", after '9' Help!
« Reply #9 on: June 27, 2016, 01:19:36 am »
Or try this, instead of using string use integer.

Code: Pascal  [Select][+][-]
  1. PROGRAM CICLE_CASE_FOR_MONTHS;
  2.  
  3. USES
  4.     CRT;
  5. VAR
  6.    OPTION:INTEGER;
  7.    I:INTEGER;
  8.  
  9. BEGIN
  10. FOR I:= 1 TO 4 DO
  11.     BEGIN
  12.     CLRSCR;
  13.     GOTOXY(32,3);
  14.     WRITE('MONTHS OF THE YEAR');
  15.     GOTOXY(5,5);
  16.     WRITE('WRITE A NUMBER FROM 1 TO 12: ');
  17.     READLN(OPTION);
  18.     CASE OPTION OF
  19.     1:
  20.         BEGIN
  21.         CLRSCR;
  22.         GOTOXY(32,12);
  23.         WRITE('JUNARY');
  24.         END;
  25.     2:
  26.         BEGIN
  27.         CLRSCR;
  28.         GOTOXY(32,12);
  29.         WRITE('FEBRUARY');
  30.         END;
  31.     3:
  32.         BEGIN
  33.         CLRSCR;
  34.         GOTOXY(32,12);
  35.         WRITE('MARCH');
  36.         END;
  37.     4:
  38.         BEGIN
  39.         CLRSCR;
  40.         GOTOXY(32,12);
  41.         WRITE('APRIL');
  42.         END;
  43.     5:
  44.         BEGIN
  45.         CLRSCR;
  46.         GOTOXY(32,12);
  47.         WRITE('MAY');
  48.         END;
  49.     6:
  50.         BEGIN
  51.         CLRSCR;
  52.         GOTOXY(32,12);
  53.         WRITE('JUNE');
  54.         END;
  55.     7:
  56.         BEGIN
  57.         CLRSCR;
  58.         GOTOXY(32,12);
  59.         WRITE('JULY');
  60.         END;
  61.      8:
  62.         BEGIN
  63.         CLRSCR;
  64.         GOTOXY(32,12);
  65.         WRITE('AUGUST');
  66.         END;
  67.     9:
  68.         BEGIN
  69.         CLRSCR;
  70.         GOTOXY(32,12);
  71.         WRITE('SEPTEMBER');
  72.         END;
  73.     10:
  74.         BEGIN
  75.         CLRSCR;
  76.         GOTOXY(32,12);
  77.         WRITE('OCTOBER');
  78.         END;
  79.     11:
  80.         BEGIN
  81.         CLRSCR;
  82.         GOTOXY(32,12);
  83.         WRITE('NOVEMBER');
  84.         END;
  85.     12:
  86.         BEGIN
  87.         CLRSCR;
  88.         GOTOXY(32,12);
  89.         WRITE('DECEMBER');
  90.         END;
  91.      END;
  92.      READLN;
  93.      END;
  94. END.

OK COPY PASTE WOKS MAN, THANK YOU VERY MUCH, BUT I SEE THE SAME CODE (EXCEPT THE INTEGER TYPE), DO YOU KNOW WHAT WAS THE PROBLEM? XD

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Problem "Case For", after '9' Help!
« Reply #10 on: June 27, 2016, 01:25:23 am »
I already say (a comment over the latest):

Code: Pascal  [Select][+][-]
  1. I've downloaded dev-pas and seems that is using an old FPC that does not support case with strings. So you need to switch to Lazarus better.

Case with strings is not supported with FPC 1.06, the version dev-pas is using. You need to use FPC 3.0 for example that is the newest-

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: Problem "Case For", after '9' Help!
« Reply #11 on: June 27, 2016, 06:41:39 am »
OK COPY PASTE WOKS MAN, THANK YOU VERY MUCH, BUT I SEE THE SAME CODE (EXCEPT THE INTEGER TYPE), DO YOU KNOW WHAT WAS THE PROBLEM? XD
Well,
first your code: '10' , '11' and '12' doesn't qualify as char since each of them contains two chars '1' + '0', '1' , '2' and that's why the compiler complain about type mismatch in the case block.

second, since you used a series of chars ('10', '11', '12') you needed to use string type for OPTION variable but again the older compiler version you are using doesn't support case of strings (as lainz pointed),, please take lainz's advice and use lazarus (preferably the latest version) to avoid any problem in the future.

finally, by changing OPTION to integer and modify the case of accordingly, you are now using case of integers and since all numbers 1..12 are integers and case of integers is supported by the old fpc version you are using so everything worked fine.

to summarize: your code was using the wrong type for OPTION and your fpc version doesn't support case of strings which left the only choice to use integer.
---
I hope it's clear to you now.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Problem "Case For", after '9' Help!
« Reply #12 on: June 27, 2016, 11:18:16 am »
So you need to switch to Lazarus better.

While I prefer the Lazarus IDE above the IDE (fp) that comes with FreePascal, in fact Genofiend only needs an up-to-date compiler (for the "case of string") for his program, so installing latest stable fpc (3.0) should be just fine for him.
(And (s)he should buy a new keyboard or fix the caps-lock key on his current one  O:-) )

I also would think that 'JUNARY' is a spellingmistake?

Another approach to his program would be to use the LongMonthNames array (declared in SysUtils unit) which would simplify his code drastically.

Code: Pascal  [Select][+][-]
  1. program Months;
  2.  
  3. uses
  4.   SysUtils, Crt;
  5. var
  6.   Option, i: integer;
  7.   OptionStr: string;
  8.  
  9. begin
  10.   for i:= 1 to 4 do
  11.   begin
  12.     ClrScr;
  13.     GotoXY(32,3);
  14.     write('Months of the year');
  15.     repeat
  16.       //read the option as a string first, if you read it as integer
  17.       //and the user enters an invalid input the program would crash, which is not user friendly
  18.       GotoXY(5,5);
  19.       write('Write a number from 1 to 12: ');
  20.       ClrEol; //clear to the end of line to erase potential leftovers from previous wrong input
  21.       readln(OptionStr);
  22.       Option := StrToIntDef(OptionStr, -1);
  23.     until Option in [1..12];
  24.     clrscr;
  25.     gotoxy(32,12);
  26.     write(AnsiUpperCase(DefaultFormatSettings.LongMonthNames[Option]));
  27.     readln;
  28.   end;
  29. end.
  30.  

Bart
« Last Edit: June 27, 2016, 11:36:38 am by Bart »

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: Problem "Case For", after '9' Help!
« Reply #13 on: June 27, 2016, 05:02:07 pm »
(And (s)he should buy a new keyboard or fix the caps-lock key on his current one  O:-) )
My thoughts exactly  ;)

Bart,
I think this is an assignment in which (maybe) he forced to use `case of`; other than that declaring a constant (array of strings) containing the names of months would be preferable and also eliminates the need of SysUtils unit for such simple/small program.

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Problem "Case For", after '9' Help!
« Reply #14 on: June 27, 2016, 05:11:00 pm »
Bart he is using another IDE not just FPC. is dev-pas, is a Windows application.
I don't know if their teacher recommended that IDE only, for that reason I say him to switch to Lazarus.

 

TinyPortal © 2005-2018