Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple question!

Status
Not open for further replies.

fireemt1

Technical User
Jul 14, 2003
16
US
Here is a simple question, if you know what your doing. Obviously I dont. Trying to learn C#, but for some reason, I cant get the 2nd ReadLine block of code to work in the following code. Any advice would be greatly appreciated.

using System;

namespace MyConsoleApplication
{
class Class1
{
[STAThread]
public static void Main()
{
//display "Hello World!" on the screen
System.Console.WriteLine("Hello World!");

//display the current date and time
System.Console.WriteLine("The date and time is " + System.DateTime.Now);

//displays user to enter a character, assigns variable with char and displays
System.Console.Write("Please enter a character: ");
char userInput = (char) System.Console.Read();
System.Console.WriteLine("You entered the letter " + userInput);


//displays user to enter string, assigns variable with string and displays
System.Console.Write("Enter a string: ");
string myString = System.Console.ReadLine();
System.Console.WriteLine("You entered " + myString);



}
}
}
 
Sorry, I wasnt clear enough. What happens is the 2nd readline() doesnt seem to try and capture any input. This is what is shows on the screen

Enter a string: You entered
---program ends---
 
Code:
using System;

namespace MyConsoleApplication
{
  class Class1
    {
      [STAThread]
      public static void Main()
        {

        char userInput;
	string myString;

        //display "Hello World!" on the screen
        Console.WriteLine("Hello World!");
            
        //display the current date and time
        Console.WriteLine
          ("The date and time is {0}", System.DateTime.Now);
        
       //displays user to enter a character
       //assigns variable with char and displays
       Console.Write("Please enter a character: ");
            userInput = Char.Parse(Console.ReadLine());
       Console.WriteLine
               ("You entered the letter {0}", userInput);

       //displays user to enter string
       //assigns variable with string and displays
        Console.Write("Enter a string: ");
            myString = Console.ReadLine();
        Console.WriteLine("You entered {0}",myString);
             
	Console.Read();    
        }
    }
}

This should work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top