如何提示用户输入并在同一行中显示某些内容?

时间:2018-05-06 13:35:24

标签: c++ formatting

基本上我现在所拥有的是:

//generates the menu
void menu(int TL, string title) {
    char a;

    //menu layout
    cout << title << endl;
    emptyLine(TL);
    centreHeading(TL, "OPTIONS");
    emptyLine(TL);
    centreText(TL, "(1) PLAY");
    centreText(TL, "(2) SET GAME LENGTH");
    centreText(TL, "(3) SET ROW LENGTH");
    centreText(TL, "(4) PEG SETTINGS");
    centreText(TL, "(9) HELP");
    centreText(TL, "(0) EXIT GAME");
    emptyLine(TL);
    cout << "|" << setw(19) << ""; cin >> a; cout << setw(19) << "|" << endl;
}

我明白这一点:

Here I use 6 as the input. Notice that the cin >> automatically moves to a new line after the input, and so the | is out of place.

我们怎样才能确保我们看到这一点?:

Notice that the input 6 is still there, but the | is on the same line as the input, circled in red.

我尝试过寻找解决方案,但似乎其他人对如何防止\ n后续的getline(或某种类型)读取\ n字符感兴趣。

感谢您的帮助。

0 个答案:

没有答案