Recent

Author Topic: Object Pascal: Storing user input into an Integer Array  (Read 1526 times)

hymcode

  • New Member
  • *
  • Posts: 13
Object Pascal: Storing user input into an Integer Array
« on: August 25, 2020, 03:57:31 pm »
Hello all. In my Object Pascal program I have a TEdit box where a user is asked to input a 5 digit number. The users number is then stored into an Array of type Integer, however I keep getting an 'Incompatible types' error message and I cannot wrap my head around why, since I am converting from String to Integer.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   intArray : Array [1..5] of Integer;
  4. begin
  5.   intArray := StrToInt(Edit1.Text);
  6. end;  

Code: Text  [Select][+][-]
  1. Error: Incompatible types: got "LongInt" expected "Array[1..5] Of LongInt"

Any help would be greatly appreciated.
« Last Edit: August 25, 2020, 03:59:14 pm by hymcode »
Don’t wish it were easier; wish you were better. – Jim Rohn

Thaddy

  • Hero Member
  • *****
  • Posts: 10602
Re: Object Pascal: Storing user input into an Integer Array
« Reply #1 on: August 25, 2020, 04:10:00 pm »
The array is your mistake.
5 digits can be stored in a SmallInt.
A simple example works like this:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. var
  3.   i:SmallInt;
  4. begin
  5.   readln(i); // reads the integer immediately
  6.   writeln(i);
  7. end.
Regarding your code this becomes:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i:SmallInt; //or integer, does not hurt
  4. begin
  5.   i := StrToInt(Edit1.Text); // better use trystrtoint() btw. Or Edit1.Text.ToInteger;
  6. end;
See: https://www.freepascal.org/docs-html/rtl/sysutils/trystrtoint.html
« Last Edit: August 25, 2020, 04:13:26 pm by Thaddy »

hymcode

  • New Member
  • *
  • Posts: 13
Re: Object Pascal: Storing user input into an Integer Array
« Reply #2 on: August 25, 2020, 04:21:52 pm »
The array is your mistake.
5 digits can be stored in a SmallInt.
A simple example works like this:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. var
  3.   i:SmallInt;
  4. begin
  5.   readln(i); // reads the integer immediately
  6.   writeln(i);
  7. end.
Regarding your code this becomes:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i:SmallInt; //or integer, does not hurt
  4. begin
  5.   i := StrToInt(Edit1.Text); // better use trystrtoint() btw. OrEdit1.Text.ToInteger;
  6. end;
See: https://www.freepascal.org/docs-html/rtl/sysutils/trystrtoint.html

Thanks for responding. I'm aware 5 digits can easily be stored into an Integer, however the next stage of my program is to check each digit, so I thought the best way to do this was to store the users input into an array and then point at each digit. Something like this:

Code: Pascal  [Select][+][-]
  1. if intArray[1] < 0 then
  2.   ShowMessage('Error');
Don’t wish it were easier; wish you were better. – Jim Rohn

Sieben

  • Full Member
  • ***
  • Posts: 144
Re: Object Pascal: Storing user input into an Integer Array
« Reply #3 on: August 25, 2020, 04:46:56 pm »
You can directly parse the string instead, sth like this

Code: Pascal  [Select][+][-]
  1. var i: integer;
  2.     S: string;
  3. begin
  4.   S := Edit.Text;
  5.   for i:=1 to Length(S) do
  6.     if StrToInt(S[i]) < 0 then
  7.       ShowMessage('Error');
  8. end;
  9.  
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32

Thaddy

  • Hero Member
  • *****
  • Posts: 10602
Re: Object Pascal: Storing user input into an Integer Array
« Reply #4 on: August 25, 2020, 05:02:50 pm »
You can directly parse the string instead, sth like this

Code: Pascal  [Select][+][-]
  1. var i: integer;
  2.     S: string;
  3. begin
  4.   S := Edit.Text;
  5.   for i:=1 to Length(S) do
  6.     if StrToInt(S[i]) < 0 then
  7.       ShowMessage('Error');
  8. end;
  9.  

That is really bad code, because any iteration can cause an exception. NEVER do that again.
It is way better to guard against wrong user input by using TryStrToInt()
Really: never use such code again.  >:D

Anyway, responses with code are always appreciated, but be careful with what code you write.... O:-)

rvk

  • Hero Member
  • *****
  • Posts: 4403
