Author |
Message |
Reality Check
|
Posted: Tue Dec 12, 2006 4:26 pm Post subject: variation in "get" statements. |
|
|
Alright I'm having a lot of trouble doing something and I was hoping someone here could help. So we're getting an assignment where the input is a line and we output something about each line. So my teacher might input:
1 9 1 R
Now I get it and all but she might also input
2 9 1 R b
Here is where I have trouble. How do I know if she will input 4 variables or 5? The first 3 will always be integer and everything after it will be string but if I get 5 variables and she only inputs 4 my program will keep on asking her for the 5th. If I get 4 variables and she inputs 5 then I get an error. It is dependant on the first number. If it is a 1 then it is 4 variables and if it is a 2 then it is 5.
Now, I tried doing an if statement but my if statement can never get a chance to go through because she doesn't press enter after each variable input she just presses enter when she is done inputting. So she'll put "1 9 1 R and then press enter or she'll put "2 9 1 R b" and then press enter. If you need more info just ask. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Tue Dec 12, 2006 4:42 pm Post subject: (No subject) |
|
|
is she always going to input one character at a time? if so you could possibly do something fancy with getch() and flexible arrays. |
|
|
|
|
![](images/spacer.gif) |
Reality Check
|
Posted: Wed Dec 13, 2006 12:34 am Post subject: (No subject) |
|
|
She will input a whole line and then press enter. But yea Getch might work. I could do that for the first variable only and check if it is a 1 or 2 and get my variables based on that. I won't need a flexible array though. |
|
|
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Wed Dec 13, 2006 1:56 pm Post subject: (No subject) |
|
|
what if she inputs five variables, and your static array only goes to 4? You should seriously consider a flexible array. |
|
|
|
|
![](images/spacer.gif) |
Reality Check
|
Posted: Wed Dec 13, 2006 4:40 pm Post subject: (No subject) |
|
|
But by using Getch, I'll know how many variables there will be before hand. Instead of using "get" for the first variables, I'll just use Input.keydown or Getch. If she presses one I'll know there will be 3 more variables to come and if she presses 2 I'll know there will be 4 more variables to come. She won't press 1 and then type in 5 variables. |
|
|
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Wed Dec 13, 2006 8:30 pm Post subject: (No subject) |
|
|
sounds like an excuse to get around using flexible arrays to me, really, just use them, they will make this so much easier instead of mickey-mousing some sort of other way to do it. |
|
|
|
|
![](images/spacer.gif) |
richcash
|
Posted: Thu Dec 14, 2006 6:36 pm Post subject: (No subject) |
|
|
You do not need getch or keydown.
code: | var arr : flexible array 1 .. 4 of string
for i : 1 .. 5
if i = 5 and arr (1) = "2" then
new arr, 5
elsif i = 5 then
exit
end if
get arr (i)
end for
for n : 1 .. upper (arr)
put arr (n)
end for |
When someone enters a number of values using a spacebar at once (when there is no colon used to include those spaces!), it only assigns the first variable up to the first space of the input and then continues the execution, in this case continuing with the for loop. When the next get statement is reached it fills in the value if there is anything left from the first input. So we can do things (use an if structure) using values that have already been assigned to variables but before certain other variables are assigned (in our case, we can prevent the fifth variable from being assigned if the first variable (already assigned) is a '1'). Here's a simple example to demonstrate :
code: | var a, b : string
get a
get b
cls
put a
put b |
b is automatically assigned if there is still something left (if there were spaces) from the first input (after assigning a). |
|
|
|
|
![](images/spacer.gif) |
|