I need help. I wrote a code that let's you to sing in with a username and a password and then to log in. A part of a program check if the username and password you enter when you log in (which are stored in different .txt files) match (if they are at the same line ). The code seems to be good but that function that checks the password and the username has a bug. It returns true anyway. Can you help me? Please! I know it's a lot of code but I have been trying for a week and I need help!!
#include<iostream>
#include<fstream>
#include<Windows.h>
#include<string>
#include<cstdlib>
#include<vector>
using namespace std;
string password[10000],username[10000],password_to_check,username_to_check,username_to_check1;
int number_of_user, number_track_keeper=0;
string username_for_log_in, password_for_log_in;
int line_for_user_track_keeper=0, line_for_password_track_keeper=0;
void sing_in();
void number();
void write_data(string a, string b);
void number_to_increase();
bool check_for_sing_in();
bool log_in();
bool user_to_log_in(string C);
bool password_to_log_in(string D);
bool password_to_log_in(string D) {
ifstream ps("passwords.txt");
getline(ps,password_to_check,'n');
vector <string> move1;
if(password_to_check != password_for_log_in && line_for_user_track_keeper != line_for_password_track_keeper) {
while(password_to_check != password_for_log_in && line_for_user_track_keeper != line_for_password_track_keeper) {
line_for_password_track_keeper++;
getline(ps,password_to_check);
move1.push_back(password_to_check);
if(password_to_check == password_for_log_in && line_for_user_track_keeper == line_for_password_track_keeper) {
return true;
}
}
} else {
return true;
}
return false;
}
bool user_to_log_in(string C) {
ifstream us("usernames.txt");
getline(us,username_to_check1,'n');
vector <string> move1;
if(username_to_check1!=username_for_log_in) {
while(username_to_check1 != username_for_log_in) {
line_for_user_track_keeper++;
getline(us,username_to_check1,'n');
move1.push_back(username_to_check1);
if(username_to_check1==username_for_log_in) {
return true;
}
}
} else {
return true;
}
return false;
}
bool log_in() {
cout<<"Enter the username:";cin>>username_for_log_in;
cout<<"Enter the password:";cin>>password_for_log_in;
user_to_log_in(username_for_log_in);
password_to_log_in(password_for_log_in);
if(user_to_log_in(username_for_log_in) == true && password_to_log_in(password_for_log_in) == true) {
cout << "You have succesfully logged in! :)" << endl;
return true;
} else{
cout<<"The data that you entered doesn't match or you are not registered yet! :(" << endl;
return false;
}
}
void number() {
ifstream num("number_of_user.txt");
if(num.is_open()) {
num >> number_of_user;
num.close();
}
}
void number_to_increase() {
ofstream num("number_of_user.txt");
if(num.is_open()) {
number_of_user++;
num << number_of_user;
num.close();
}
}
bool check_for_sing_in() {
ifstream us("usernames.txt");
getline(us, username_to_check,'n');
vector <string> track_keeper;
if(username_to_check != username[number_of_user]) {
while(username_to_check != username[number_of_user] && number_of_user != number_track_keeper) {
getline(us,username_to_check,'n');
number_track_keeper++;
track_keeper.push_back(username_to_check);
if((number_track_keeper==number_of_user)) {
break;
}
}
}
if(username_to_check!=username[number_of_user]) {
return true;
}
if(username_to_check==username[number_of_user]) {
return false;
}
return false;
}
void write_data(string a, string b) {
if(number_of_user!=0) {
check_for_sing_in();
if(check_for_sing_in()==true) {
ofstream us("usernames.txt",ios::app);
us << username[number_of_user] << endl;
us.close();
ofstream ps("passwords.txt",ios::app);
ps << password[number_of_user] << endl;
ps.close();
Sleep(2000);
cout << "Your data has been succesfully saved! :) Go and log in!" << endl;
} else {
cout << "Sorry! We already have an account for this username. Try again!" << endl;
sing_in();
}
} else {
ofstream us("usernames.txt");
us << username[number_of_user] << endl;
us.close();
ofstream ps("passwords.txt");
ps << password[number_of_user] << endl;
ps.close();
Sleep(2000);
cout << "Your data has been succesfully saved! :) Go and log in!" << endl;
}
}
void sing_in() {
number();
cout << "Enter your new user name:";
cin >> username[number_of_user];
cout << "Enter your new password:";
cin >> password[number_of_user];
write_data(username[number_of_user],password[number_of_user]);
number_to_increase();
}
int main() {
string answer;
Start:
cout << "Do you want to log in or to sing in?n";
cin >> answer;
if(answer == "sing_in") {
sing_in();
} else if(answer == "log_in") {
log_in();
} else{
cout << "Bad answer! Try again!" << endl;
goto Start;
}
}
Aucun commentaire:
Enregistrer un commentaire