CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    May 2017
    Posts
    10

    Not able to have getche() working in chcount.cpp

    Hi C++ Guru,

    I am having difficulty getting the chcount.cpp program
    to count the number of characters & words entered using getche() library function.
    This program is on page 100 of Object Oriented Programming in C++ by Robert Lafore (4th ED),
    from https://ia801303.us.archive.org/25/items/ObjectOrientedProgrammingInC4thEdition/Object-Oriented%20Programming%20in%20C++%20(4th%20Edition).pdf

    The issue after having debugged it was that ch has been assigned a random characters / numbers
    (e.g. 1, 0..), before I get to enter any character, which resulted in stucked in WHILE loop.

    Below is the code snippet:
    Code:
    // chcount.cpp
    #include <iostream>
    #include <conio.h>				// for getche()
    
    using namespace std;
    
    int main()
    {
    	int chcount = 0;				// counts non-space characters
    	int wdcount = 1;				// counts spaces between words
    	char ch = 'a';					// ensure it isn't '\r'
    
    	while( ch != '\r' ) 	//loop until Enter is typed
    	{
    //		ch = getchar(); 			// Read a character before I enter anything
    		ch = getche(); 				// Not running getche() function.
    		if ( ch == ' ' )			// if it's a space
    			wdcount++;				// count it as a word
    		else						// otherwise,
    			chcount++;				// count a character
    	}
    	cout << "\nWords=" << wdcount << endl
    	     << "Letters=" << chcount-1 << endl;
    	return 0;
    }
    getche() did not process further while getchar() simply accept random characters / numbers (e.g. 1,0)
    before I get to enter anything in.

    I have tried other functions such as getc(stdin) & getch() & lookup similar resolutions
    without success for a few hours.

    I am running Eclipse C/C++ Neon.3 (4.6.3 - 20170314-1500)
    with MinGW-w64 on Windows 10.

    Your assistance would be much appreciated.

    Thanks,

    George
    Attached Files Attached Files

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Not able to have getche() working in chcount.cpp

    I don't use Eclispe/MinGW, so can't test it with that set-up. However, the code as posted in post #1 works as expected with VS2017.

    ch has been assigned a random characters / numbers
    (e.g. 1, 0..), before I get to enter any character, which resulted in stucked in WHILE loop
    Initially, ch is assigned the value of 'a', During the loop ch is assigned the value of the entered char which is echoed back to the screen. Is the screen showing the character entered? '\r' is the CR character which terminates the line. It may be there is an issue with the value of the line terminator. Try adding this to the program
    Code:
    		ch = getche(); 				// Not running getche() function.
    		cout << (int)ch << endl;
    When you enter CR, what value is displayed on the screen? It should be 13. For test, if you enter qwe<CR> then the display should be
    Code:
    q113
    w119
    e101
    13
    
    Words=1
    Letters=3
    Note that the book Object Oriented Programming in C++ by Robert Lafore (4th ED) was published 2002 and is very out of date and can't now be recommended from which to learn c++. It covers the c++98 version of c++ which has been greatly expanded over the following years - particulary with the C++11 release. The current version is c++14 and c++17 is due for release later this year.

    As you are using Windows 10, why not the free Microsoft VS 2017 Community Edition? See https://www.visualstudio.com/downloads/
    Last edited by 2kaud; May 24th, 2017 at 03:40 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720

    Re: Not able to have getche() working in chcount.cpp

    This code works fine for me, I'm using VS2017. I believe this is nothing to do with a compiler.
    Code:
    #include <iostream>
    #include <conio.h>					// for getche()
    #include <cctype>
    
    using namespace std;
    
    int main()
    {
    	int chcount = 0;				// counts non-space characters
    	int wdcount = 0;				// counts spaces between words
    	char c;
    	while ((c = getche()) != '\r') {
    		if (std::isspace(c))
    			++wdcount;
    		else ++chcount;
    	}
    	cout << "\nWords=" << wdcount + 1 << endl \
    	<< "Letters=" << chcount - 1<< endl;
    	system("pause");
    	return 0;
    }

  4. #4
    Join Date
    May 2017
    Posts
    10

    Smile Re: Not able to have getche() working in chcount.cpp

    Thanks to the great detail response from 2kaud & AvDav,

    I am not familiar with using VS2017, hence just managed to run the debugger to
    successfully ran both programs. The original one posted built (Eclipse) & supplied by AvDav (VS2017). I did have
    to change using _getch() since compiler informed that getche() is deprecated.

    Btw, can either of you provide instruction on how to run the project / code after having
    compiled / built solution in VS2017?

    I will be writing C++ programs on Unix (Solaris & RedHat Enterprise) platforms, so it is
    important that I stay with Eclipse. However, it does appear that this is a compiler specific
    issue. Is there anyone using Eclipse IDE who can assist instead?

    Lastly, what are some current C++ books that I should use instead?

    Thanks in advance,

    George

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Not able to have getche() working in chcount.cpp

    However, it does appear that this is a compiler specific issue.
    Anything defined in conio.h IMO should be considered compiler specific - and could behave differently between different compilers on same/different systems. I would suggest looking at the compiler documentation for getche() for the compiler/system used. The preference is to avoid where possible using anything defined in conio.h as these are not c++ standard defined.

    Btw, can either of you provide instruction on how to run the project / code after having
    compiled / built solution in VS2017?
    In VS2017, after the program has compiled successfully, an .exe will have been generated. This will have the name specified under Project properties/Configuration Properties/General/target name and will be created in the folder specified by Output Directory. From the Command Prompt, this .exe can be run like any other .exe.

    For an on-line resource, consider
    http://www.learncpp.com/
    http://www.cplusplus.com/reference/

    For books, I can suggest a couple but often this comes down to readers preference. I would suggest only books published since 2015 that cover c++14.

    Starting Out with C++ by Tony Gaddis https://www.amazon.co.uk/Starting-Ou...ywords=c%2B%2B

    c++ programming D Malik https://www.amazon.co.uk/Programming...5638926&sr=1-5

    c++ How To Progam Deitel https://www.amazon.co.uk/How-Program...ywords=c%2B%2B
    Last edited by 2kaud; May 24th, 2017 at 11:18 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720

    Re: Not able to have getche() working in chcount.cpp

    Well, under VS2017 and previous versions of this environment you can simply press Ctrl+F5 to see the behavior of the program.

  7. #7
    Join Date
    May 2017
    Posts
    10

    Re: Not able to have getche() working in chcount.cpp

    Thanks to the wealth of detail information provided by 2Kaud which I will use as a reference. Nevertheless, looks like I have posted this question in the VS2017 C++ as opposed to Eclipse CDT which I should for this question.

    Good news, I managed to get the original chcount.cpp to work by adding "cout << flush;" before getche() and run it outside of Eclipse most importantly. In other word, it was Eclipse that was the issue after all.

    Also thanks to AvDav, I was expecting the output of this program to be displayed in Console as opposed to popping up the separate black output windows, that didn't give me time to examine the output. Nevertheless, <CONTROL-F5> did run the binary program from within VS2017.

    You have done outstanding work with high quality materials and quick response.

    Well done.

    George

  8. #8
    Join Date
    May 2017
    Posts
    10

    Re: Not able to have getche() working in chcount.cpp

    Hi 2kaud & AvDav,

    I have since found other issues with using Eclipse IDE so would need to switch between VS2017 to make use & test any functionalities that is lacking in Eclipse. As a result, is there a way to run just current source code / project and display output in Console like window, so I can examine the result within VS2017 as opposed to run the executable in a command prompt?

    I am not familiar with using VS2017 and hope to use it quickly where possible.

    Many thanks,

    George

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Not able to have getche() working in chcount.cpp

    is there a way to run just current source code / project and display output in Console like window, so I can examine the result within VS2017 as opposed to run the executable in a command prompt
    From your post #7
    <CONTROL-F5> did run the binary program from within VS2017
    This is how you run a console program within VS. What different/extra are you looking for?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Not able to have getche() working in chcount.cpp

    I have since found other issues with using Eclipse IDE
    Eclipse is just the IDE - not the compiler. The compiler is from the MinGW installation. This includes a port of the GNU gcc c++ compiler for windows. Issues found are unlikely to be with Eclipse but with the MinGW Windows port of the gcc compiler.

    Unfortunately as I don't use the gcc compiler (only VS under Windows), I can't help with gcc issues. If you post the details of the issues you are having with gcc under Windows some guru who uses gcc may be able to assist further.

    Cheers!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    May 2017
    Posts
    10

    Re: Not able to have getche() working in chcount.cpp

    Hi 2kaud,

    I have had to append "system("pause>nul");" just before return 0 back to delay the black output screen, to read the program output, follow by pressing <ENTER> to close / release it.

    I would have thought that functionality would be built into VS2017 instead of me having to control it from within the program.

    Thanks,

    George

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Not able to have getche() working in chcount.cpp

    Quote Originally Posted by netbeansfan View Post
    Hi 2kaud,

    I have had to append "system("pause>nul");" just before return 0 back to delay the black output screen, to read the program output, follow by pressing <ENTER> to close / release it.

    I would have thought that functionality would be built into VS2017 instead of me having to control it from within the program.

    Thanks,

    George
    It is. The issue is that within VS the sub-system hasn't been explicitly set to console.

    project/properties/Linker/System/Subsystem and change it to Console. This then displays a message before the console window closes and waits for keyboard input before the window is closed.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    Join Date
    May 2017
    Posts
    10

    Re: Not able to have getche() working in chcount.cpp

    Thanks 2kaud,

    That worked perfectly without the need to include "system("pause>null");" in the code.

    Just one last question, how to increase the font of subsystem Console (black) windows after having tried adjusting the Editor font size without success. I can't find an answer from quick searches.

    Thanks a lot,

    George

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Not able to have getche() working in chcount.cpp

    Click the icon in the top left of the console window, then properties then font then choose the required font type and size. This only sets it for the current console. If you want to set for all consoles created, then choose defaults rather than properties and set the required font/size.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    May 2017
    Posts
    10

    Re: Not able to have getche() working in chcount.cpp

    Yes, the properties font settings on console window works for all consoles while defaults did not.

    Thanks so much 2kaud,

    George

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured