我正在尝试自学C ++,这是我正在进行的一个程序,但我遇到了密码循环问题。甚至为什么我输入正确的用户名和密码,它只是一次又一次地问我用户名而不是去虚拟菜单功能
#include <iostream>
#include <stdio.h>
#include <string>
#include <fstream>
using namespace std;
void menu(int argc, const char * argv[], char text[2000]);
string encryptDecrypt(string toEncrypt) {
char key = 'K'; //Any char will work
string output = toEncrypt;
for (int i = 0; i < toEncrypt.size(); i++)
output[i] = toEncrypt[i] ^ key;
return output;
}
void menu(int argc, const char * argv[], char text[2000])
{
system("color 0A");
//ifstream my_input_file;
ofstream my_output_file;
my_output_file.open("output_data.txt");
cout<<"Please enter your text: ";
//cin>>text;
//cin.ignore();
//cin.getline(text, sizeof text);
cin.getline(text,2000);
//cout<<"You entered: "<< text <<"\n";
//string encrypted = encryptDecrypt("kylewbanks.com");
string encrypted = encryptDecrypt(text);
cout << "Encrypted:" << encrypted << "\n";
string decrypted = encryptDecrypt(encrypted);
cout << "Decrypted:" << decrypted << "\n";
my_output_file << "Your encrypted text is: " << encrypted;
my_output_file.close();
cin.get();
}
int main()
{
string username;
string password;
do {
cout << "username: ";
getline(std::cin, username);
if (username == "John") {
std::cout << "password: ";
getline(std::cin, password);
if (password != "1234") {
cout << "invalid password. try again." << std::endl;
}
else if (password == "1234"){
void menu(int argc, const char * argv[], char text[2000]);
}
}
else {
std::cout << "invalid username. try again." << std::endl;
}
} while (password != "1234");
return 1;
}
答案 0 :(得分:4)
如果密码等于“cherry”,那么你什么也不做,只需在本地声明函数即可 菜单
else if (password == "cherry"){
void menu(int argc, const char * argv[], char text[2000]);
}
本声明
void menu(int argc, const char * argv[], char text[2000]);
不是函数调用。这是一个功能声明。
但是如果输入“1234”,则循环结束,因为它的条件是
} while (password != "1234");
编辑:我看到你更新了帖子和替换语句
else if (password == "cherry"){
的
else if (password == "1234"){
然而,实质上没有任何改变。在新陈述之后,仍然有一个函数声明。
答案 1 :(得分:1)
#include <iostream>
#include <stdio.h>
#include <string>
#include <fstream>
using namespace std; //the point of using namespace std is to avoid writing std:: every time
string encryptDecrypt(string toEncrypt) {
char key = 'K'; //Any char will work
string output = toEncrypt;
for (int i = 0; i < toEncrypt.size(); i++)
output[i] = toEncrypt[i] ^ key;
return output;
}
// better use the string class rather than an array of chars. Made it a local variable.
void menu(int argc, const char * argv[])
{
system("color 0A");
//ifstream my_input_file;
ofstream my_output_file;
my_output_file.open("output_data.txt");
cout << "Please enter your text: ";
string text;
cin >> text;
string encrypted = encryptDecrypt(text);
cout << "Encrypted:" << encrypted << "\n";
string decrypted = encryptDecrypt(encrypted);
cout << "Decrypted:" << decrypted << "\n";
my_output_file << "Your encrypted text is: " << encrypted;
my_output_file.close();
}
int main(int argc, const char * argv[])
{
string username;
string password;
//changed the do while to be
while(password != 1234) {
cout << "username: ";
cin >> username;
if (username == "John") {
cout << "password: ";
cin >> password;
//swapped comparisons to avoid 1 comparison and make the code clearer
if (password == "cherry")
menu(argc, argv); //it didn't make sense giving text as an argument
//because you then do a cin >> text in menu.
else cout << "invalid password. try again." << endl;
}
else cout << "invalid username. try again." << endl;
}
答案 2 :(得分:1)
首先,不要使用using namespace std;
。只是不要。这是不好的做法,不管其他人是否不指出你。其次,认真阅读函数的调用,制作和反馈参数,特别是如果你想复制别人的代码。即使你纠正了你的循环,如果你不知道哪些参数传递给你的menu
函数,那么它就是zilch。
现在已经开始了,请参阅以下版本的代码。
#include <iostream>
#include <string>
void menu()
{
std::cout << "I am inside the menu!" << std::endl;
}
int main()
{
std::string username;
std::string password;
std::string access = "cherry";
do {
std::cout << "Username: ";
std::getline(std::cin, username);
if (username != "John") {
std::cout << "Invalid username. Please try again.\n";
continue;
}
std::cout << "Password: ";
std::getline(std::cin, password);
if (password != access){
std::cout << "Incorrect password. Resetting access procedure.\n";
continue;
}
} while (password != access);
menu();
std::cin.get();
return 1;
}
了解我不需要将menu
置于do-while
循环中。当然,这意味着只要用户名和密码不正确,它就不会到达该部分。如何退出是你的练习。
截图:
希望这有帮助。