* count_string program
@ 2004-02-26 9:10 Cecilia Chew
2004-02-26 10:17 ` Jacob Sparre Andersen
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Cecilia Chew @ 2004-02-26 9:10 UTC (permalink / raw)
Hi all,
I am doing a program that will count the input characters from user.
There is a problem that I can't solve.
Problem is : since I'm going to count the characters. So, I can't ask
user how many characters they want to input. Compilation error come and
mention that the string need to be constrainted.
How am I going to constraint the string type since I don't know how many
characters user going to input.
Program is as follow:
========================================================================
with Ada.Text_Io, Ada.Integer_Text_Io;
use Ada.Text_Io, Ada.Integer_Text_Io;
procedure Count_String is
type String is array (Positive range <>) of Character;
Input : String;
Count : Integer;
begin
Count := 0;
Put ("Please enter a string :");
for I in Input'Range loop
Get(Input(I));
Count := Count + 1;
end loop;
New_Line;
Put ("There are" & Integer'Image(Count) & " characters");
end Count_String;
=========================================================================
Problem occured at input : string;
with compilation error: unconstrainted subtype not allowed.
Please give any comments to this program. Thank you.
--
Cecilia Chew
c e c i l i a <AT> g r e e n l i m e . c o m
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: count_string program
2004-02-26 9:10 count_string program Cecilia Chew
@ 2004-02-26 10:17 ` Jacob Sparre Andersen
2004-02-27 3:13 ` Cecilia Chew
2004-02-26 15:43 ` Björn Persson
2004-02-27 13:28 ` Stephen Leake
2 siblings, 1 reply; 9+ messages in thread
From: Jacob Sparre Andersen @ 2004-02-26 10:17 UTC (permalink / raw)
Cecilia Chew wrote:
> I am doing a program that will count the input characters from
> user. There is a problem that I can't solve.
> Problem is : since I'm going to count the characters. So, I can't
> ask user how many characters they want to input. Compilation error
> come and mention that the string need to be constrainted.
> How am I going to constraint the string type since I don't know how
> many characters user going to input.
Some questions that might help you:
* Do you have to store the characters entered by the user?
* How does the user signal that he/she is done entering characters?
Jacob
--
�Great minds discuss ideas,
Average minds discuss events,
Small minds discuss people.�
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: count_string program
2004-02-26 9:10 count_string program Cecilia Chew
2004-02-26 10:17 ` Jacob Sparre Andersen
@ 2004-02-26 15:43 ` Björn Persson
2004-02-27 3:19 ` Cecilia Chew
2004-02-27 13:28 ` Stephen Leake
2 siblings, 1 reply; 9+ messages in thread
From: Björn Persson @ 2004-02-26 15:43 UTC (permalink / raw)
Cecilia Chew wrote:
> Hi all,
> I am doing a program that will count the input characters from user.
> There is a problem that I can't solve.
> Problem is : since I'm going to count the characters. So, I can't ask
> user how many characters they want to input. Compilation error come and
> mention that the string need to be constrainted.
> How am I going to constraint the string type since I don't know how many
> characters user going to input.
If you do want to store the characters, and not just count them, then
you obviously need a data type that can handle a variable number of
characters. The first place to look for a suitable data type is Ada's
standard library.
--
Björn Persson
jor ers @sv ge.
b n_p son eri nu
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: count_string program
2004-02-26 10:17 ` Jacob Sparre Andersen
@ 2004-02-27 3:13 ` Cecilia Chew
2004-02-27 11:00 ` Jacob Sparre Andersen
0 siblings, 1 reply; 9+ messages in thread
From: Cecilia Chew @ 2004-02-27 3:13 UTC (permalink / raw)
Jacob Sparre Andersen wrote:
> Cecilia Chew wrote:
>
>
>>I am doing a program that will count the input characters from
>>user. There is a problem that I can't solve.
>>Problem is : since I'm going to count the characters. So, I can't
>>ask user how many characters they want to input. Compilation error
>>come and mention that the string need to be constrainted.
>>How am I going to constraint the string type since I don't know how
>>many characters user going to input.
>
>
> Some questions that might help you:
>
> * Do you have to store the characters entered by the user?
>
> * How does the user signal that he/she is done entering characters?
>
> Jacob
Isn't when i get the input from user, the characters will store in
(input(i)) variable? If not yet stored, what statement i need to add in?
In order to store the characters.
For the 2nd question above, I have no idea on it. Isn't once the user
push 'enter' button means their input is done.
--
Cecilia Chew
c e c i l i a <AT> g r e e n l i m e . c o m
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: count_string program
2004-02-26 15:43 ` Björn Persson
@ 2004-02-27 3:19 ` Cecilia Chew
2004-02-27 6:49 ` tmoran
0 siblings, 1 reply; 9+ messages in thread
From: Cecilia Chew @ 2004-02-27 3:19 UTC (permalink / raw)
Bj�rn Persson wrote:
> Cecilia Chew wrote:
>
>> Hi all,
>> I am doing a program that will count the input characters from user.
>> There is a problem that I can't solve.
>> Problem is : since I'm going to count the characters. So, I can't ask
>> user how many characters they want to input. Compilation error come
>> and mention that the string need to be constrainted.
>> How am I going to constraint the string type since I don't know how
>> many characters user going to input.
>
>
> If you do want to store the characters, and not just count them, then
> you obviously need a data type that can handle a variable number of
> characters. The first place to look for a suitable data type is Ada's
> standard library.
>
Since user will enter more than one characters, is that any problem if i
placed the type string is array (positive range <>) of character; to
handle a variable number of characters?
As input is declared as type string, isn't the characters will be stored
as the Get (input(i)); being executed??
--
Cecilia Chew
c e c i l i a <AT> g r e e n l i m e . c o m
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: count_string program
2004-02-27 3:19 ` Cecilia Chew
@ 2004-02-27 6:49 ` tmoran
2004-02-27 18:29 ` Björn Persson
0 siblings, 1 reply; 9+ messages in thread
From: tmoran @ 2004-02-27 6:49 UTC (permalink / raw)
placed the type string is array (positive range <>) of character; to
handle a variable number of characters?
> Isn't once the user push 'enter' button means their input is done.
but
> for I in Input'Range loop
> Get(Input(I));
> Count := Count + 1;
> end loop;
That loop does not check the character to see if it is "enter" (probably
ascii.cr), so it will continue to loop until Input is full of characters.
So if Input had room for, say, 50 characters, the program will demand 50
characters, even if none or all 50 of them are "enter", and then exit the
loop. Count will of course be 50. If you actually check the input
characters, and exit the loop when you see an "enter", then the count will
tell how many were entered. BTW, why are you saving the characters in
Input? The rest of the program never does anything with them. You really
only need to hold a single input character while you test for "enter".
> type String is array (Positive range <>) of Character;
Ada automatically has exactly such a String type, so there is no
need to duplicate what's already there.
> Input : String;
A String is an "array of characters". A particular string, like Input,
is an array of a certain number of characters. It's like saying a Book is
a bunch of pages between two covers. But any specific book has a certain
set number of pages. If you want a string of varying length, look up
Ada.Strings.Unbounded In the real world, you can probably assume most
users won't enter more than, say, 1,000 characters, so you could specify
that your program only handles a maximum of 1,000, and then declare
Input : String(1 .. 1000);
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: count_string program
2004-02-27 3:13 ` Cecilia Chew
@ 2004-02-27 11:00 ` Jacob Sparre Andersen
0 siblings, 0 replies; 9+ messages in thread
From: Jacob Sparre Andersen @ 2004-02-27 11:00 UTC (permalink / raw)
Cecilia Chew wrote:
> > Cecilia Chew wrote:
> >>I am doing a program that will count the input characters from
> >>user. There is a problem that I can't solve.
> >>Problem is : since I'm going to count the characters. So, I can't
> >>ask user how many characters they want to input. Compilation error
> >>come and mention that the string need to be constrainted.
> >>How am I going to constraint the string type since I don't know how
> >>many characters user going to input.
> Isn't when i get the input from user, the characters will store in
> (input(i)) variable? If not yet stored, what statement i need to add
> in? In order to store the characters.
I didn't write that you didn't store the characters entered. I just
tried to get you to think about exactly what you needed. If you don't
need all the characters entered later in the program, then you don't
have to store them, and that makes the program much easier to write.
> For the 2nd question above, I have no idea on it. Isn't once the
> user push 'enter' button means their input is done.
Sometimes. Other times they mark it by sending an end-of-file code.
In the package Ada.Text_IO there are functions to check both if the
user has sent an end-of-line code (pressed 'enter' on the keyboard)
and to check if the user has sent an end-of-file code. The two
functions are named: End_Of_Line and End_Of_File.
Jacob
--
LDraw.org Parts Tracker FAQ:
http://www.ldraw.org/library/tracker/ref/faq/
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: count_string program
2004-02-26 9:10 count_string program Cecilia Chew
2004-02-26 10:17 ` Jacob Sparre Andersen
2004-02-26 15:43 ` Björn Persson
@ 2004-02-27 13:28 ` Stephen Leake
2 siblings, 0 replies; 9+ messages in thread
From: Stephen Leake @ 2004-02-27 13:28 UTC (permalink / raw)
To: comp.lang.ada
Cecilia Chew <cecilia@nowhere.com> writes:
> Hi all,
> I am doing a program that will count the input characters from user.
> There is a problem that I can't solve.
> Problem is : since I'm going to count the characters. So, I can't ask
> user how many characters they want to input. Compilation error come
> and mention that the string need to be constrainted.
> How am I going to constraint the string type since I don't know how
> many characters user going to input.
See Ada.Text_IO.Get_Line.
For that, you need to know in advance the maximum number of characters
the user might enter.
Or, if you are using GNAT, see Ada.Strings.Unbounded.Text_IO (not a
standard Ada package). Then you don't need a max in advance.
--
-- Stephe
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: count_string program
2004-02-27 6:49 ` tmoran
@ 2004-02-27 18:29 ` Björn Persson
0 siblings, 0 replies; 9+ messages in thread
From: Björn Persson @ 2004-02-27 18:29 UTC (permalink / raw)
tmoran@acm.org wrote:
> In the real world, you can probably assume most
> users won't enter more than, say, 1,000 characters, so you could specify
> that your program only handles a maximum of 1,000, and then declare
>
> Input : String(1 .. 1000);
You (Cecilia) could do that, but if you need to store the string it's
because you want to use it later. Then you will most likely need to know
how many of those 1000 positions are used and how many contain garbage.
That is, you have to keep track of the actual length of the string
separately. Since there are string types in the standard library that
keep track of the length automatically, it's better to use them.
--
Björn Persson
jor ers @sv ge.
b n_p son eri nu
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2004-02-27 18:29 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-26 9:10 count_string program Cecilia Chew
2004-02-26 10:17 ` Jacob Sparre Andersen
2004-02-27 3:13 ` Cecilia Chew
2004-02-27 11:00 ` Jacob Sparre Andersen
2004-02-26 15:43 ` Björn Persson
2004-02-27 3:19 ` Cecilia Chew
2004-02-27 6:49 ` tmoran
2004-02-27 18:29 ` Björn Persson
2004-02-27 13:28 ` Stephen Leake
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox