C++ getchar有什么问题(因为程序在do while循环中没有检查就退出)?

C++ getchar有什么问题(因为程序在do while循环中没有检查就退出)?,c++,getchar,C++,Getchar,在这个main函数中,在do while循环中使用getchar会产生问题(根据我的理解),使用getch可以解决这个问题..plz help为什么会这样 #include <iostream> #include <cstring> #include <stdio.h> using namespace std; const int size = 10; int main() { int stack[size]; int top =

在这个main函数中,在do while循环中使用getchar会产生问题(根据我的理解),使用getch可以解决这个问题..plz help为什么会这样

 #include <iostream>
 #include <cstring>
 #include <stdio.h>

using namespace std;

const int size = 10;

int main()
{
    int stack[size];
    int top = -1;
    char ch;
    char chh;
    do {
        cout << "what you want to do? 1:Push,2:Pop,3:Display \n";
        cin >> ch;

        if (ch == '1')
        {
            int a;
            cout << "enter element to be entered";
            a = getchar();
            int r = push(stack, &top, a);
            if (r == -1) cout << "array already full \n";
        }

        if (ch == '2')
        {
            pop(stack, &top);
        }
        if (ch == '3')
        {
            display(stack, &top);
        }
        cout << "enter y to continue";
        chh = getchar();
    } while (chh == 'y');

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
常数int size=10;
int main()
{
int堆栈[大小];
int top=-1;
char ch;
char chh;
做{
cout>ch;
如果(ch='1')
{
INTA;

cout一些else子句会有所帮助。将所有这些if放入一个大if/else if/else循环:

if (ch == '1') { ... }
else if (ch == '2') { ... }
else if (ch == '3') { ... }
else { /*print out the bad char*/ }
你可能会得到一个你并不期待的角色,比如一个回车

另外,为什么要混合cin和getc?

可能是重复的