Discuss .NET & C# programming, The Common Language Runtime (CLR), Intermediate Language (IL), Assemblies, .NET Classes and the role of C# in .NET Applications. Windows applications in .NET, Windows Forms, Windows Controls, and Custom Controls.
by siten0308 » Thu Jul 10, 2008 1:21 pm
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:
- Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1{
class Program{
static void Main(string[] args){
string firstn;
string secondn;
string thirdn;
int first;
int second;
int third;
string sign;
int signn;
begin:
switch (thirdn){
case 1:
Console.WriteLine("Please enter the first number:");
firstn = Console.ReadLine();
first = int.Parse(firstn);
Console.WriteLine("Please enter if you would like to Add, subtract, multiply, or divide?");
sign = Console.ReadLine();
signn = Int32.Parse(sign);
Console.WriteLine("Please enter the number to add with " + first);
secondn = Console.ReadLine();
second = int.Parse(secondn);
third = first + second;
Console.WriteLine(third);
Console.Read();
break;
case 2:
Console.WriteLine("Please enter the first number:");
firstn = Console.ReadLine();
first = Int32.Parse(firstn);
Console.WriteLine("Please enter if you would like to Add, subtract, multiply, or divide?");
sign = Console.ReadLine();
Console.WriteLine("Please enter the second number to subtract with " + first);
secondn = Console.ReadLine();
second = Int32.Parse(secondn);
third = first - second;
Console.WriteLine(third);
Console.Read();
break;
case 3:
Console.WriteLine("Please enter the first number:");
firstn = Console.ReadLine();
first = Int32.Parse(firstn);
Console.WriteLine("Please enter if you would like to Add, subtract, multiply, or divide?");
sign = Console.ReadLine();
Console.WriteLine("Please enter the second number to multiply with " + first);
secondn = Console.ReadLine();
second = Int32.Parse(secondn);
third = first * second;
Console.WriteLine(third);
Console.Read();
break;
case 4:
Console.WriteLine("Please enter the first number:");
firstn = Console.ReadLine();
first = Int32.Parse(firstn);
Console.WriteLine("Please enter if you would like to Add, subtract, multiply, or divide?");
sign = Console.ReadLine();
Console.WriteLine("Please enter the second number to divide with " + first);
secondn = Console.ReadLine();
second = Int32.Parse(secondn);
third = first / second;
Console.WriteLine(third);
Console.Read();
break;
default:
Console.WriteLine("Please enter a number");
}
decide:
Console.Write("Type \"continue\" to go on or \"quit\" to stop: ");
firstn = Console.ReadLine();
// switch with string type
switch (firstn){
case "continue":
goto begin;
case "quit":
Console.WriteLine("Good Bye");
Console.Read();
break;
default:
Console.WriteLine("Your input {0} is incorrect.", firstn);
goto decide;
}
-

siten0308
-
- Posts: 2
- Joined: Wed Jul 09, 2008 11:02 am
- Location: United States
by msaqib » Thu Jul 10, 2008 3:11 pm
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
- Code: Select all
string thirdn;
switch (thirdn){
case 1:
//code
case 2:
//code
}
Now the problem is you can not use string type variable in switch statement. You could use it like
- Code: Select all
string thirdn;
switch (thirdn){
case "1":
//code
case "2":
//code
}
Or you could have use int type variable like
- Code: Select all
int thirdn;
switch (thirdn){
case 1:
//code
case 2:
//code
}
C and C++ Programming Tutorials & Source Code
http://mycplus.com
-

msaqib
-
- Posts: 74
- Joined: Sun Feb 22, 2004 2:38 pm
- Location: United Kingdom
-
Return to .NET and C# Programming
Who is online
Users browsing this forum: No registered users and 0 guests