Hi,
I am new to C#. I have compiled and executed the following program in Microsofr Visual Studio .NET 2003 .

using System;
publicclass Enumdemo
{
enum Color{
red,
blue,
black,
green
}

public static void Main(string[] args) {
Color Mycolor;

System.Console.Write("Enter the values: 0 for red 1 for blue 2 for black 3 for green\n");

string a = Console.ReadLine();

System.Console.WriteLine("the value of the a is {0}",a);
Mycolor = (Color) Convert.ToInt32(a);
switch(Mycolor){
case Color.blue: System.Console.WriteLine("you have choosen the value blue ");
break;
case Color.red: System.Console.WriteLine("you have choosen the value red ");
break;
case Color.black: System.Console.WriteLine("you have choosen the value black ");
break;
case Color.green: System.Console.WriteLine("you have choosen the value green ");
break;
default: System.Console.WriteLine("you have choosen no color ");
break;

}
}
}

It is not reading the input from the user for the following line

"System.Console.Write("Enter the values: 0 for red 1 for blue 2 for black 3 for green\n");

string a = Console.ReadLine();"


Its not allowing me to enter some value for "a". Its giving the
out put as
"you have choosen the value red " without entering anything.


How to get the output in a seperate console window(black window).

please write your important code and surround it with

and the rest don't
really I could not read your question!!!!!

Your enum declaration is wrong;

enum Color { red, blue, black, green };

You need to have a semicolon [;] at the end of it's declaration. Also, only enter Digits at the question prompt, because then the string is converted into integer. So anything other an integer will cause the program to break.

So you can specify to accept the data as integer only.

int a = Convert.ToInt32(Console.ReadLine());
Mycolor = (Color)(a);

It'll work too. Hope it helps.

Umm.. Sorry, in the last code you can have the integer variable without parenthesis, since we are already type-casting it to Colour.

Your enum declaration is wrong;

enum Color { red, blue, black, green };

You need to have a semicolon [;] at the end of it's declaration. Also, only enter Digits at the question prompt, because then the string is converted into integer. So anything other an integer will cause the program to break.

So you can specify to accept the data as integer only.

int a = Convert.ToInt32(Console.ReadLine());
Mycolor = (Color)(a);

It'll work too. Hope it helps.

the same code is there in my code...........but its not working.....semicolonis not there in the books.

Hi,
My frame work is not reaing input from console for simple programes too....
could you please tell me, what should i do.
I am using Microsoft visul studio .NET2003
Should i change any settings in the frame work to accept inputs from console(user)

This is the sample program i entered, its giving the following out put immediatly without accepting any input from user.

------------------------------------------------------------------------------
using System;

public class Consoledemo
{ static void Main(string[] args)
{
Console.WriteLine("Hello there!");
Console.WriteLine("Please input your name: ");
string alfa = Console.ReadLine();
Console.WriteLine("U have enterd u r name as, {0}",alfa);
Console.Read();
}
}

---------------------------------------------------------------------------------------------------------
output:

'DefaultDomain': Loaded 'c:\winnt\microsoft.net\framework\v1.1.4322\mscorlib.dll', No symbols loaded.
'latest': Loaded 'C:\Srikar\latest\bin\Debug\latest.exe', Symbols loaded.
Hello there!
Please input your name:
U have enterd u r name as,
The program '[2312] latest.exe' has exited with code 0 (0x0).

---------------------------------------------------------------------------------------------------------

Wts wrong vth my code......

Thanks and Regards.

>but its not working
>semicolon is not there in the books.
The code is correct. I checked it in my Visual Studio 2008 express, and is working just fine. You syntax for enum is wrong. It's a user defined, value type data type, right. So just like structures, it must be ended with the semicolon.

[attributes] [modifiers] enum identifier [:base-type] {enumerator-list};

Here is the syntax. I checked it would work without semicolon too, but that syntax is wrong and you are bound to get into some unknown errors with it.

>My frame work is not reading input from console
You know if that's the case, it doesn't take a genius to figure out that your visual studio has some errors. Try repairing it.

>Should i change any settings in the frame work >to accept inputs from console
ROFL, you seriously must be joking me.. OMG! there is no such thing like that, unless you played with your settings it would work. Try repairing, if that doesn't help, go into Tools > Import & Export Settings > reset settings. It should help. If not, then boy, time for you to get the new version, or do a clean installation.

>...
Why are you not including your programs in a [ code ] [/ code] tag. It's really annoying reading it. If you are not gonna follow it, then I'm afraid, no one's one gonna help you. And I for one will certainly not.

>output:...
If you are getting THAT output, I'm afarid. I'm a bit lost there. Which template are you using? Are you sure that you are doing the right things. I know it might sound stupid but sometimes you might have selected a different template so it might have happened. I personally have never used the Visual Studio .NET version, only 2005 and 2008. So you might wanna check there.

Try out repairing and resetting, see if that works. You could also download Visual Studio Express, even though it lacks advanced services, it could help you, if your just learning this language.

This article has been dead for over six months. Start a new discussion instead.