В каждой строке входного текстового файла располагается логин и пароль пользователя (через пробел); количество пользователей не менее
10. На вход подаются две строки - логин и пароль. Проверить, совпадают ли они с данными из файла.
C/C++
Помогите, на с++
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
struct Authentication {
string login;
string password;
private:
friend istream& operator>>(istream& inp, Authentication& a) {
return inp >> a.login >> a.password;
}
friend bool operator==(const Authentication& a, const Authentication& b) {
return a.login == b.login && a.password == b.password;
}
};
int main() {
auto exist = false;
while (true) {
cout << "Path: ";
string path;
getline(cin, path);
ifstream inp(path);
if (inp.is_open()) {
int attempt = 3;
Authentication user;
Authentication next;
do {
cout << "There are " << attempt << " attempts left!\n";
cout << "Input login and password: ";
cin >> user;
while (inp >> next) {
if (next == user) {
exist = true;
break;
}
}
puts(exist ? "User exists!" : "User not found!");
if (exist) break;
} while (--attempt);
inp.close();
break;
} else {
cout << "Path " << path << " not found!\n";
continue;
}
}
puts(exist ? "Welcome!" : "Access closed!");
system("pause > nul");
}
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
struct Authentication {
string login;
string password;
private:
friend istream& operator>>(istream& inp, Authentication& a) {
return inp >> a.login >> a.password;
}
friend bool operator==(const Authentication& a, const Authentication& b) {
return a.login == b.login && a.password == b.password;
}
};
int main() {
auto exist = false;
while (true) {
cout << "Path: ";
string path;
getline(cin, path);
ifstream inp(path);
if (inp.is_open()) {
int attempt = 3;
Authentication user;
Authentication next;
do {
cout << "There are " << attempt << " attempts left!\n";
cout << "Input login and password: ";
cin >> user;
while (inp >> next) {
if (next == user) {
exist = true;
break;
}
}
puts(exist ? "User exists!" : "User not found!");
if (exist) break;
} while (--attempt);
inp.close();
break;
} else {
cout << "Path " << path << " not found!\n";
continue;
}
}
puts(exist ? "Welcome!" : "Access closed!");
system("pause > nul");
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct para
{
string login;
string password;
};
int main()
{
const char file_name [] = "data.txt"; // текстовый файл, где хранятся логины и пароли
const int max_size = 100;
int i = 0;
para p[max_size];
ifstream base;
base.open(file_name);
if(base.is_open())
{
while(!base.eof() && i < max_size)
{
base >> p[i].login;
base >> p[i].password;
i++;
}
base.close();
}
string login,password;
char q;
do
{
cout << "Введите логин: ";
cin >> login;
cout << "Введите пароль: ";
cin >> password;
for(int x = 0; x < i; x++)
{
if(login == p[x].login)
{
if(password == p[x].password)
{
cout << "Введены правильные логин и пароль!" << endl;
return 0;
}
}
}
cout << "Введены неправильно логин или пароль." << endl;
cout << "\tЧтобы повторить попытку, введите \'y\': ";
cin >> q;
} while(q == 'y' || q == 'Y');
return 0;
}
#include <fstream>
#include <string>
using namespace std;
struct para
{
string login;
string password;
};
int main()
{
const char file_name [] = "data.txt"; // текстовый файл, где хранятся логины и пароли
const int max_size = 100;
int i = 0;
para p[max_size];
ifstream base;
base.open(file_name);
if(base.is_open())
{
while(!base.eof() && i < max_size)
{
base >> p[i].login;
base >> p[i].password;
i++;
}
base.close();
}
string login,password;
char q;
do
{
cout << "Введите логин: ";
cin >> login;
cout << "Введите пароль: ";
cin >> password;
for(int x = 0; x < i; x++)
{
if(login == p[x].login)
{
if(password == p[x].password)
{
cout << "Введены правильные логин и пароль!" << endl;
return 0;
}
}
}
cout << "Введены неправильно логин или пароль." << endl;
cout << "\tЧтобы повторить попытку, введите \'y\': ";
cin >> q;
} while(q == 'y' || q == 'Y');
return 0;
}
Похожие вопросы
- Помогите с кодом C++
- Помогите понять формулу из книги Д. Кнута
- Помогите решить лабораторную c++
- Помогите с кодом с++
- Помогите новичку с программированием и матрицами C++
- Помогите с заданием по программированию язык C(не С++)
- Помогите дописать код с массивом
- Помогите дописать код с массивом?
- Помоги пожалуйста с программой в С++
- Помогите написать программу на С