gpt4 book ai didi

c++ - 如何检查用户是否没有输入任何字符串?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:44:34 25 4
gpt4 key购买 nike

如果用户没有在用户名和密码中输入任何内容,我该如何添加另一个条件? (“空的用户名和/或密码 - 需要用户名和密码!。再试一次?(是/否)”)

这是我的。代码。

#include "stdafx.h"
#include <iostream>;
#include <string>;
using namespace std;
string username;
string password;
string choice;

int main(int argc, const char * argv[])
{
do {

Username:
std::cout << "Enter Username: ";
std::cin >> username;

if (username != "Joven")
{
std::cout << "Invalid Username. Please try again.\n\n\n";
goto choice;
goto Username;

}
Password:
std::cout << "Enter Password: ";
std::cin >> password;

if (password != "Fabricante7188")
{
std::cout << "Invalid Password. Please try again.\n\n\n";
std::cout << "Do you want to try again? y/n \n\n";
cin >> choice;
goto Password;
}
else
{
std::cout << "Correct Username and Password - Log In Successfull.\n";
break;

choice:
std::cout << "Do you want to try again? y/n \n\n";
std::cin >> choice;

}
}while (choice != "y" && choice != "n");

if (choice != "y" && choice != "n")
{
cout << "Invalid choice.\n";
goto choice;
}
system("pause");
return 0;
}`

非常感谢!

最佳答案

使用输入操作 >>> 你不能。它将阻塞,直到有一些实际的非空格输入后跟 Enter 键(或者出现错误或“文件结束”)。

C++ 标准解决方案(不求助于操作系统特定功能)是像使用例如std::getline ,去除前导(和可能的尾随)空格的输入,然后查看结果字符串是否为空。

关于c++ - 如何检查用户是否没有输入任何字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41633513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com