Re: Object Pascal: Storing user input into an Integer Array
« Reply #5 on: August 25, 2020, 05:07:50 pm »
Thanks for responding. I'm aware 5 digits can easily be stored into an Integer, however the next stage of my program is to check each digit, so I thought the best way to do this was to store the users input into an array and then point at each digit. Something like this:

Code: Pascal  [Select][+][-]
  1. if intArray[1] < 0 then
  2.   ShowMessage('Error');
One character number that's < 0 ???? (-1 is already two characters)

Can you explain exactly what you want to check.
There might be a lot better ways if you explain your desired outcome.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2433
  • Compiler Developer
Re: Object Pascal: Storing user input into an Integer Array
« Reply #6 on: August 25, 2020, 05:16:25 pm »
Hello all. In my Object Pascal program I have a TEdit box where a user is asked to input a 5 digit number. The users number is then stored into an Array of type Integer, however I keep getting an 'Incompatible types' error message and I cannot wrap my head around why, since I am converting from String to Integer.

StrToInt returns an Integer, not an array of Integer. These two are completely different. If you need to manually process the digits (as you said in another post) you need to do so manually by using div and mod on the returned value.

Sieben

  • Full Member
  • ***
  • Posts: 144
Re: Object Pascal: Storing user input into an Integer Array
« Reply #7 on: August 25, 2020, 05:25:10 pm »
@Thaddy - You're right, I'm sorry.  :-[
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32

Thaddy

  • Hero Member
  • *****
  • Posts: 10602
Re: Object Pascal: Storing user input into an Integer Array
« Reply #8 on: August 25, 2020, 05:47:45 pm »
@Thaddy - You're right, I'm sorry.  :-[
Well, rvk and PascalDragon were also right....

hymcode

  • New Member
  • *
  • Posts: 13
Re: Object Pascal: Storing user input into an Integer Array
« Reply #9 on: August 25, 2020, 05:49:21 pm »
Thanks for responding. I'm aware 5 digits can easily be stored into an Integer, however the next stage of my program is to check each digit, so I thought the best way to do this was to store the users input into an array and then point at each digit. Something like this:

Code: Pascal  [Select][+][-]
  1. if intArray[1] < 0 then
  2.   ShowMessage('Error');
One character number that's < 0 ???? (-1 is already two characters)

Can you explain exactly what you want to check.
There might be a lot better ways if you explain your desired outcome.

Hi there. Sorry, that was a very vague code explanation attempt from me. I am learning about data handling and number conversion in Object Pascal. So, the program I am attempting to make is to take a user’s input of a fixed length. Store the value and then allow them to point to a digit and change just the selected value.

For example user enters 25697 -> value gets stored in an Array (or something more suitable) -> display the array, i.e.:
index1 [2]
index2 [5]
index3 [6]
index4 [9]
index5 [7] 
-> Allow the user to select what number to replace by selecting the index number. -> Then generate a new number showing the changes.

In my earlier code attempt I was trying to say that if the user inputs a 0 as the first number then display and error message. I hope this helps.
« Last Edit: August 25, 2020, 05:57:08 pm by hymcode »
Don’t wish it were easier; wish you were better. – Jim Rohn

Sieben

  • Full Member
  • ***
  • Posts: 144
Re: Object Pascal: Storing user input into an Integer Array
« Reply #10 on: August 25, 2020, 06:09:54 pm »
Well, rvk and PascalDragon were also right....

Of course. It was just a silly attempt without really thinking about it...
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32

Leledumbo

  • Hero Member
  • *****
  • Posts: 8279
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Object Pascal: Storing user input into an Integer Array
« Reply #11 on: August 26, 2020, 09:32:47 pm »
Hi there. Sorry, that was a very vague code explanation attempt from me. I am learning about data handling and number conversion in Object Pascal. So, the program I am attempting to make is to take a user’s input of a fixed length. Store the value and then allow them to point to a digit and change just the selected value.

For example user enters 25697 -> value gets stored in an Array (or something more suitable) -> display the array, i.e.:
index1 [2]
index2 [5]
index3 [6]
index4 [9]
index5 [7] 
-> Allow the user to select what number to replace by selecting the index number. -> Then generate a new number showing the changes.

In my earlier code attempt I was trying to say that if the user inputs a 0 as the first number then display and error message. I hope this helps.
Watch closely to PascalDragon's hint:
If you need to manually process the digits (as you said in another post) you need to do so manually by using div and mod on the returned value.
When you mod an integer by 10, you get the last digit.
When you div an integer by 10, you strip out the last digit.
Likewise, you can form the original integer back by multiplying it by 10 then add the last digit from mod result.
You just need to loop this for an exact number of times depending on which digit your user wants to change.
This is a good brain training that I'm afraid more and more programmers are lacking and tend to use less resource efficient solution such as using array like what you did.

Bart

  • Hero Member
  • *****
  • Posts: 4042
    • Bart en Mariska's Webstek
Re: Object Pascal: Storing user input into an Integer Array
« Reply #12 on: August 26, 2020, 11:15:47 pm »
Here's a naive approach with rudimentary error checking.

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$H+}
  3. uses
  4.   SysUtils, Math;
  5.  
  6. procedure ErrorInvalidIndex;
  7. begin
  8.   writeln('Invalid Index');
  9.   halt;
  10. end;
  11.  
  12. procedure ErrorInvalidDigit;
  13. begin
  14.   writeln('Invalid Digit');
  15.   halt;
  16. end;
  17.  
  18. var
  19.   N, Index, Digit, ValueAtDigit: Integer;
  20.  
  21. begin
  22.   repeat
  23.     write('Enter number (0 quits): ');
  24.     readln(N);
  25.     if (N <> 0) then
  26.     begin
  27.       writeln('Number = ',N);
  28.       write('Enter index to change: ');
  29.       readln(Index);
  30.       if (Index <1) or (10**(Index-1) > N) then
  31.         ErrorInvalidIndex;
  32.       write('Enter new digit for index ',Index,': ');
  33.       readln(Digit);
  34.       if not (Digit in [0..9]) then
  35.         ErrorInvalidDigit;
  36.       ValueAtDigit := N div 10**(Index-1);  //the ** operator on integers performs integer power (so 10^(Index-1))
  37.       ValueAtDigit := ValueAtDigit Mod 10;
  38.       N := N - ValueAtDigit * 10**(Index-1);
  39.       N := N + Digit * 10**(Index-1);
  40.       writeln('Number is now: ',N);
  41.     end;
  42.   until N = 0;
  43. end.

Mind you, since all you want to do is changing a digit, and you read from user input, it is much simpler to have the number as a string and access the index directly.

[Edit]
Notice that Index is counted from right to left.
It is left as an excercise to hymcode to alter this so that it is left to right.
[/Edit]

Bart
« Last Edit: August 27, 2020, 09:34:57 am by Bart »

egsuh

  • Hero Member
  • *****
  • Posts: 505
Re: Object Pascal: Storing user input into an Integer Array
« Reply #13 on: August 27, 2020, 04:16:33 am »
Use TMaskEdit.

rvk

  • Hero Member
  • *****
  • Posts: 4403
Re: Object Pascal: Storing user input into an Integer Array
« Reply #14 on: August 27, 2020, 09:30:02 am »
A very rudimentary solution with treating your edit as string:
This can be made a lot more efficient (using intermediate variables) but it's just to get an idea.

Code: Pascal  [Select][+][-]
  1. uses StrUtils;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   NewDigit: String;
  6.   NewStr: String;
  7. begin
  8.  
  9.   if (StrToIntDef(Edit1.Text, 0) < 10000) or (StrToIntDef(Edit1.Text, 0) > 99999) then
  10.   begin
  11.     Showmessage('Wrong 5 digit number');
  12.     exit;
  13.   end;
  14.  
  15.   if (StrToIntDef(EditSelectedNumber.Text, 0) in [1..5]) then
  16.   begin
  17.     Showmessage('Wrong selected position');
  18.     exit;
  19.   end;
  20.  
  21.   NewDigit := InputBox('Change digit', 'New digit on position ' + EditSelectedNumber.Text, Edit1.Text[StrToInt(EditSelectedNumber.Text)]);
  22.  
  23.   NewStr := Edit1.Text;
  24.   if StrToInt(EditSelectedNumber.Text) = 1 then // you wanted position 1 never to be 0.
  25.   begin
  26.     if StrToIntDef(NewDigit, 0) in [1..9] then NewStr[StrToInt(EditSelectedNumber.Text)] := NewDigit[1];
  27.     Edit1.Text := NewStr;
  28.   end
  29.   else
  30.   begin
  31.     if StrToIntDef(NewDigit, 0) in [0..9] then NewStr[StrToInt(EditSelectedNumber.Text)] := NewDigit[1];
  32.     Edit1.Text := NewStr;
  33.   end;
  34.  
  35. end;

 

TinyPortal © 2005-2018