Solved

Problem reading data from console

Posted on 2004-08-06
9
217 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

732 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