We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,644 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

how to execute event without pressing Enter key?

Hi! I want to execute a method without pressing the enter key, what I want to do is get the press key an save it in an array while the user is typing, but I dont know how to execute an event without pressing the enter key

here is my code:

void productor(){
    char tecla=getchar();
    while (contador<50){ 
        buffer[contador]=tecla;
        contador++;
        }
    }   
6
Contributors
9
Replies
23 Hours
Discussion Span
9 Months Ago
Last Updated
11
Views
montjoile
Light Poster
38 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Standard C++ does not allow this. Only a couple compilers have extensions that will, but learning to rely on non-standard functionality hurts you when you need to use different compilers.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

Won't getch() do the trick?
something like his while loop
with getch as the first command everytime?

or am i missing something?

char charArray[50]
int k=0
while(k<50)
---charArray[k]=getch()
---k++

Despairy
Junior Poster
123 posts since Mar 2011
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 0

why do you want to do this?

Haifaa
Newbie Poster
2 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Won't getch() do the trick?

It's not part of the standard library. Microsoft includes getch() it in the conio.h header, but I'm not sure what's used (if anything) with other compilers/operating systems.

nmaillet
Posting Pro
542 posts since Aug 2008
Reputation Points: 111
Solved Threads: 104
Skill Endorsements: 4

Are my eyes tricking me? where did he say he doesnt want to use the non standard libraries?
he even used it in his code just in the wrong place :O
though i could be wrong >.<

Despairy
Junior Poster
123 posts since Mar 2011
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 0

where did he say he doesnt want to use the non standard libraries?

He didn't.

We prefer to suggest standard functionality rather than relying on tricks and non-standard techniques because you cannot transfer the non-standard skills to other compilers. Especially things like getch().

Also, if he's using g++, Watcom, or any of a couple hundred other compilers, using getch() isn't even an option. So we keep our help standard unless specifically asked.

he even used it in his code just in the wrong place :O
though i could be wrong >.<

Yes, you are wrong. His code is standard.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

oh my bad, guess i have to lower the resolution >.<
so anyway no option to get stream from the keyboard?
how sad :O!

Despairy
Junior Poster
123 posts since Mar 2011
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 0

so anyway no option to get stream from the keyboard?

No portable option. There are non-portable options, but we kind of have to know what OS and compiler are being targeted before any viable options can be suggested.

deceptikon
Challenge Accepted
Administrator
3,505 posts since Jan 2012
Reputation Points: 824
Solved Threads: 481
Skill Endorsements: 58

yes, I am using gcc compiler and I want to use standar libraries for the solution, that I am seeing it's no possible to do it, but thanks anyways!

montjoile
Light Poster
38 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0890 seconds using 2.81MB