home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!rpi!batcomputer!munnari.oz.au!hp9000.csc.cuhk.hk!uxmail!cssu40!pmchoi
- From: pmchoi@uxmail.ust.hk (Sam P. M. Choi,Rm 4001A,ph# x7024,no way!!!)
- Subject: Re: Fairly simple text-entry help needed
- Message-ID: <1993Jan27.073603.23653@uxmail.ust.hk>
- Sender: usenet@uxmail.ust.hk (usenet account)
- Reply-To: pmchoi@uxmail.ust.hk
- Organization: Hong Kong University of Science and Technology
- References: <Dean_Wagner.47v0@aldhfn.akron.oh.us>
- Date: Wed, 27 Jan 1993 07:36:03 GMT
- Lines: 53
-
- In article 47v0@aldhfn.akron.oh.us, Dean_Wagner@aldhfn.akron.oh.us (Dean_Wagner) writes:
- > I'm working on a program, and I want a person to be able to enter their name
- > (up to 30 characters).
- > scanf doesn't work because you can only enter non-whitespace. I could
- > probably translate the whitespace by scanning one character at a time, but
- > that can't be the easiest way to do it.
- > gets won't work becauI can't check for max position.
- > getche isn't working either...here's what I'm doing now...
- >
- > while ((i < 29) && (string[i]))
- > i++;
- > string[i] = getche();
- >
- > printf("%s", string);
- >
- >
- > I know this isn't technically correct, but you see what I'm doing. The
- > manual I read says while (string[i]) should work, but it doesn't seem to be,
- > so I changed it to while {string[i] != '\r') || string[i] != '\n'
- >
- > I think it should register \r as enter, but I'm not sure.
- > It prints absolutely nothing for the printf statement in this form. Can
- > anybody suggest any fixes, or any alternate ways of doing this?
- >
- > Just for arguement, I'm using Microsoft C v600a, running under os2. I'd put
- > it under DOS, but both the compiler and DOS would need re-installed, and I
- > just don't have that kind of time while at work.
- >
- > Thanks...
- >
- > --
- > Dean Wagner Nothing matters but the weekend
- > Dean_Wagner@aldhfn.akron.oh.us From a Tuesday point of view
- > bx451@cleveland.freenet.edu -Switch into Glide, The Kings
-
-
- Did you try the following?
-
- char string[31]; /* reserve one char for NULL */
-
- scanf("%30[^\n]", string);
- /* read in 30 chars (including white space) or less if <cr> is encountered */
-
- which should work on any ANSI C conformant compiler.
-
-
- ,----------------------------v-------------------------.
- | Sam P. M. Choi | pmchoi@uxmail.ust.hk |
- | Dept of Computer Science | Tel: (852)358-7024 |
- | HKUST, Clear Water Bay | Fax: (852)358-1477 |
- | Hong Kong | |
- `----------------------------^-------------------------'
-
-