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 method q0()which is caused by main()the symbol required and if the symbol is within the range [A;z] or outside, method q0() 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;
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2