Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. getchar() ?

getchar() ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kuniva
    wrote on last edited by
    #1

    Can someone please explain to me why, when u exceed 20 characters input, it doesnt stop but just keeps on accepting input, and somehow ends up passing through the loop a few times instantaneously? I mean.. just try typing in some 60 characters or so to see what i mean:

    #include <stdio.h>

    int IsPalindrome(const char*);

    int main(void)
    {
    char str[21]={NULL};
    int i=0;
    char c=NULL;

    do
    {
    if(str[0]!=NULL)
    {
    if(IsPalindrome(str))
    printf("This is a palindrome!\n");
    else
    printf("This isn't a palindrome.\n");
    }
    i = 0;
    while((c=getchar())!='\n' && i<21)
    {
    if((c>='a' && c<='z') || c=='$')
    {
    str[i] = c;
    i++;
    }
    }
    str[i] = NULL;
    } while(str[0]!='$');

    return 0;
    }

    int IsPalindrome(const char* str)
    {
    int len=0, i=0;
    // Get string length
    while(str[len++]!=NULL) {}
    len--;

    for(i=0;i

    Kuniva

    D A 2 Replies Last reply
    0
    • K Kuniva

      Can someone please explain to me why, when u exceed 20 characters input, it doesnt stop but just keeps on accepting input, and somehow ends up passing through the loop a few times instantaneously? I mean.. just try typing in some 60 characters or so to see what i mean:

      #include <stdio.h>

      int IsPalindrome(const char*);

      int main(void)
      {
      char str[21]={NULL};
      int i=0;
      char c=NULL;

      do
      {
      if(str[0]!=NULL)
      {
      if(IsPalindrome(str))
      printf("This is a palindrome!\n");
      else
      printf("This isn't a palindrome.\n");
      }
      i = 0;
      while((c=getchar())!='\n' && i<21)
      {
      if((c>='a' && c<='z') || c=='$')
      {
      str[i] = c;
      i++;
      }
      }
      str[i] = NULL;
      } while(str[0]!='$');

      return 0;
      }

      int IsPalindrome(const char* str)
      {
      int len=0, i=0;
      // Get string length
      while(str[len++]!=NULL) {}
      len--;

      for(i=0;i

      Kuniva

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Change the while loop to:

      while((c = getche()) != 0x0d && i<21)

      For the IsPalindrome() function, why not just call strrev() followed by strcmp()?


      "Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow

      K 1 Reply Last reply
      0
      • K Kuniva

        Can someone please explain to me why, when u exceed 20 characters input, it doesnt stop but just keeps on accepting input, and somehow ends up passing through the loop a few times instantaneously? I mean.. just try typing in some 60 characters or so to see what i mean:

        #include <stdio.h>

        int IsPalindrome(const char*);

        int main(void)
        {
        char str[21]={NULL};
        int i=0;
        char c=NULL;

        do
        {
        if(str[0]!=NULL)
        {
        if(IsPalindrome(str))
        printf("This is a palindrome!\n");
        else
        printf("This isn't a palindrome.\n");
        }
        i = 0;
        while((c=getchar())!='\n' && i<21)
        {
        if((c>='a' && c<='z') || c=='$')
        {
        str[i] = c;
        i++;
        }
        }
        str[i] = NULL;
        } while(str[0]!='$');

        return 0;
        }

        int IsPalindrome(const char* str)
        {
        int len=0, i=0;
        // Get string length
        while(str[len++]!=NULL) {}
        len--;

        for(i=0;i

        Kuniva

        A Offline
        A Offline
        Andrew Peace
        wrote on last edited by
        #3

        The problem here is that you've allocated a char array of size 21, i.e. with subscripts 0 to 20. Then, you're accepting input of length up to 20 characters, to fill the array you created. This is where the problem occurs, because in actual fact you should only be accepting 19 characters of input, since the twentieth needs to be the NULL string terminator. To clarify, note that str[21] is NOT inside the bounds of the array. Hope this helps, -- Andrew.

        K 2 Replies Last reply
        0
        • D David Crow

          Change the while loop to:

          while((c = getche()) != 0x0d && i<21)

          For the IsPalindrome() function, why not just call strrev() followed by strcmp()?


          "Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow

          K Offline
          K Offline
          Kuniva
          wrote on last edited by
          #4

          Because we couldn't use string functions. And I don't see how u explain what happens here.. Kuniva --------------------------------------------

          D 1 Reply Last reply
          0
          • A Andrew Peace

            The problem here is that you've allocated a char array of size 21, i.e. with subscripts 0 to 20. Then, you're accepting input of length up to 20 characters, to fill the array you created. This is where the problem occurs, because in actual fact you should only be accepting 19 characters of input, since the twentieth needs to be the NULL string terminator. To clarify, note that str[21] is NOT inside the bounds of the array. Hope this helps, -- Andrew.

            K Offline
            K Offline
            Kuniva
            wrote on last edited by
            #5

            Thats not what I was asking, it doesn't have anything to do with my question. What does the size of the array have to do with the while loop not really running? If u don't believe me change it and see for urself. What I was asking is why when u enter like 45 characters on the command line and press enter, it'll print three lines of output. It's supposed to simply stop at 20 chars (or 19 if u fix it whatever) and show the output, but it doesnt, it just keeps on accepting input even though i should become larger than 20, its like it doesnt execute the body of the loop at all.. Kuniva --------------------------------------------

            1 Reply Last reply
            0
            • A Andrew Peace

              The problem here is that you've allocated a char array of size 21, i.e. with subscripts 0 to 20. Then, you're accepting input of length up to 20 characters, to fill the array you created. This is where the problem occurs, because in actual fact you should only be accepting 19 characters of input, since the twentieth needs to be the NULL string terminator. To clarify, note that str[21] is NOT inside the bounds of the array. Hope this helps, -- Andrew.

              K Offline
              K Offline
              Kuniva
              wrote on last edited by
              #6

              NVM, got it, the getchar() function just behaves differently from what I anticipated. It doesn't just wait for one key to be pressed and then return the result like i thought, instead u can enter as many chars as u want untill u hit return and they all end up in the input buffer, and it returns only the first char u enterred.. kinda weird but whatever. Kuniva --------------------------------------------

              1 Reply Last reply
              0
              • K Kuniva

                Because we couldn't use string functions. And I don't see how u explain what happens here.. Kuniva --------------------------------------------

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                Kuniva wrote: Because we couldn't use string functions. Fair enough. Kuniva wrote: And I don't see how u explain what happens here.. What's to explain? I trust you know that getchar() buffers input while getche() does not. That's why I suggested using it.


                "Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow

                K 1 Reply Last reply
                0
                • D David Crow

                  Kuniva wrote: Because we couldn't use string functions. Fair enough. Kuniva wrote: And I don't see how u explain what happens here.. What's to explain? I trust you know that getchar() buffers input while getche() does not. That's why I suggested using it.


                  "Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow

                  K Offline
                  K Offline
                  Kuniva
                  wrote on last edited by
                  #8

                  Yea.. my bad, sorry. Kuniva --------------------------------------------

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups