Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem reading data from console

Posted on 2004-08-06
9
Medium Priority
?
227 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 500 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

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!
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.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

876 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