|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Waiting for user input
I have a program that prints out multiple lines of output with 15 characters every line.
And i want to be able to ask the user if he wants to continue after every 10 lines? i is my counter keeping track of number of characters printed till now. Code:
if( i%150 == 0) then print "do you want to continue (y/n) ?" response = gets exit if response == 'n' end But this dont work. And it doesnt even wait for user input. It keeps printing lines even after the line "do you want to continue (y/n) ?". Is there a better way to do it? |
|
#2
|
|||
|
|||
|
Likely that the newline is your problem - gets returns the result with the newline included. For example...
Code:
irb(main):007:0> def get1 irb(main):008:1> puts "Matched 'n'" if "n" == gets irb(main):009:1> end => nil irb(main):010:0> def get2 irb(main):011:1> puts "Matched 'n'" if "n" == gets.chomp irb(main):012:1> end => nil irb(main):013:0> get1 n => nil irb(main):014:0> get2 n Matched 'n' => nil irb(main):015:0>
__________________
-- I'll provide you with reference points; if they dont work, refer to something else. If you process text, this might make your life a little easier. |
|
#3
|
|||
|
|||
|
Code:
if( i % 150 == 0 ) then puts "do you want to continue (y/n) ?" input = gets.chomp if input == 'n' then exit end end Yeah chomp does make matching part alright. But still the program wont wait for user input. Therefore the program never gets to matching part at all ![]() |
|
#4
|
|||
|
|||
|
Code:
irb(main):009:0> def fun(max)
irb(main):010:1> max.times { |i|
irb(main):011:2* if i % 150 == 0 then
irb(main):012:3* puts "Continue? (y/n)"
irb(main):013:3> return if 'n' == gets.chomp
irb(main):014:3> end
irb(main):015:2> }
irb(main):016:1> end
=> nil
and Code:
irb(main):017:0> fun 300 Continue? (y/n) y Continue? (y/n) y => 300 irb(main):018:0> fun 300 Continue? (y/n) n => nil I'm not sure where the problem is... Is there more context? |
|
#5
|
|||
|
|||
|
Yes there is more context. My program prints out multiple lines of output with 15 characters every line.
For example, if it has 100 lines of output, I want to ask user if he wants to continue printing more or stop after every 10 lines. Code:
OUTPUT: abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno Do you want to continue ? (y/n) y abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno Do you want to continue ? (y/n) n But what happens is : Code:
OUTPUT: abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno Do you want to continue ? (y/n) abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno abcdefghijklmno Do you want to continue ? (y/n) abcdefghijklmno . . . . and so on upto 100 lines Program doesnt stop to take input from keyboard. It should wait for a keystroke 'y' or 'n'. |
|
#6
|
|||
|
|||
|
what I meant by context was in your code. In other words, are you running multiple threads? What type of IO are you doing before the loop?
If possible, please paste the full code. We can then take it and try to run it ourselves. |
|
#7
|
|||
|
|||
|
Hey , I sorted it out. I was missing a flush in there.
Code:
def fun(max)
begin
max.times {
puts "continue (y/n)?"
STDOUT.flush()
exit if 'n' == STDIN.gets.chomp
}
end
end
Thanks L7Sqr for your guidance ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > Waiting for user input |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|