Solved

Problem reading data from console

Posted on 2004-08-06
9
198 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 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