I'm new to ruby and am trying to convert a string into an integer.
I am trying to calculate GPA so I am using gets for input of a letter grade (A,B,C,etc.) then I will convert each of these into their respective number grades (A=4, B=3, C=2, etc.). I have found a bunch of info on how to convert integer to strings but not strings to integer. Any suggestions?
puts ("This program will calculate your GPA this semester")
puts ("Please type your grade, then press enter")
puts("How many courses are you taking?")
num_courses=gets.chomp
puts("Your are taking #{num_courses} courses")
puts ("Use A, A-, B+, B, B-, C+, C, C-, D, or F (Press enter after typing each grade.)")
gradeList = []
gradeList.push gets.chomp while gradeList.last != ''
puts gradeList.sort
"A"=4
"A-"=3.7
"B+"=3.3
Update: Changed code completely. I think I was coming at it from the wrong angle. However, I am still getting an error:grades.rb:10: undefined method `last' for nil:NilClass (NoMethodError)
puts "This program will calculate your GPA this semester"
puts "Please type your grade, then press enter"
puts("How many courses are you taking?")
num_courses=gets.chomp
puts("Your are taking #{num_courses} courses")
puts ("Use A, A-, B+, B, B-, C+, C, C-, D, or F (Press enter after typing each grade.)")
grade=gets.chomp while grade.last != ''
if grade == "A"
total=total+4
elsif grade=="B"
total=total+3
elsif grade=="C"
total=total+2
elsif grade=="D"
total=total+1
end
gpa=total/num_courses
puts"Your GPA is #{gpa}"