473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

get "default" text from the console

Hi,
can I print some default text on the console when doing a ReadLine?

A silly example below shows two small scenarios. In the first a user
is told what he appears to be called, and asks for a name.

In the second case I want the the user to be able to just press return
to accept the username of the system. (I understand it does not work
the way I do it below - I just want to illustrate what I want).

Thanks,
P9K

---------
using System;
using System.Text;

namespace FeedMeName
{
class Program
{
static void Main(string[] args)
{
string name1 = Environment.UserName;
Console.WriteLine("Are you '{0}'?", name1);
Console.Write("name please >");
name1 = Console.ReadLine();
Console.WriteLine("Hello '{0}'!", name1);

string name2 = Environment.UserName;
Console.WriteLine("Are you '{0}'?.", name2);
Console.Write("name please >");
Console.Write(name2);
Console.CursorLeft -= name2.Length;
name2 = Console.ReadLine();
Console.WriteLine("Hello '{0}'!", name2);
}
}
}
---------
>FeedMeName.exe
Are you 'per'?
name please >my name is foobar
Hello 'my name is foobar'!
Are you 'per'?.
name please >foobar, goddamit
Hello 'foobar, goddamit'!
---------
>FeedMeName.exe
Are you 'per'?
name please >fo
Hello 'fo'!
Are you 'per'?.
name please >brr
Hello 'br'!

Apr 25 '07 #1
3 2875
per9000 <pe*****@gmail.comwrote:
can I print some default text on the console when doing a ReadLine?
ReadLine will read data. If you want to write some text on the console
before reading, just call Write or WriteLine with that text, and handle
the case where the user just presses return (i.e. you'll get an empty
line back).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 25 '07 #2
On 25 Apr, 18:58, Jon Skeet [C# MVP] <s...@pobox.comwrote:
per9000 <per9...@gmail.comwrote:
can I print some default text on the console when doing a ReadLine?

ReadLine will read data. If you want to write some text on the console
before reading, just call Write or WriteLine with that text, and handle
the case where the user just presses return (i.e. you'll get an empty
line back).
Sure, that would work fine if the user accepts the username or has a
short username. But I want to be able to treat the case when a user
wants to *change* a line. Imagine a user with a really long name like
"Boutros Boutros Boutros Boutros Boutros Boutros Boutros Boutros-
Ghali". Perhaps such a user would like to just press delete a couple
of times to get a name like "BBBBBBBB-G" . Or imagine that this is a
text-editor (like vi or nano) and a user wants to rewrite a line at a
time. If the only change to a line is a typo then it is too much to
ask of a user to rewrite the entire line.

Perhaps what I want is to write the line to standard in or something
like that. Something like this (illegal code but this is that I want):
string line = "hell_o_world() {Console.WriteLine("hell o
world") } ;"; // notice the incorrect ; in the string
Console.Write("Rewrite this line>>");
Console.In.Write(line);
line = Console.ReadLine();

/Per

Apr 26 '07 #3
On Apr 26, 11:33 am, per9000 <per9...@gmail.comwrote:
ReadLine will read data. If you want to write some text on the console
before reading, just call Write or WriteLine with that text, and handle
the case where the user just presses return (i.e. you'll get an empty
line back).

Sure, that would work fine if the user accepts the username or has a
short username. But I want to be able to treat the case when a user
wants to *change* a line.
I can't say I've seen console applications which work like that.
Normally it would put the default in square brackets, like this:

Enter administrative user name:
(User enters "Fred")
Enter helpdesk user name [Fred]:

At that point, hitting return would use the value "Fred".

Jon

Apr 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: whtevr | last post by:
Consider this HTML on an ASP page called default.asp in a virtual directory: <FORM name="Flogin" id="Flogin" action="" method=post> </FORM> Do I HAVE to put the literal "default.asp" into the...
12
by: Stefano | last post by:
Hi all, what is the correct use of the "default" attribute in XML Schema? For example: <xs:element name="myProperty" type="xs:string" default="myDefaultValue"/> What can I do with it? What...
18
by: Clark Nu | last post by:
It seems that when I define a fuction,I can set a default value to some of the peremeters.When I call the fuction without some of them,the fuction will use the default value automaticlly then...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
2
by: Keith | last post by:
I have populated (or created a collection) in a ComboBox. However - I would like to set a default value - when my form is first opened (for the ComboBox) - like "Select topic" However - when I...
4
by: sods | last post by:
Hi, I write a test code about template used for strategy. it's very similar to sample code in TC++PL 13.4.1. #include <iostream> #include <string> using std::basic_string;
1
by: welles.lo | last post by:
Hi all, I found that the default "can grow" in Crystal Reports 10 is different from the one in Crystal Report 9. In version 9, when "can grow" is disable, the words in the field are chopped...
15
by: simonoficina | last post by:
Hello all! I am a vb.net beginner in Spain. When I use VB6 ,the button object has a property called "default" that can set this button like press "ENTER" key. But in the VB.net I can't find this...
2
by: LinNan | last post by:
When C source code is compiled into an object file (.o), the functions are put into the .text section. Is it possible that I put all functions to a renamed text section whose name may be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.