Thread: Simple question.. I think

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    Simple question.. I think

    I am VERY new to c# and am trying to make a simple program that will calculate the area of a square, triangle, or circle. The user will enter S for square T for triangle and so on.

    I am trying to figure out how I can accept input for both a capital S and lower case s to represent a square. Is there a way to accept both instances of user input. Currently I have a faulty if statement.

    if (userInput = 's' || 'S')


    any suggestions?

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    if (userInput == 's' || userInput == 'S')
    or use .ToLower() to convert the input to lower case and then compare.

    Don't forget: = is for assignment, == is for comparison.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    I better way is to use switch/case:
    Code:
    switch(userInput.ToLower()) {
        case 's' : DoSquareStuff(); break;
        case 't' : DoTriangleStuff(); break;
        case 'c' : DoCircleStuff(); break;
        default : Console.WriteLine("Invalid input") ; break;
    }
    You can easily add more options without the clumsy if/else if/ else if, etc.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Or maybe a dictionary!
    Code:
    Dictionary<char, Action> dict = new Dictionary<char, Action>();
    dict.Add('s', DoSquareStuff);
    dict.Add('t', DoTriangleStuff);
    dict.Add('c', DoCircleStuff);
    
    char ch = Char.ToLower(userInput);
    if(dict.Keys.Contains(ch))
      dict[ch]();
    else
      Console.WriteLine("You lose.");
    Last edited by itsme86; 04-05-2011 at 02:56 PM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by itsme86 View Post
    Or maybe a dictionary!
    Code:
    Dictionary<char, Action> dict = new Dictionary<char, Action>();
    dict.Add('s', DoSquareStuff);
    dict.Add('t', DoTriangleStuff);
    dict.Add('c', DoCircleStuff);
    
    char ch = Char.ToLower(userInput);
    if(dict.Keys.Contains(ch))
      dict[ch]();
    else
      Console.WriteLine("You lose.");
    That isn't Enterprisey enough, you really ought to be using LINQ for this...

    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're right!

    Here, I fixed it:
    Code:
        class Program
        {
            static void Main(string[] args)
            {
                char userInput = 'T';
    
                Regex pattern = new Regex("Do(?<letter>.).*Stuff");
                MethodInfo mInfo = typeof(Program).GetMethods(BindingFlags.Static | BindingFlags.NonPublic).FirstOrDefault(m => pattern.IsMatch(m.Name) && Char.ToLower(pattern.Match(m.Name).Groups["letter"].Value[0]) == Char.ToLower(userInput));
                if (mInfo == null)
                    Console.WriteLine("You lose.");
                else
                    mInfo.Invoke(null, null);
            }
    
            static void DoSquareStuff()
            {
                Console.WriteLine("Square stuff");
            }
    
            static void DoTriangleStuff()
            {
                Console.WriteLine("Triangle stuff");
            }
    
            static void DoCircleStuff()
            {
                Console.WriteLine("Circle stuff");
            }
        }
    Code:
    > SimpleQuestion.exe
    Triangle stuff
    Last edited by itsme86; 04-05-2011 at 04:26 PM. Reason: Not simple enough. Added regular expressions too.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM