#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    USA
    Posts
    42
    Rep Power
    7

    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    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>
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    USA
    Posts
    42
    Rep Power
    7
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    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?
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    USA
    Posts
    42
    Rep Power
    7
    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'.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    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.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    USA
    Posts
    42
    Rep Power
    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

IMN logo majestic logo threadwatch logo seochat tools logo