Home > front end >  String doesnt seem to return
String doesnt seem to return

Time:05-30

The string in NewString doesnt seem to return to UserType() function. Is it wrong to declare that variable there? I want to return values to UserType so I can use it in another function like the Vowel function.


using System;

namespace prob1{
    class Pogram{
        static void Main(string[] args){
            UserType();
        }

        static void Menu(){
            Console.WriteLine("\nChoice of Operation:");
            Console.WriteLine("1. Enter new/another string");
            Console.WriteLine("2.  Count vowels in string and display result");
            Console.WriteLine("3. Count consonants in string and display result");
            Console.WriteLine("4. Convert string to uppercase letters and display");
            Console.WriteLine("5. Convert string to lowercase letters and display");
            Console.WriteLine("6. Count number of words in the string");
            Console.WriteLine("7. Exit Program");
        }

        static void UserType(){
            string mainString = System.String.Empty;
            Menu();
            int menuChoice;
            menuChoice = Int32.Parse(Console.ReadLine());
            
                 switch (menuChoice)
            {
                case 1:
                    NewString(mainString);
                    
                    UserType();
                    break;
                case 2:
                    Vowel(mainString);
                    UserType();
                    break;
                default:
                    break;
            }
            
        }

        static string NewString(string mainString){
            Console.WriteLine("Enter a new string: ");
            mainString = Console.ReadLine().ToLower();
      
            return mainString;
        }

        static void Vowel(string mainString){
            int total = 0;
            var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
            
    
            for (int finder = 0; finder < mainString.Length; finder  )
            {
                if (vowels.Contains(mainString[finder]))
                {
                total  ;
                }
            }

            Console.WriteLine("total: "   total);
            
            Console.ReadKey(true);

        }
    }
}

CodePudding user response:

You call NewString(mainString) method but didn't assign the returned value from the method back to mainString. So in UserType method, the mainString value won't be updated based on the result from NewString(mainString).

case 1:
    NewString(mainString);
    Console.WriteLine("test menu string: "  mainString);
    UserType();
    break;

You need is to assign the result NewString(mainString) back to mainString.

mainString = NewString(mainString);

CodePudding user response:

You need to add a string parameter with a default value of String.Empty() or, "", to UserType like so -

static void UserType(string inputString = ""){...}

Check to see if inputString is NullOrEmpty and if it is, then Show the Main Menu. If it is not empty, then pass it into Vowel() and save its return value to a variable to be used again -

var userString = Vowel(inputString);

Then in your switch statement when the value is returned to userString, you can pass that into UserType like this-

UserType(userString)
  • Related