C++ C++;字符验证问题

C++ C++;字符验证问题,c++,validation,C++,Validation,控制台应用程序中(Y/N)输入的这种验证是有效的,但如果用户不输入任何内容,只需按下“回车”按钮,则光标将不会返回其原始二维位置。(返回到其原始位置下方的行) 我不知道为什么。代码如下: char again(int col, int row) { char reply; do { gotoXY(col, row); cin >> noskipws >> reply; reply = toupper(reply); if ((reply

控制台应用程序中(Y/N)输入的这种验证是有效的,但如果用户不输入任何内容,只需按下“回车”按钮,则光标将不会返回其原始二维位置。(返回到其原始位置下方的行)

我不知道为什么。代码如下:

char again(int col, int row)
{
char reply;

do
{
    gotoXY(col, row);
    cin >> noskipws >> reply;
    reply = toupper(reply);

    if ((reply != 'Y' && reply != 'N'))
    {
        message("Must Enter Y or N ", 5, row + 3);
        clearLine(col, row);            

    //  cin.clear();
        cin.ignore(150, '\n');
    }

    cin.setf(ios::skipws);      

} while (reply != 'Y' && reply != 'N'); 

return reply;
}
有什么建议吗

这将允许您编译和查看问题:

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <Windows.h> 
#include <iomanip> 


using namespace std;

VOID gotoXY(short x, short y);
char again(int col, int row);
void clearLine(int col, int row);
void pressKey(int col, int row);
void message(char message[], int col, int row);

int _tmain(int argc, _TCHAR* argv[])
{
char  reply; 
do
{
    gotoXY(5, 13);
    cout << "Do you want to run the program again (Y/N):";
    reply = again(51, 13);

    cin.ignore(150, '\n');

} while (reply == 'Y');

return 0;
}
VOID gotoXY(short x, short y)
{
COORD c = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}


void message(char message[], int col, int row)
{
gotoXY(col, row);
cout << message;
pressKey(col, row + 2);
clearLine(col, row);
clearLine(col, row + 2);
}

void pressKey(int col, int row)
{
gotoXY(col, row);
cout << "Press any key to continue...";
_getch();
}


void clearLine(int col, int row)
{
//Used to clear prompts and user input
gotoXY(col, row);

for (int i = col; i <= 80; i++)
{
    cout << " ";
}
}

char again(int col, int row)
{
char reply;

do
{
    gotoXY(col, row);
    cin >> noskipws >> reply;
    reply = toupper(reply);

    if ((reply != 'Y' && reply != 'N'))
    {
        message("Must Enter Y or N ", 5, row + 3);
        clearLine(col, row);
        cin.setf(ios::skipws);
        //  cin.clear();
        cin.ignore(150, '\n');
    }

    /*cin.setf(ios::skipws);*/

} while (reply != 'Y' && reply != 'N');

return reply;
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
使用名称空间std;
VOID gotoXY(短x,短y);
再次显示字符(int列,int行);
无效清除线(整数列,整数行);
无效按键(整数列,整数行);
无效消息(字符消息[],整数列,整数行);
int _tmain(int argc,_TCHAR*argv[]
{
字符回复;
做
{
gotoXY(5,13);

cout这里是诀窍:当您按[T],[Enter]时,两个符号被添加到流中:“T”,“\n”。第一个是在回复时读取的,第二个是在回复时读取的,忽略(150,“\n”);

但当您只按[Enter]键时,只有“\n”被添加到流中。它被读取以应答,当控件到达
std::cin.ignore(150“\n”);
时,没有符号可从流中读取;此时,输入光标留在
clearLine()
留下它的位置,所有进一步的输入直到“\n”(或前150个符号)将被忽略


简单的(虽然不是最好的)解决方案是检查
是否(reply!='\n')cin.ignore(150,'\n');
。更好的办法是从一开始就不读取字符,而是读取
std::string
,这将消除场景中忽略的必要性。此外,请参见以下问题:

您需要提供更多上下文。您认为
消息()
clearLine()
do?当你说光标没有返回到它的当前位置时,你是指水平位置(例如从左边距到特定偏移量)还是二维位置(例如从屏幕左上侧到水平和垂直偏移量)?最后,您的程序运行在什么环境中?它是控制台应用程序吗?您是否依赖于特定的设备特性(例如,能够将光标移动到2D屏幕上的特定位置)并非所有屏幕上都有这些功能?@SkottokS:如果您提供了最低限度但足够完整的代码来理解将成功编译的代码,那就太好了。gotoXY()是一个预定义的函数(可能来自)?如果是,请指定您的操作系统、编译器以及如何运行应用程序(否则,请提供gotoXY()的源代码))。大多数“现代”函数称为gotoxy(),因此我感觉您使用的库相当过时,在现代操作系统中可能会产生奇怪的效果。您的编辑提供了更多的信息(尽管
gotoxy()
是非标准的),但会将81个字符输出到一个只有80个字符宽的终端(相当多的旧的/基本的物理终端和相当多的现代逻辑终端)将换行并向下滚动,无论您在执行此操作之前如何定位光标。定位光标的函数与
std::cout
之间通常没有直接关系,即使两者恰好影响相同的输出设备/窗口。@SkottokS如果您能够解决此问题,您应该自己回答为了跟在你后面有同样问题的人的缘故,提问并接受答案。