Home › Forums › C# Programming › Cannot implicitly convert type int to string? why?
- This topic has 1 reply, 2 voices, and was last updated 16 years, 5 months ago by msaqib.
Viewing 1 reply thread
- AuthorPosts
- July 10, 2008 at 12:21 pm #2114CRaviParticipant
Hello i am still new to C#, has been a month now studying this from VS 2008 C# but now i am concentrating more on code than gui interface, i am looking right now if and else and switch, i decied to use a switch since it is new to me, it keeps giving me that error “implicitly convert type into string, for all 4 cases i made, why? all of the code is below:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798<br />using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Text;<br /><br />namespace ConsoleApplication1{<br />class Program{<br /><br />static void Main(string[] args){<br />string firstn;<br />string secondn;<br />string thirdn;<br />int first;<br />int second;<br />int third;<br />string sign;<br />int signn;<br />begin:<br />switch (thirdn){<br />case 1:<br />Console.WriteLine("Please enter the first number:");<br />firstn = Console.ReadLine();<br />first = int.Parse(firstn);<br />Console.WriteLine("Please enter if you would like to Add, subtract, multiply, or divide?");<br />sign = Console.ReadLine();<br />signn = Int32.Parse(sign);<br />Console.WriteLine("Please enter the number to add with " + first);<br />secondn = Console.ReadLine();<br />second = int.Parse(secondn);<br />third = first + second;<br />Console.WriteLine(third);<br />Console.Read();<br />break;<br />case 2:<br />Console.WriteLine("Please enter the first number:");<br />firstn = Console.ReadLine();<br />first = Int32.Parse(firstn);<br />Console.WriteLine("Please enter if you would like to Add, subtract, multiply, or divide?");<br />sign = Console.ReadLine();<br />Console.WriteLine("Please enter the second number to subtract with " + first);<br />secondn = Console.ReadLine();<br />second = Int32.Parse(secondn);<br />third = first - second;<br />Console.WriteLine(third);<br />Console.Read();<br />break;<br />case 3:<br />Console.WriteLine("Please enter the first number:");<br />firstn = Console.ReadLine();<br />first = Int32.Parse(firstn);<br />Console.WriteLine("Please enter if you would like to Add, subtract, multiply, or divide?");<br />sign = Console.ReadLine();<br />Console.WriteLine("Please enter the second number to multiply with " + first);<br />secondn = Console.ReadLine();<br />second = Int32.Parse(secondn);<br />third = first * second;<br />Console.WriteLine(third);<br />Console.Read();<br />break;<br />case 4:<br />Console.WriteLine("Please enter the first number:");<br />firstn = Console.ReadLine();<br />first = Int32.Parse(firstn);<br />Console.WriteLine("Please enter if you would like to Add, subtract, multiply, or divide?");<br />sign = Console.ReadLine();<br />Console.WriteLine("Please enter the second number to divide with " + first);<br />secondn = Console.ReadLine();<br />second = Int32.Parse(secondn);<br />third = first / second;<br />Console.WriteLine(third);<br />Console.Read();<br />break;<br />default:<br />Console.WriteLine("Please enter a number");<br />}<br /><br />decide:<br />Console.Write("Type "continue" to go on or "quit" to stop: ");<br /><br />firstn = Console.ReadLine();<br /><br />// switch with string type<br /><br />switch (firstn){<br /><br />case "continue":<br />goto begin;<br />case "quit":<br />Console.WriteLine("Good Bye");<br />Console.Read();<br />break;<br />default:<br />Console.WriteLine("Your input {0} is incorrect.", firstn);<br />goto decide;<br /><br />}<br /> - July 10, 2008 at 2:11 pm #3405msaqibParticipant
HelloYou are trying to use string type variable in your switch statement which is expecting an int type variable. Here is how you are doing
1234567string thirdn;<br />switch (thirdn){<br />case 1:<br />//code<br />case 2:<br />//code<br />}Now the problem is you can not use string type variable in switch statement. You could use it like
12345678<br />string thirdn;<br />switch (thirdn){<br />case "1":<br />//code<br />case "2":<br />//code<br />}Or you could have use int type variable like
12345678<br />int thirdn;<br />switch (thirdn){<br />case 1:<br />//code<br />case 2:<br />//code<br />}
- AuthorPosts
Viewing 1 reply thread
- The forum ‘C# Programming’ is closed to new topics and replies.