10 Replies - 1031 Views - Last Post: 20 September 2013 - 01:17 PM Rate Topic: -----

#1 panamapapi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-September 13

C# Console App won't accept "Yes" and "Y" Input

Posted 19 September 2013 - 05:12 PM

Simple Bank Roll Console Loop here...... Having a bit of a time trying to get this one to accept both "YES" and 'y' inputs , I thought maybe if I added something like
while (response == 'Y' || response = "YES") I would be able to get this one working properly

Please HELP

using System;
public class LoopingBankBal
{
    public static void Main()
    {
        double bankBal = 1000;
        const double INT_RATE = 0.04;
        string inputString;
        char response;
        Console.Write("Do you want to see your balance? Y or N ...");
        inputString = Console.ReadLine();
        response = Convert.ToChar(inputString);
        while (response == 'Y')
        {
            Console.WriteLine("Bank balance is {0}", bankBal.ToString("C"));
            bankBal = bankBal + bankBal * INT_RATE;
            Console.Write("Do you want to see next year's balance? Y or N ...");
            inputString = Console.ReadLine();
            response = Convert.ToChar(inputString);
        }
        Console.WriteLine("Have a nice day!");
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: C# Console App won't accept "Yes" and "Y" Input

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,980
  • Joined: 08-June 10

Re: C# Console App won't accept "Yes" and "Y" Input

Posted 19 September 2013 - 06:06 PM

Hi. There were a few problems with your post that I'd like to address first:

Please do not use all-caps for a title. On the internet, that's the same as shouting.

Please use code tags when posting code.
:code:

I've fixed your post, but please follow the posting rules in the future.



On to the question.

Quote

I thought maybe if I added something like
while (response == 'Y' || response = "YES") I would be able to get this one working properly


Have you tried this? I don't see it in the code you've posted. Of course, there's a problem with it (remember the difference between = and ==

But you're doing more than you need to. "Yes" and "Y" both start with the same letter. Why worry about anything but the first character? You can use the StartsWith method to help. Read the link for an example of how to use that.

And on top of that, you can make it easier on your users by letting them enter in lower and upper case. There are two ways to do this. You can either check the first character against "Y" and "y", or you can convert the incoming values to lower case or upper case and just check one. Here's the method you'd use for making a string lower case: http://msdn.microsof...y/e78f86at.aspx

Hope that helps.

Side note: consider using a do loop instead of a while loop. They're basically the same, except a do-loop will always execute once. That way, you don't have to write your first question outside the loop, just do everything inside.
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5633
  • View blog
  • Posts: 12,086
  • Joined: 02-June 10

Re: C# Console App won't accept "Yes" and "Y" Input

Posted 19 September 2013 - 07:17 PM

Y and y are different. So comparing to upper case will only match upper case. You can use the string.ToLower method to make sure everything is as you expect.

while (response[0].ToLower() == 'y')


This would allow Yes, YES, Y, y, Yeah etc. because it is taking the first char of the string, dropping it to lowercase then comparing.
Was This Post Helpful? 1
  • +
  • -

#4 panamapapi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-September 13

Re: C# Console App won't accept "Yes" and "Y" Input

Posted 20 September 2013 - 07:33 AM

View PosttlhIn`toq, on 19 September 2013 - 07:17 PM, said:

Y and y are different. So comparing to upper case will only match upper case. You can use the string.ToLower method to make sure everything is as you expect.

while (response[0].ToLower() == 'y')


This would allow Yes, YES, Y, y, Yeah etc. because it is taking the first char of the string, dropping it to lowercase then comparing.




still Not exactly sure where I would add this line of code?
Was This Post Helpful? 0
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: C# Console App won't accept "Yes" and "Y" Input

Posted 20 September 2013 - 07:39 AM

Instead of converting inputString to a char and then using that you can use inputstring[0] to gain access to the first character of the string and test to see if that is a 'y'.

Instead of response he meant inputString.

while(inputString[0].ToLower() == 'y')


Replace your current while condition with that.
Was This Post Helpful? 1
  • +
  • -

#6 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 190
  • Joined: 26-April 10

Re: C# Console App won't accept "Yes" and "Y" Input

Posted 20 September 2013 - 07:52 AM

Maaahn!
removed by moderator


This post has been edited by Curtis Rutland: 20 September 2013 - 08:20 AM
Reason for edit:: giving out full solutions

Was This Post Helpful? 1
  • +
  • -

#7 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: C# Console App won't accept "Yes" and "Y" Input

Posted 20 September 2013 - 08:01 AM

Or even better use Curtis' method with .StartsWith

while(inputString.ToLower().StartsWith("y"))

Actually an even better way would be:

while(inputString.StartsWith("y", StringComparison.CurrentCultureIgnoreCase))

This post has been edited by andrewsw: 20 September 2013 - 10:28 AM
Reason for edit:: With not Witch ;)

Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: C# Console App won't accept "Yes" and "Y" Input

Posted 20 September 2013 - 12:55 PM

Why even both doing any of the above? when the console can read single key.
As well as pseudo-constants represent keyboard keys.
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,980
  • Joined: 08-June 10

Re: C# Console App won't accept "Yes" and "Y" Input

Posted 20 September 2013 - 12:59 PM

I was assuming that it was an assignment requirement. Otherwise, I'd usually suggest the ReadKey method as well.
Was This Post Helpful? 0
  • +
  • -

#10 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: C# Console App won't accept "Yes" and "Y" Input

Posted 20 September 2013 - 01:00 PM

To be able to accept any kind of input that the user may throw at it - such as 'yes', 'yeah' 'y' etc.

Edit - just saw that the OP just wants yes or 'y'

This post has been edited by Ryano121: 20 September 2013 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: C# Console App won't accept "Yes" and "Y" Input

Posted 20 September 2013 - 01:17 PM

I would also extract it out into a boolean returning function.

Edit: No and Yes don't appear as valid replies in the question asked to the user. It only states (Y/N).

This post has been edited by AdamSpeight2008: 20 September 2013 - 01:19 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1