Why is this code coming out prematurely?

    #include <stdio.h>
    #define MAX 5
    int stk[MAX];
    int top=-1;

    main() 
     {
      char ch;
      void push();
      void pop();
     void display();

     do
     {
      printf("1. Push\n");
      printf("2. Pop\n");
      printf("3. Display\n");
      ch=getchar();

         if(ch=='1')
            push();
         if(ch=='2')
            pop();
         if(ch=='3')
            display();

    printf("Do u want to continue y/n"); 
    ch=getchar();
       }while(ch=='y'||ch=='Y');

    }

void push()
 {
   }

void pop()
 {
   }

void display()
 {
   }

The moment that I end by pressing the operation once ... the program prints "Do you want to continue y / n" and exit .... does not wait for user "y / Y"

Help Pls

+3
source share
3 answers
  • Your buffer has a new line ( \n) in it when the user presses the Enter key.
  • You call getchar()once, reading in this new line from the buffer, which is assigned chand does not equal to 'y'or 'y', so your loop ends.

, . , getchar(). . (: fgets). , getchar() , .

, , , , , . , , .

!

+8

, , 1, RETURN, (1 a newline).

newline getchar(), , Y, Y, .

( kludgy): getchar(); printf.

, . , :

#include <stdio.h>
#include <string.h>

#define OK       0
#define NO_INPUT 1
#define TOO_LONG 2
static int getLine (char *prmpt, char *buff, size_t sz) {
    int ch, extra;

    // Get line with buffer overrun protection.
    if (prmpt != NULL) {
        printf ("%s", prmpt);
        fflush (stdout);
    }
    if (fgets (buff, sz, stdin) == NULL)
        return NO_INPUT;

    // If it was too long, there'll be no newline. In that case, we flush
    // to end of line so that excess doesn't affect the next call.
    if (buff[strlen(buff)-1] != '\n') {
        extra = 0;
        while (((ch = getchar()) != '\n') && (ch != EOF))
            extra = 1;
        return (extra == 1) ? TOO_LONG : OK;
    }

    // Otherwise remove newline and give string back to caller.
    buff[strlen(buff)-1] = '\0';
    return OK;
}

 

// Test program for getLine().

int main (void) {
    int rc;
    char buff[10];

    rc = getLine ("Enter string> ", buff, sizeof(buff));
    if (rc == NO_INPUT) {
        // Extra NL since my system doesn't output that on EOF.
        printf ("\nNo input\n");
        return 1;
    }

    if (rc == TOO_LONG) {
        printf ("Input too long [%s]\n", buff);
        return 1;
    }

    printf ("OK [%s]\n", buff);

    return 0;
}

:

$ ./tstprg
Enter string>[CTRL-D]
No input

$ ./tstprg
Enter string> a
OK [a]

$ ./tstprg
Enter string> hello
OK [hello]

$ ./tstprg
Enter string> hello there
Input too long [hello the]

$ ./tstprg
Enter string> I am pax
OK [I am pax]

push, pop display:-) . , .


, , , . , , , -:

rc = getLine ("Enter string> ", buff, sizeof(buff));

Google, .

+5

A small thing to note. getChar returns int, not char. This can cause all kinds of chaos and unexpected problems, because types can be of different sizes.

0
source

Source: https://habr.com/ru/post/1760218/