Sign in to follow this  

C# string/switch question

This topic is 4731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just got into C# and I can't shut this book. I really love the langauge. If I can get this working, the better :) I need to break the buffer into single words and look for them in a switch. Currently, it just allows you to write one word and it looks for it through the cases. This works, but it'd be fun if I could break up the sentence. I have no purpose for this experiment, but I just wondered if it was possible. Thanks!
		string buffer = "";
		
		while (exit != true)
		{
			Console.Write(": ");
			buffer = Console.ReadLine();
			
			switch (buffer)
			{
					case "sword": Console.WriteLine("Yes, I have one!");
								  break;
				    case "/exit": exit = true;
				    			  break;
			}
				
			
		}	

Share this post


Link to post
Share on other sites
You can do it by using whatever String functions in C# to split up a "sentence" into multiple words and store each in a vector for example. Then loop your code that you have now (if C# accepts Strings as switches). Notice that instead of "buffer" you have to use the current single-word element that you have extracted from the "sentence".

Share this post


Link to post
Share on other sites

This topic is 4731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this