home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20288 < prev    next >
Encoding:
Text File  |  1993-01-28  |  2.6 KB  |  66 lines

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