Solved

Problem reading data from console

Posted on 2004-08-06
9
181 Views
Last Modified: 2010-04-15
What reasons could exist for the program not allowing me (just nothing happening) to type in input for the following:

Console.Write("SHOOT OF MOVE (S-M)? ");                  
int i = Console.Read();
0
Comment
Question by:smacca
9 Comments
 
LVL 4

Expert Comment

by:NTAC
ID: 11734962
Are you hitting the enter key?  That code works fine for me.
0
 
LVL 15

Expert Comment

by:SRigney
ID: 11737695
Are you getting a type mismatch?

Try
string inputString = Console.Read();
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11737761
Console.Read returns an int representing the char they hit ..

"The next character from the input stream, or negative one (-1) if no more characters are available."

I believe you want to use Console.ReadLine()

using System;

namespace ConsoleApplication15
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  string line = Console.ReadLine();
                  Console.Write(line);
            }
      }
}


0
 
LVL 15

Expert Comment

by:SRigney
ID: 11741113
gregory you are so smart.  You answer everything.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11741125
did you also work with VistaGen ?
0
 
LVL 15

Expert Comment

by:SRigney
ID: 11741157
No, They are both sold by Syntellect, but VistaView is their older product.  We have not upgraded or migrated to VistaGen, and there is currently no discussions about doing such.

If you'd like to continue this it's
bone86_il@yahoo.com
0
 

Author Comment

by:smacca
ID: 11741903
Still having the problem - any chance of someone looking at project code:

  http://www.chrismccormack.com/wumpus.zip

I will increase points to 500.

If you run/debug the application you will notice my problem.

Cheers.
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 125 total points
ID: 11743191
int i = Console.Read();

change this to string foo = Console.ReadLine();

whats happenning is that the stream isnt being processed until you hit enter with the .Read() ... then when yoiu go to the next Read() it still has a character in the read so it returns immediately ...

this code shows the problem ...

using System;

namespace ConsoleApplication5
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  int i;
                  char c;

                  while(true)
                  {
                        i = Console.Read ();
                        if (i == -1) Console.WriteLine("End of stream.");
                  
                        c = (char) i;
                        Console.WriteLine ("Echo: {0}", c);
                        //Console.In.ReadToEnd();
                  }
            }
      }
}

run it and then uncomment the ReadToEnd ....

There are other ways you can do this (like getc in C) but they are more complex. Let me know if you want the other method.

Cheers,

Greg
0
 
LVL 9

Expert Comment

by:s_sansanwal
ID: 11749799
I think Greg has updated fine.
But for your issue, replace start function as

            public void Start()
            {
                  //display game title
                  Console.WriteLine("\nHUNT THE WUMPUS\n\n");

                  bool ok = true;
                  Console.ReadLine();  // add this
                  while(ok)
                  {
                        //display shoot or move instructions
                        Console.Write("SHOOT OF MOVE (S-M)? ");                  
                        string i = Console.ReadLine();
                                                
                        //move
                        if ( i == "m" || i == "M")
                        {
                              ok = (this.Move() != -1);                                                            
                        }

                              //shoot
                        else if ( i == "s" || i == "S")
                        {
                              this.Shoot();
                        }
                        
//                        //move
//                        if ( (char)i == 'm' || (char)i == 'M')
//                        {
//                              ok = (this.Move() != -1);                                                            
//                        }
//
//                        //shoot
//                        else if ( (char)i == 's' || (char)i == 'S')
//                        {
//                              this.Shoot();
//                        }
                  }
            }
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now