r/learnruby • • Mar 24 '15

Error with string comparison

Here's what I'm working with:

contents=[]
fl=File.open("elementsSep.txt").readlines.each do |line|
   line=line.gsub(/[\n]/,"") #regex
   contents.push(line)
end
for i in 0...contents.length
    elem=contents[i].split(";")
    contents[i]=elem
end
#
checker = false
while(!checker)
    puts"                               ~ Chemquiz 2015 ~"
    #splash
    puts"Would you like to [r]eview, [q]uiz, or [e]xit?"
    starter=gets; starter.downcase!
    if(starter == "r" || starter == "review")
	    puts"What information do you have of the element:"
	    puts"  [N]umber, N[a]me, or [S]ymbol?";info=gets;info.downcase!
	    if(info=="n") #number given
		    puts"What is the number?";num=gets;num=num.to_i
		    if(num > 118 || num < 1) 
			    puts"Invalid expression, please try again."
		    elsif(1 <= num <= 118)
			    puts"Would you like the [n]ame or [s]ymbol?";ans=gets;ans.downcase!
			    if(ans=="n")
				    puts"The name is #{contents[num-1][0]}"
			    elsif(ans=="s")
				    puts"The symbol is #{contents[num-1][1]}"
			    else
				    puts"Invalid expression, please try again."
			    end
		    end
	    elsif(info=="a") #name given
		    puts"What is the name?";name=gets;name.downcase!;key=(-1);
		    for i in 0...contents.length
			    if(contents[i][0] == name)
				    key=i
			    else #
			    end
		    end
		    if(key < 0)
			    puts"Invalid expression, please try again."
		    elsif(key >= 0)
			    puts"Would you like to know the [n]umber or [s]ymbol?"
			    ans=gets;ans.downcase!
			    if(ans == "n")
				    puts"The number for #{name} is #{key}"
			    elsif(ans == "s")
				    puts"The symbol for #{name} is #{contents[key][1]}"
			    else #
			    end
		    else #
		    end
	    elsif(info=="s") #symbol given
		    puts"What is the symbol?";name=gets;name.downcase!;key=(-1);
		    for i in 0...contents.length
			    if(contents[i][1] == name)
				    key=i
			    else #
			    end
		    end
		    if(key < 0)
			    puts"Invalid expression, please try again."
		    elsif(key >= 0)
			    puts"Would you like to know the [n]umber or n[a]me?"
			    ans=gets;ans.downcase!
			    if(ans == "n")
				    puts"The number for #{name} is #{key}"
			    elsif(ans == "s")
				    puts"The name for #{name} is #{contents[key][0]}"
			    else #
			    end
		    else #
		    end
	    elsif(info=="e") #symbol given
		    checker=true
	    else 
		    puts"Invalid expression, please try again."
	    end
    elsif(starter == "q" || starter == "quiz")
	    #quiz area, randomize
    else
	    puts"You entered an invalid expression. Try Again or Exit?"
	    endinp=gets;endinp.downcase!;(endinp=="exit")? checker=true : checker=false
    end
end
puts"Hit <ENTER> to exit."
gets

When I run the program, for some reason, correct text input isn't recognized. What am I doing wrong here?

1 Upvotes

3 comments sorted by

2

u/Chaoist Mar 24 '15

The way this code is set out makes it really difficult to go through (read painful). You really need to go through a style guide. Also use #each instead of for x in y style for-loops.

You have an elsif condition "1 <= n <= 118" which is an invalid conditional expression. So your review by number section probably isn't working.

If you could specify what inputs you are testing the program with it'd be helpful to pinpoint the issue. But that's what I've found so far. Fix those up and see what happens.

1

u/Ando1 Mar 25 '15

The String#chomp is something that I always forget. That was where the main error occurred. Also, I know that my code might not look the prettiest, but I've done a lot of C++, so the semicolon habit and need to condense lines is something that I find saves space.

TL;DR: I like condensing statements, mostly because Ruby allows for it.

1

u/mCseq Mar 25 '15

You need to String#chomp your user inputs.

For example, you have something like this:

starter=gets; starter.downcase!

First of all, it looks much better to not have multiple statements on one line like this. I'd rather it look like this:

starter = gets
starter.downcase!

Second, you don't need to do these separately. You can chain methods like this and get the same thing.

starter = gets.downcase

Third, user input contains a '\n' at the end of it. You can remove that with the chomp method like this:

starter = gets.chomp.downcase

or

starter = gets.downcase.chomp

Either is fine. Without this checking for string equality with input won't work. You will be asking if "review\n" == "review" which will always be false.

There's a lot of other things I could say about your script but I'll hold off unless you want more of a critique.