locked
Magic Strings RRS feed

  • Question

  • I recently started a C# class and my grader has started sending back my programs because of magic strings. I don't even know what they are. Here is a recent program that was sent back to me. Could someone please explain what magic strings are and point out where they appear in my code? I have tried looking it up but everything I found was confusing and didn't help me out at all. Please be gentle. I am fairly new at programming all together. Thanks in advance.
    using System;
    
    static class Program
    {
        static void Main()
        {
            Random dice = new Random();
            string userInput = "";
            bool verifyInput = true;
    
            do
            {
                Console.Write("Would you like to roll the dice? (Y/N): ");
                userInput = Console.ReadLine();
    
                if(userInput == "yes" || userInput == "y" || userInput == "Yes" || userInput == "Y")
                {
                    int diceOne = dice.Next(1, 7);
                    int diceTwo = dice.Next(1, 7);
    
                    if (diceOne == 6 && diceTwo == 6)
                    {
                        Console.WriteLine("You rolled boxcars!\n");
                    }
                    else if (diceOne == 1 && diceTwo == 1)
                    {
                        Console.WriteLine("You rolled snake-eyes!\n");
                    }
                    else
                    {
                        Console.WriteLine("You rolled a {0} and a {1}\n", diceOne, diceTwo);
                    }
                }
                else if (userInput == "no" || userInput == "n" || userInput == "No" || userInput == "N")
                {
                    verifyInput = false;
                    break;
                }
                else
                {
                    Console.WriteLine("Invalid input. Please try again.");
                }
    
            } while (verifyInput);
    
            Console.WriteLine("\nGoodbye");
            Console.ReadLine();
        }//End Main()
    }//End class Program
    

    Thursday, August 1, 2013 3:08 AM

Answers

  • Greetings Enigmade.

    A magic string is a hidden command, like a cheat code in a game. If the user enters some particular string in exactly the right format, which they would have no way of guessing from the prompts, then the program does something "magical".

    I'm at a bit of a loss to see any real magic strings in your code. It all looks legit to me.

    The only thing that I can think of that your teacher might object to is that you are prompting the user for "(Y/N)" (which is very standard, as far as I know), but then accepting variations in a mix of full words and casing. So for instance, if the user types "Yes" then the game will work, but if he or she types "YES" it will say that the input is invalid, and the user won't know why.

    Perhaps your teacher is saying that if you have prompted the user to input Y or N, then you should only accept Y or N (and y or n, naturally), and not Yes or No. It might also be a good idea to clarify the situation in the error message.

    Console.WriteLine("Valid inputs are either 'Y' or 'N' (upper or lower case). Please try again.");

    • Proposed as answer by Christopher84 Thursday, August 1, 2013 11:36 AM
    • Marked as answer by Enigmade Thursday, August 1, 2013 2:48 PM
    Thursday, August 1, 2013 6:14 AM
  • The problem could be that if you enter YES (all chars upper case) you see an error.

    I would suggest to change the line:

    userInput = Console.ReadLine();

    into

    userInput = Console.ReadLine().ToLower();

    and 

    if(userInput == "yes" || userInput == "y" || userInput == "Yes" || userInput == "Y")
    {
                    ...
    }
    else if (userInput == "no" || userInput == "n" || userInput == "No" || userInput == "N")

    into

    if(userInput == "yes" || userInput == "y")
    {
                   ...
    }
    else if (userInput == "no" || userInput == "n")

    • Proposed as answer by dsmwb Thursday, August 1, 2013 12:24 PM
    • Marked as answer by Enigmade Thursday, August 1, 2013 2:48 PM
    Thursday, August 1, 2013 6:52 AM

All replies

  • Greetings Enigmade.

    A magic string is a hidden command, like a cheat code in a game. If the user enters some particular string in exactly the right format, which they would have no way of guessing from the prompts, then the program does something "magical".

    I'm at a bit of a loss to see any real magic strings in your code. It all looks legit to me.

    The only thing that I can think of that your teacher might object to is that you are prompting the user for "(Y/N)" (which is very standard, as far as I know), but then accepting variations in a mix of full words and casing. So for instance, if the user types "Yes" then the game will work, but if he or she types "YES" it will say that the input is invalid, and the user won't know why.

    Perhaps your teacher is saying that if you have prompted the user to input Y or N, then you should only accept Y or N (and y or n, naturally), and not Yes or No. It might also be a good idea to clarify the situation in the error message.

    Console.WriteLine("Valid inputs are either 'Y' or 'N' (upper or lower case). Please try again.");

    • Proposed as answer by Christopher84 Thursday, August 1, 2013 11:36 AM
    • Marked as answer by Enigmade Thursday, August 1, 2013 2:48 PM
    Thursday, August 1, 2013 6:14 AM
  • The problem could be that if you enter YES (all chars upper case) you see an error.

    I would suggest to change the line:

    userInput = Console.ReadLine();

    into

    userInput = Console.ReadLine().ToLower();

    and 

    if(userInput == "yes" || userInput == "y" || userInput == "Yes" || userInput == "Y")
    {
                    ...
    }
    else if (userInput == "no" || userInput == "n" || userInput == "No" || userInput == "N")

    into

    if(userInput == "yes" || userInput == "y")
    {
                   ...
    }
    else if (userInput == "no" || userInput == "n")

    • Proposed as answer by dsmwb Thursday, August 1, 2013 12:24 PM
    • Marked as answer by Enigmade Thursday, August 1, 2013 2:48 PM
    Thursday, August 1, 2013 6:52 AM
  • Why can'y you ask the grader to explain what he means by "magic string",

    or at least, give you a link to an explanation.

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Proposed as answer by Christopher84 Thursday, August 1, 2013 11:37 AM
    Thursday, August 1, 2013 10:14 AM
  • I'm sure you searched for "magic string" in one of the online search engines.  Did you understand the Wikipedia entry?  Do you see where it applies in your code?
    Thursday, August 1, 2013 10:57 AM
  • You may also change the line

    userInput = Console.ReadLine();

    into

    userInput = Console.ReadKey().KeyChar.ToString().ToLower();
    Console.WriteLine();

    Thursday, August 1, 2013 12:31 PM
  • I have. And my professor also. They both wanted me to research it out and try and figure out what they were on my own. Since I haven't been able to find anything that makes sense while searching on the internet I was hoping somebody would be able to help me understand here on the forums.
    Thursday, August 1, 2013 2:46 PM
  • I read the entry but as I stated earlier I am relatively new to programming and it didn't make enough sense for me to be able to apply it to my code.
    Thursday, August 1, 2013 2:47 PM
  • Thanks to everyone that contributed. Your explanations and suggestions really helped me understand what magic strings actually are. I think I have enough to go on now. 
    Thursday, August 1, 2013 3:00 PM