How does it work getchar()?
-
Reading on the Internet, I realized that gettingchar(s) is the same std: cin, only the entrance receives one symbol, and this code has to eject "Ch= + the declared symbol." Why does this code endlessly request the introduction of symbols and brings nothing out?
int main() { char ch; ch = getchar(); cout << "Ch= " << ch << endl; system("pause"); return 0; }
-
I don't know what you want, but there's no problem with the method.
getChar()
a problem in methodq0()
which is caused bymain()
the symbol required and if the symbol is within the range[A;z]
or outside, methodq0()
It will be called again and again demanding the symbol until a gap is established.#include "stdafx.h" #include <iostream> using namespace std;
int length = 0;
char ch;void q1();
void q0() {
ch = getchar();
if ((ch >= 'A') && (ch <= 'z')) {
length++;
q0();
}
else if (ch == ' ') {
q1();
}
else {
length = 0;
q0();
}
}void q1() {
return; // ???
}int main() {
ch = getchar();
q0();
cout <<"Ch= "<<ch<<endl;
system("pause");
return 0;
}