Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 19th, 2008, 07:42 PM
m4st3rm1nd m4st3rm1nd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 6 m4st3rm1nd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 52 m 42 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old July 19th, 2008, 10:02 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 616 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 20 m 7 sec
Reputation Power: 101
Send a message via AIM to L7Sqr
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.

Reply With Quote
  #3  
Old July 20th, 2008, 12:19 AM
m4st3rm1nd m4st3rm1nd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 6 m4st3rm1nd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 52 m 42 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old July 20th, 2008, 08:52 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 616 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 20 m 7 sec
Reputation Power: 101
Send a message via AIM to L7Sqr
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?

Reply With Quote
  #5  
Old July 20th, 2008, 09:26 AM
m4st3rm1nd m4st3rm1nd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 6 m4st3rm1nd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 52 m 42 sec
Reputation Power: 0
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'.

Reply With Quote
  #6  
Old July 20th, 2008, 11:13 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 616 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 20 m 7 sec
Reputation Power: 101
Send a message via AIM to L7Sqr
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.

Reply With Quote
  #7  
Old July 20th, 2008, 01:40 PM
m4st3rm1nd m4st3rm1nd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 6 m4st3rm1nd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 52 m 42 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Waiting for user input


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway