Single Character Input

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 7290 Views - Last Post: 22 May 2010 - 07:06 AM Rate Topic: -----

#1 Muhammad.Arif   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-April 10

Single Character Input

Posted 20 May 2010 - 02:28 AM

i m searching for a method by wich i can take a character input without using conio.h
(getche())
and with out pressing enter at end

a function which is exactly similar to getche() of conio.h
Is This A Good Question/Topic? 0
  • +

Replies To: Single Character Input

#2 Allizoid   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 91
  • Joined: 09-November 09

Re: Single Character Input

Posted 20 May 2010 - 02:33 AM

Why don't you use getchar() ?

char ch;

while( (ch = getchar()) != EOF ){
   putchar(ch);
}


That will echo the keyboard input back to the user.
Was This Post Helpful? 0
  • +
  • -

#3 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Single Character Input

Posted 20 May 2010 - 02:34 AM

Will this help you?
Was This Post Helpful? 0
  • +
  • -

#4 Muhammad.Arif   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-April 10

Re: Single Character Input

Posted 21 May 2010 - 01:28 PM

View Postsarmanu, on 20 May 2010 - 01:34 AM, said:

Will this help you?


gtchar() requires enter key to be pressed after entering any character

i want take single character input from console
for example here is sample out put

Enter Your choice : (here user should press only one key)
Your required informations are : ...

program should not wait for enter key ('\n')
Was This Post Helpful? 0
  • +
  • -

#5 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Single Character Input

Posted 21 May 2010 - 02:34 PM

if your on windows you could use the getch() function in conio.h
Was This Post Helpful? 0
  • +
  • -

#6 Muhammad.Arif   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-April 10

Re: Single Character Input

Posted 21 May 2010 - 09:30 PM


i dont want use conio.h
any alternate method
Was This Post Helpful? 0
  • +
  • -

#7 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Single Character Input

Posted 21 May 2010 - 09:35 PM

Did you look at the snippet sarmanu gave you at post #3?

If not then have a look at it.

If you did then what about it was not what you wanted?
Was This Post Helpful? 0
  • +
  • -

#8 mr.coder   User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 41
  • Joined: 31-January 10

Re: Single Character Input

Posted 21 May 2010 - 09:37 PM

View PostMuhammad.Arif, on 22 May 2010 - 09:00 AM, said:


i dont want use conio.h
any alternate method


You should have a look here:
http://www.cplusplus...am/istream/get/

Hope it helps!
Was This Post Helpful? 0
  • +
  • -

#9 Muhammad.Arif   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-April 10

Re: Single Character Input

Posted 21 May 2010 - 09:37 PM

View PostAllizoid, on 20 May 2010 - 01:33 AM, said:

Why don't you use getchar() ?

char ch;

while( (ch = getchar()) != EOF ){
   putchar(ch);
}


That will echo the keyboard input back to the user.

i could not underestand loops condition while( (ch = getchar()) != EOF ) when will loop terminate
Was This Post Helpful? 0
  • +
  • -

#10 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Single Character Input

Posted 21 May 2010 - 09:46 PM

View PostMuhammad.Arif, on 22 May 2010 - 01:37 PM, said:

i could not underestand loops condition while( (ch = getchar()) != EOF ) when will loop terminate


That loop, as written, will never terminate.

If you wanted to break out of the loop when certain conditions are met then I suspect you have all the coding skills and knowledge to write code to achieve that.

Perhaps an if() test?
Perhaps a break statement?

This post has been edited by janotte: 21 May 2010 - 09:54 PM

Was This Post Helpful? 0
  • +
  • -

#11 Muhammad.Arif   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-April 10

Re: Single Character Input

Posted 22 May 2010 - 06:11 AM

actually no one understood my problem i have written a program and there are many choices for user to use that program for example user can enter data, view data, delete data etc. from a given record here is some sample output
Press 1 to enter data
Press 2 to view record
Press 3 to delete record
Press 4 to exit program (and so on some other options)

now i want that is when user just press 1 key there should be menu to enter data if user press 2 record should be viewed on screen (and so on)

how can i take input for choice from user without using conio.h
i know that using conio.h i can solve this easily like
char ch = getche();
but i want this without using conio.h as conio.h is not part of standart c++
all functions cin.get(ch) or other forms of this functions require enter key to be pressed at the end
there should no need for user to press enter key
can any one help me
Was This Post Helpful? 0
  • +
  • -

#12 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Single Character Input

Posted 22 May 2010 - 06:22 AM

Have a read here:
http://msdn.microsof...ol.keydown.aspx
Was This Post Helpful? 0
  • +
  • -

#13 Muhammad.Arif   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-April 10

Re: Single Character Input

Posted 22 May 2010 - 06:37 AM

i think this link is of .net not c++ i have not learned commands like this
Was This Post Helpful? 0
  • +
  • -

#14 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Single Character Input

Posted 22 May 2010 - 06:40 AM

It is C++ in the .NET Framework.
Really read the link.

Since we have to guess at what constraints you are putting on the program you are writing then we have to guess at what answers might be useful to you.
Was This Post Helpful? 0
  • +
  • -

#15 Muhammad.Arif   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-April 10

Re: Single Character Input

Posted 22 May 2010 - 06:48 AM

is there any other option?

This post has been edited by Muhammad.Arif: 22 May 2010 - 06:49 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2