If 语句未完成代码并跳过获取用户输入
If statement does not complete code and skips getting user input
#include <iostream>
#include <cstdlib>
#include <crime>
#include <stream>
#include <cmath>
using namespace std;
char game;
char username;
char passwordOnline;
int password = 2427;
int password2 = 2724;
int answer1;
int answers;
int main()
{
cout << "Hello Welcome to PasswordHolder by ItsScrandy \n";
cout << "Please Enter The First Password: \n";
cin >> answer1;
cout << "Please Enter The Second Password \n";
cin >> answer2;
if (answer1 == password && answer2 == password2)
{
cout << "What Is The Game Called? \n";
cin >> game;
cout << "What Is The Username/Email? \n";
cin >> username;
cout << "What Is The Password? \n";
cin >> passwordOnline;
}
if (answer1 == password || answer2 == password2)
{
cout << "One of the password's you have enterd is incorrect";
}
else {
cout << "Wrong Password";
}
//creating a .txt file
ofstream pctalk;
pctalk.open("Login Details.txt", ios::app);
//actually logging
pctalk << "Game: " << game << " | " << "Username/Email: " << username << " | " << "Password: " <<
passwordOnline << "\n";
//closing our file
pctalk.close();
return 0;
}
当我 运行 编码时,我的程序似乎运行良好,直到它要求用户提供游戏。获得输入后
它自动 运行s if 语句的其余部分。然而,如果语句被 bing 跳过,而其余代码 运行 则发生的是辅助输入。谁能告诉我为什么这些 if 语句没有正确执行?
您正在对密码的第一条和第二条语句进行相同的评估。在第一个声明中:
if (answer1 == password && answer2 == password2)
第二个:
if (answer1 == password || answer2 == password2)
是一样的。试试这个用于第二个 if 语句:
if (answer1 != password || answer2 != password2)
正如其他人所说
你使用 char
而不是 std::string
您的代码中有 3 个 cin
cout << "What Is The Game Called? \n";
cin >> game;
cout << "What Is The Username/Email? \n";
cin >> username;
cout << "What Is The Password? \n";
cin >> passwordOnline;
如果你的游戏名称有超过 3 个字符,每个 cin 给 1 char
因为你只读取了 1 个字符,而其他字符保留在 cin 缓冲区中
如果你改变这行
char game;
char username;
char passwordOnline;
到
std::string game;
std::string username;
std::string passwordOnline;
它将正常工作
#include <iostream>
#include <cstdlib>
#include <crime>
#include <stream>
#include <cmath>
using namespace std;
char game;
char username;
char passwordOnline;
int password = 2427;
int password2 = 2724;
int answer1;
int answers;
int main()
{
cout << "Hello Welcome to PasswordHolder by ItsScrandy \n";
cout << "Please Enter The First Password: \n";
cin >> answer1;
cout << "Please Enter The Second Password \n";
cin >> answer2;
if (answer1 == password && answer2 == password2)
{
cout << "What Is The Game Called? \n";
cin >> game;
cout << "What Is The Username/Email? \n";
cin >> username;
cout << "What Is The Password? \n";
cin >> passwordOnline;
}
if (answer1 == password || answer2 == password2)
{
cout << "One of the password's you have enterd is incorrect";
}
else {
cout << "Wrong Password";
}
//creating a .txt file
ofstream pctalk;
pctalk.open("Login Details.txt", ios::app);
//actually logging
pctalk << "Game: " << game << " | " << "Username/Email: " << username << " | " << "Password: " <<
passwordOnline << "\n";
//closing our file
pctalk.close();
return 0;
}
当我 运行 编码时,我的程序似乎运行良好,直到它要求用户提供游戏。获得输入后 它自动 运行s if 语句的其余部分。然而,如果语句被 bing 跳过,而其余代码 运行 则发生的是辅助输入。谁能告诉我为什么这些 if 语句没有正确执行?
您正在对密码的第一条和第二条语句进行相同的评估。在第一个声明中:
if (answer1 == password && answer2 == password2)
第二个:
if (answer1 == password || answer2 == password2)
是一样的。试试这个用于第二个 if 语句:
if (answer1 != password || answer2 != password2)
正如其他人所说
你使用 char
而不是 std::string
您的代码中有 3 个 cin
cout << "What Is The Game Called? \n";
cin >> game;
cout << "What Is The Username/Email? \n";
cin >> username;
cout << "What Is The Password? \n";
cin >> passwordOnline;
如果你的游戏名称有超过 3 个字符,每个 cin 给 1 char
因为你只读取了 1 个字符,而其他字符保留在 cin 缓冲区中
如果你改变这行
char game;
char username;
char passwordOnline;
到
std::string game;
std::string username;
std::string passwordOnline;
它将正常工作