Jump to content
  • Advertisement
Sign in to follow this  
Kenny77

C# Switch Statement

This topic is 4174 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'm having a problem with switch statements in C#, where it will not output properly. I'm pretty sure the idea of this would work in C++, so I can't see what I'm doing wrong.
switch (num)
            {
                case 1 : Console.WriteLine("Num == 1"); break;
                case 2 : Console.WriteLine("Num == 2"); break;
                case 3 : Console.WriteLine("Num == 3"); break;
                default: Console.WriteLine("Number not found."); break;
            }

Share this post


Link to post
Share on other sites
Advertisement
I don't see anything wrong with this. What output are you getting? And what output where you expecting to get? Also what data type do you have for num?

Share this post


Link to post
Share on other sites
Is your application's output type a console application (check in Project -> Properties)?

If not and your application's output type is a windows application, then Console.WriteLine stuff should appear in the Output window when debugging (By default I think this is bottom left in the same tab window as Locals and Watch).

Regards,
ViLiO

Share this post


Link to post
Share on other sites
Whoops, sorry, should have been more specific. I always get "Number not found", no matter what I enter.


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num;

Console.Write("Enter a number: ");
num = Console.Read();

switch (num)
{
case 1 : Console.WriteLine("Num == 1"); break;
case 2 : Console.WriteLine("Num == 2"); break;
case 3 : Console.WriteLine("Num == 3"); break;
default: Console.WriteLine("Number not found."); break;
}

}
}
}

Share this post


Link to post
Share on other sites
Console.Read does appear to return the character code, which makes sense when you think about it [smile]

You could try something like this instead
static void Main(string[] args)
{
int num;

Console.Write("Enter a number:");
if (int.TryParse(Console.ReadLine(), out num))
{
switch (num)
{
case 1: Console.WriteLine("Num == 1"); break;
case 2: Console.WriteLine("Num == 2"); break;
case 3: Console.WriteLine("Num == 3"); break;
default: Console.WriteLine("Number not found."); break;
}
}
}
Regards,
ViLiO

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!