C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 12th, 2004, 11:22 AM
alpha-numberic alpha-numberic is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 2 alpha-numberic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
c++ problems with getch()

Hello, I'm a novice programmer still in college, and I can't make the getch() function (from conio.h) to work properly. My understanding is that it can be used to pause a program untill the user presses a button (the "Press any key to continue" kind of thing) however, its not working for me at all.

My compiler is Microsoft visual c++ 5.0, and I've tried several approaches but none work properly. I'll post a few code snippets and tell the outcome.

cout << "Any phrase" << endl;
getch()
cout<< "another phrase"<< endl; // this works as expected

cout << "Any phrase" << endl;
getch()
cout<< "another phrase"<< endl;
getch()
cout<< "third phrase"<< endl; //This only writes the first string, then user presses enter twice and program exits

and in my real program, at the end of the main function right before return 0, I have

cout << "Press any key to continue" << endl;
getch()

and it does nothing.

Any help would be greatly appreciated, all I'm trying to do is let the user read all the final text before the dos window closes.

Reply With Quote
  #2  
Old January 12th, 2004, 05:52 PM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 785 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 17
system ("PAUSE");

Reply With Quote
  #3  
Old January 12th, 2004, 10:06 PM
dubovski dubovski is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 3 dubovski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
To pause use system("PAUSE");
and #include <stdlib.h>
Also try getchar(); instead of getch()
and #include <stdio.h>

Reply With Quote
  #4  
Old April 13th, 2004, 01:44 PM
yoyomaster yoyomaster is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 1 yoyomaster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Reply

"conio.c" might work because "conio.c" calls "conio.h"

__getch(); the functon is stored within conio, and its much better than getchar() b/c it does not conflict with your "cin >>" operator

the bad thing about __getch is that you need to __initscr() first, and once you intialize your screen, you cannot use "cin>>"

so have fun and try more things.

Reply With Quote
  #5  
Old April 17th, 2004, 06:43 AM
devnull devnull is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 1 devnull User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
getch() == Borland?

Isn't getch() only implemented in Borland's compiler? You're using VC++, so that might be your problem -- try compiling with Borland and see if that helps...or use some of the other suggestions here that might work in VC++

Reply With Quote
  #6  
Old December 27th, 2007, 02:57 AM
momentsb4autumn momentsb4autumn is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Location: Chiang Mai, Thailand
Posts: 2 momentsb4autumn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 20 sec
Reputation Power: 0
Send a message via ICQ to momentsb4autumn Send a message via AIM to momentsb4autumn Send a message via MSN to momentsb4autumn
Red face getch();


mmhmm...interesting

_getch(); would be located within the conio.h library.

I used that for one of my programs and it worked like a charm!

I had getch(); before and it kept giving me an error.

BTW, I am using Visual Studio 2008.


later dude ^_^

Reply With Quote
  #7  
Old January 1st, 2008, 12:23 PM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
Underscores preceding a name has special meanings, in terms of who decided it's supposed to be there. AFAIR two underscores is the domain of the compiler implementer, and one is defined by the standards. Meaning two underscores should not be used if you want it to work on other compilers.

---

system("PAUSE");
should work on windows, at least. Other than that, try the various functions (wasn't there a getc()? ) to see if any work.

Anyway, I noticed the thread starter didn't use ; after his getch(). Probably just a matter of writing it on the boards.

Reply With Quote
  #8  
Old February 2nd, 2008, 08:13 PM
Syzyx Syzyx is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 4 Syzyx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 45 sec
Reputation Power: 0
When using visual studio 2005, it flags a warning when I use getch(). I'm not sure if this is universal, because I don't use any other IDE.

If you're having a massive problem with it, just make your own function:

void bla()
{
cout << "Press any key to continue . . ." << endl;
cin.get();
cin.ignore();
}

this will get any input from the keyboard, and obviously, ignore it.

The only thing with this is that it does take up 6 lines where getch() takes up 1.

Reply With Quote
  #9  
Old February 3rd, 2008, 03:32 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
Microsoft decided to entice programmers, especially beginners, from using the C runtime libs, and instead use microsoft proprietary "secure" versions. Why? Probably just to have them learn to use functions that won't port to non-MS compilers and platforms.

One thing about cin (as implemented in VC++) is that it basically does not process any of the input until it has a whole line of it.
The "proper" version of the above is therefore something along the lines of
Code:
void pause()
{
  cout << "Press enter to continue" << endl;
  cin.ignore(-1,'\n');
}


I'd have to verify that -1 is appropriate input for that function though.

There's no cin.get() in that, in fact, the above expects two characters, and if the first was enter, it might be waiting for another line so it can ignore the first character of that line. (If enter is read as "\r\n" which would be windows only, then you could possibly get away with it - on windows.)

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > c++ problems with getch()


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap