ramteen1993
ramteen1993

Reputation: 5

C# Ask user another input

I am creating a soundDex application and I need to ask the user for a second name after they have inputted the first one. I also want "error no input" if the user does not input a second name. How would I do this within my soundDex?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoundDexFinal
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = null;
            bool good = false;


            Console.WriteLine("SoundDex is a phonetic algorithm which allows a user to encode words which sound the same into digits."
                + "The program allows the user essentially to enter two names and get an encoded value.");

            while (!good) // while the boolean is true
            {

                Console.WriteLine("Please enter a name -> "); // asks user for an input
                input = Console.ReadLine();
                // Make sure the user entered something
                good = !string.IsNullOrEmpty(input); // if user enters a string which is null or empty
                if (!good) // if boolean is true
                    Console.WriteLine("Error! No input."); // displays an error to the user
            }
            soundex soundex = new soundex(); // sets new instance variable and assigns from the method
            Console.WriteLine(soundex.GetSoundex(input)); // gets the method prior to whatever the user enters   
            Console.ReadLine(); // reads the users input


        }

        class soundex
        {

            public string GetSoundex(string value)


            {
                value = value.ToUpper(); // capitalises the string
                StringBuilder soundex = new StringBuilder(); // Stringbuilder holds the soundex code or digits
                foreach (char ch in value) // gets the individual chars via a foreach which loops through the chars
                {
                    if (char.IsLetter(ch))
                        AddChar(soundex, ch); // When a letter is found this will then add a char 
                } // soundex in (parameter) is for adding the soundex code or digits 
                return soundex.ToString(); //return the value which is then converted into a .String() 
            }

            private void AddChar(StringBuilder soundex, char character) //encodes letter as soundex char and this then gets appended to the code
            {
                string code = GetSoundexValue(character);
                if (soundex.Length == 0 || code != soundex[soundex.Length - 1].ToString())
                    soundex.Append(code);
            }

            private string GetSoundexValue(char ch)
            {
                string chString = ch.ToString();

                if ("BFPV".Contains(chString))  // converts this string into a value returned as '1'
                    return "1";
                else if ("CGJKQSXZ".Contains(chString))  // converts this string into a value returned as '2'
                    return "2";
                else if ("DT".Contains(chString))  // converts this string into a value returned as '3'
                    return "3";
                else if ("L".Contains(chString))  // converts this string into a value returned as '4'
                    return "4";
                else if ("MN".Contains(chString))  // converts this string into a value returned as '5'
                    return "5";
                else if ("R".Contains(chString))  // converts this string into a value returned as '6'
                    return "6";

                else
                    return ""; // if it can't do any of these conversions then return nothing
            }
        }
    }



}

Upvotes: 1

Views: 1298

Answers (1)

Peter Duniho
Peter Duniho

Reputation: 70652

I admit, I am not entirely clear on what part precisely you're having trouble with. But the specific goal is stated clearly enough, and you've provided a sufficient code example, so…

The basic problem is "how do I require the user to input the same kind of data more than once?" The basic answer is the same as for any programming problem that involves having to repeat an action: generalize that action into a subroutine (i.e. "method" in C# parlance) that will perform that action, and call the subroutine every time you need to perform the action.

For example:

class Program
{
    static void Main(string[] args)
    {
        string input1, input2;

        Console.WriteLine("SoundDex is a phonetic algorithm which allows a user to encode words which sound the same into digits."
            + "The program allows the user essentially to enter two names and get an encoded value.");

        input1 = GetValidInput("Please enter a name -> ");
        input2 = GetValidInput("Please enter a second name -> ");

        soundex soundex = new soundex(); // sets new instance variable and assigns from the method
        Console.WriteLine(soundex.GetSoundex(input1)); // gets the method prior to whatever the user enters

        // do whatever you want with input2 as well

        Console.ReadLine(); // reads the users input
    }

    static string GetValidInput(string prompt)
    {
        while (true)
        {
            string input;

            Console.WriteLine(prompt); // asks user for an input
            input = Console.ReadLine();
            // Make sure the user entered something
            if (!string.IsNullOrEmpty(input))
            {
                return input;
            }

            Console.WriteLine("Error! No input."); // displays an error to the user
        }
    }
}

Upvotes: 1

Related Questions