Thread: Stuck again...

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

    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Stuck again...


    As I learn new things, I'm adding them all into my last program. Making it bigger.

    Well... I've tested the "if" and "else" thing before with no issue. For some reason, now it's giving me an issue.

    This is what I have...

    Code:
    wwidth = 100
    puts ( 'Hello there!  What is your first name?' .center(wwidth))
    firstname = gets.chomp
    if
      firstname.capitalize != 'Kyle'
      puts ('Not quite as nice as "Kyle," but still nice.  And your middle name?'.center(wwidth))
    else
      puts ('That is my favorite name!  And what is your middle name?'.center(wwidth))
    end
    middlename = gets.chomp
    if
      middlename.capitalize != 'Ryan'
      puts ('Not bad.  And your last name, ' + firstname.capitalize +'?'.center(wwidth))
    else
      puts ('That makes a splendid middle name!  And what would your last name be?'.center(wwidth))
    end
    lastname = gets.chomp
    if
      lastname.capitalize != 'Conway'
      puts ('Nice to meet you, ' + firstname.capitalize + middlename.capitalize + lastname.capitalize + '!  What is your favorite number?'.center(wwidth))
    else
      puts ('It is you!  My creator!  Thank you for making me, oh mighty one!  In case your favorite number has changed since you programmed me, could you tell me your favorite number?'.center(wwidth))
    end
    favenumber = gets.chomp
    betternumber = favenumber.to_i + 1 
    puts ( 'Well what do you think about ' + (favenumber.to_i + 1).to_s + '?  It\'s one bigger, and thus must be one better!') .center(wwidth)
    puts ( 'And speaking of numbers, did you know that there are ' + (firstname.length.to_i + middlename.length.to_i + lastname.length.to_i).to_s + ' letters in your whole name?') .center(wwidth)
    puts ' '
    puts ' '
    puts 'Press ENTER to close.'
    STDIN.gets
    When I run it I get two error messages...
    C:/Users/The Jester/Desktop/Ruby Programs/Name with a bit more input.rb:13: syntax error, unexpected
    tUPLUS, expecting ')'
    ...ame, ' + firstname.capitalize +'?'.center(wwidth))
    and another one which I can't "ctrl+c" for some reason about a unexpected ")" and "keyword_end."

    I've looked through it and can't seem to find an "unexpected" ")" anywhere. Everyone I see belongs right where I put it. And as for the "end" bit, I seem to have used them properly.

    Any ideas?
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    The specific problem lies in spacing, specifically, that there needs to be a space after the concatenation operator ('+') before the '?' at the end of the line.

    As an aside, if you look at how the line in question is written:
    Code:
     puts ('Not bad.  And your last name, ' + firstname.capitalize + '?'.center(wwidth))
    ... you will notice that the .center() method is being applied only to the final '?' substring, not the whole string you as you are intending. As with the last question, there are a few different things you can do about it. First off, you can construct the string and save it to a temporary variable:
    Code:
      middle = 'Not bad.  And your last name, ' + firstname.capitalize + '?'
      puts(middle.center(wwidth))
    or you can use string interpolation:
    Code:
      puts ("Nice to meet you, #{firstname} #{middlename} #{lastname}!  What is your favorite number?".center(wwidth))
    Remember to use double quotes (") rather than single quotes (') if you choose the second approach.
    Last edited by Schol-R-LEA; January 16th, 2012 at 02:12 PM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0
    Thanks again for all of the help! Could you explain why double quotes would be better than single? I don't think the book has covered that yet.

    Also, I thought that putting # before things took what followed out of the program. I thought it was used for turning things into notes.

    Edit: I figured I'd add one more question here instead of filling the forum with my newbie questions.

    I decided that I want my program to greet anyone with my last name in a third way. I want it to suggest that maybe we're related. With the help of Google, I've found "elsif." However, I can't seem to figure out how to make use of it.

    Before it was anyone with my last name would be confused for me. But I have that fixed now.

    Code:
    if
         firstname.capitalize + middlename.capitalize + lastname.capitalize == 'KyleRyanConway'
    But I can't seem to figure out how to give it a different response...

    Is there a code that goes something along the lines of:
    Code:
    if
         firstname.capitalize != 'Kyle' or middlename.capitalize != 'Ryan' and lastname.capitalize == 'Conway'
    ?

    That sure would be useful... At the point I'm at, he's hit on "if/else" statements, but hasn't gone very in depth.

    Additional Edit: I'll keep the original up there in case anyone else stumbles across it and might need it.

    I used 'elsif' and had it make sure the last name matched. As the first 'if' came first in the code, it would check for the entire name first and put what it was told to before checking the second 'elsif.' If that failed, it moved on and matched only the last name.

IMN logo majestic logo threadwatch logo seochat tools logo