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.
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
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.
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!
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?
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.
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!
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.
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.
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!
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!
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.
Re: Not able to have getche() working in chcount.cpp
Originally Posted by netbeansfan
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!
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.
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!
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.