Reputation: 43
Im pretty new to programming and I have a challenge but i need your help. My task is to write a program which is reading something from the console and then if its number it will print 1, if its string it will look like this (string + *). Heres my code but there is something wrong and I cant figure it out.Its a must to use Switch - Case.
static void Main(string[] args)
{
string x = Console.ReadLine();
switch (x)
{
case "int" :
int i = int.Parse(x);
i = i + 1;
Console.WriteLine(i);
break;
case "double":
double d = double.Parse(x);
d = d + 1;
Console.WriteLine(d);
break;
case "string":
string s = (x);
Console.WriteLine(s + "*");
break;
default:
break;
}
}
Upvotes: 4
Views: 2235
Reputation: 30813
switch case
does not work like that. It takes the argument's data type that you pass:
string x = Console.ReadLine();
switch(x) //x is the argument for switch
As it is. In your case x
is always a string
. Switch checks the value of the argument and find the designed case
for that value, it does not check the type of the argument and find the designed case
for that value.
But if your aim is to check if the string
is convertible to int
, double
, DateTime
, some other data types, or can only be read as string
, you should do it with TryParse
for individual data type:
int myInt;
double myDouble;
bool r1 = int.TryParse(x, out myInt); //r1 is true if x can be parsed to int
bool r2 = double.TryParse(x, out myDouble); //r2 is true if x can be parsed to double
Edit:
Since it is a must to use switch
case, then you can put the result in an integer:
int a = (r1 ? 1 << 1 : 0) + (r2 ? 1 : 0); //0 if string, 1 if int, 2 if double, 3 if int or double
using concept of bit-flag
, and make the switch
case like this:
switch (a){
case 0: //string case
Console.WriteLine(x + "*");
break;
case 1: //int case
Console.WriteLine((Convert.ToInt32(x) + 1).ToString());
break;
case 2: //double case
Console.WriteLine((Convert.ToDouble(x) + 1).ToString());
break;
case 3: //int or double case
Console.WriteLine((Convert.ToInt32(x) + 1).ToString());
break;
}
Original:
Then you can do something like this:
if (r1){ //parsable to int
//do something, like raise the number by 1
myInt += 1;
x = myInt.ToString();
} else if (r2){ //parsable to double
//do something, like raise the number by 1
myDouble += 1;
x = myDouble.ToString();
} else { //cannot be parsed to any
//do something like add `*`
x = x + "*";
}
Console.WriteLine(x);
Upvotes: 2
Reputation: 154
Hope this will work, for now it works with int,double,string we can extend
public static class Extenstions
{
public static bool IsValid<T>(this string source) where T : struct
{
MethodInfo tryParse = (MethodInfo)typeof(T).GetMember("TryParse").FirstOrDefault();
if (tryParse == null) return false;
return (bool)tryParse.Invoke(null, new object[] { source, null });
}
public static Type GetParsableType(this string source)
{
return source.IsValid<int>()&&!source.Contains(".") ? typeof(int) : source.IsValid<double>() ? typeof(double) : typeof(string);
}
}
class Program
{
static void Main(string[] args)
{
while (true)
{
string x = Console.ReadLine();
switch (x.GetParsableType().Name.ToLower())
{
case "int32":
case "int":
int i = int.Parse(x);
i = i + 1;
Console.WriteLine(i); break;
case "double":
double d = double.Parse(x);
d = d + 1;
Console.WriteLine(d); break;
case "string":
string s = (x);
Console.WriteLine(s + "*"); break;
default: ; break;
}
}
}
}
Upvotes: 1