如果用户没有在用户名和密码中添加任何内容,我将如何添加其他条件? (“空用户名和/或密码 - 需要用户名和密码!再试一次?(是/否)”)
这是我的。代码。
#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;
}`
非常感谢!
答案 0 :(得分:0)
使用输入操作>>
,您无法做到。它会阻塞,直到有一些实际的非空格输入后跟 Enter 键(或者出现错误或&#34;文件结尾&#34;)。
C ++标准解决方案(不依赖于OS特定功能)是读取整个内容,例如使用例如std::getline
,删除前导(可能是尾随)空格的输入,然后查看结果字符串是否为空。
答案 1 :(得分:0)
我的解决方案是这样的:( scorch编辑2017/01/14 10:24 AEST)
//#include "stdafx.h" I made this a comment because I don't appear to have this file. It wasn't necessary for me. Don't know whether you need it or not.
// include statements must not end with semicolons.
#include <iostream>
#include <string>
#include <cstdlib> // Has system() function
using namespace std;
int main(int argc, const char * argv[])
{
// Always declare variables inside the function.
string username;
string password;
string choice;
bool finished = false; // Declare this.
bool askingToTryAgain = false; // For when the user is asked whether they want to try again.
do {
std::cout << "Enter Username: ";
std::getline(std::cin, username);
std::cout << "Enter Password: ";
std::getline(std::cin, password);
// Validate username and password.
if (username != "Joven" && password != "Fabricante7188") {
// If both the username and password are invalid, report it.
std::cout << "Invalid Username and Password. Try again? (y/n): ";
} else if (username == "Joven" && password == "Fabricante7188") {
// If both fields are valid, login is successful.
std::cout << "Correct Username and Password - Log In Successful.\n";
finished = true; // Login is now complete, the loop will end.
} else {
// If just one of the fields is invalid, report which one it is.
if (username != "Joven") {
std::cout << "Invalid Username. Try again? (y/n): ";
}
if (password != "Fabricante7188") {
std::cout << "Invalid Password. Try again? (y/n): ";
}
}
if (finished == false) {
// If the login was unsuccessful, await user input for whether they wish to try again or not.
askingToTryAgain = true;
do {
// Fetch user input (y/n)
std::getline(std::cin, choice);
// Validate it.
if (choice != "y" && choice != "n") {
std::cout << "Enter 'y' or 'n'\n";
} else {
askingToTryAgain = false;
if (choice == "y") {
// Nothing to do here. The parent loop will continue after this one stops.
} else if (choice == "n") {
finished = true; // The user wishes to quit.
}
}
} while (askingToTryAgain);
}
} while (finished == false);
system("pause"); // During testing I used 'sleep 4' (sleep for 4 seconds) because I'm running Linux.
return 0;
}
另外,请帮个忙,避免使用'goto'作为控制程序执行流程的方法。在结构化编程语言(如C ++)中,循环和条件是比'goto'语句更好的解决方案。
我建议您查看http://cplusplus.com/作为一个很好的参考工具。
希望这有帮助。