Solved

Problem reading data from console

Posted on 2004-08-06
9
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 15

Expert Comment

by:SRigney
ID: 11741113
gregory you are so smart.  You answer everything.
0
 
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 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