用户输入感叹号后停止接受字符

Stop accepting characters once the user enters the exclamation sign

void main()
{
    Stack *S = new Stack;
    char val;

    while(true)
    {
        cout<<"enter character:"<<endl;
        cin>>val;
        S->push(val);
    }

    S->pop();
}

我正在做 Stacks 数据结构。一旦用户输入感叹号,我要编写什么代码来停止接受字符?

输入感叹号时直接跳出循环。

void main()
{
    Stack *S = new Stack;
    char val;

    while(true)
    {
        cout<<"enter character:"<<endl;
        cin>>val;
        if (val == '!') { // exclamation point was entered
             break;
        }
        S->push(val);
    }

    S->pop();
}