2014-10-21 3 views
-4
#include <iostream> 
#include <string> 
#include <fstream> 
#include <cmath> 
#include <iomanip> 
#include <cctype> 

using namespace std; 

int main() 
{ 
    fstream fin; 
    string password; 
    cout << "Please enter your password!" << endl; 
    cin >> password; 
    fin.open("Text.txt"); 

    int nlen = password.length(); 

    if (nlen <= 7) 
     return false; 
    if (nlen >= 8) 
     return true; 
    bool hasUpp = false; 
    bool hasLow = false; 
    bool hasDig = false; 
    bool hasSym = false; 

    for (int i = 0; i < nlen; i++) 
    { 
     if (isupper(password[i])) 
      hasUpp = true; 
     if (islower(password[i])) 
      hasLow = true; 
     if (isdigit(password[i])) 
      hasDig = true; 
    } 
    if (hasLow && hasUpp && hasDig && hasSym) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
    if (hasLow && hasUpp && hasDig && hasSym) 
    { 
     cout << "Your password is strong! " << endl; 
    } 
    else 
    { 
     cout << "Your password is too weak! " << endl; 
    } 
    cin.get(); 
    cin.get(); 
    return 0; 
} 

Эта программа должна принимать входные данные от пользователя и решать, является ли это несколько надежным паролем. Я понимаю, что это еще не закончено. Проблема, с которой я сталкиваюсь, заключается в том, чтобы программа прочитала мой входной файл и выяснила, вводится ли во входном файле какие-либо слова во входном файле, а затем скажет им, что их пароль плох.Программа для пароля

+1

Вы открываете файл, но не читаете его. Это может быть место для начала? – crashmstr

+0

Сразу после прочтения пароля (и открытия неиспользуемого файла) вы возвращаете 'false' или' true' (в зависимости от длины пароля), заканчивая программу, прежде чем что-либо еще может произойти. Кроме того, 'main()' должно возвращать целое число - обычно '0' для успеха, но ненулевое - если оно возвращается *. –

ответ

0

Я внесли некоторые изменения в вашу программу, чтобы она работала хотя бы с пользовательскими входными данными. Вы упрямо задавались вопросом, почему вы не получаете какой-либо выход из своей программы? Есть два если-положения в вашей программе, которая будет вызывать функцию основной() для возврата (= ваше приложение завершается):

if (nlen <= 7) 
    return false; 
if (nlen >= 8) 
    return true; 

Одно из заявлений возвратных называется, так как один из если-статей всегда верно и поэтому ваша программа выйдет из него.

Вот измененный код для обработки одного пароля, введенного пользователем:

#include <iostream> 
#include <string> 
#include <fstream> 
#include <cmath> 
#include <iomanip> 
#include <cctype> 

using namespace std; 

int main() 
{ 
    string password; 
    cout << "Please enter your password!" << endl; 
    cin >> password; 

    int nlen = password.length(); 

    bool hasUpp = false; 
    bool hasLow = false; 
    bool hasDig = false; 
    bool hasSym = false; 
    bool isLong = false; 

    if (nlen >= 8) 
     isLong = false; 

    for (int i = 0; i < nlen; i++) 
    { 
     if (isupper(password[i])) 
      hasUpp = true; 
     if (islower(password[i])) 
      hasLow = true; 
     if (isdigit(password[i])) 
      hasDig = true; 
    } 

    if (hasLow && hasUpp && hasDig && hasSym && isLong) 
    { 
     cout << "Your password is strong! " << endl; 
    } 
    else 
    { 
     cout << "Your password is too weak! " << endl; 
    } 

    cin.get(); 
    cin.get(); 
    return 0; 
} 

Чтобы продлить это прочитать несколько паролей из файла, вы должны прочитать файл построчно (as explained here) и обрабатывать каждую строку.

0
**YOU CAN DO LIKE THIS** 

#include <iostream> 
#include <conio.h> 
#include <windows.h> 

using namespace std; 

void gotox(int x) 
{ 
COORD xy = {0, 0}; 
xy.X = x; 
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xy); 
} 

void getx(int &x) { 
CONSOLE_SCREEN_BUFFER_INFO csbi; 
if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) { 
x = csbi.dwCursorPosition.X; 
} 
} 

int main() 
{ 
SetConsoleTitle("file password protector"); 
char pas; 
string password = ""; 
int x,t = 0; 
cout<<"enter password : "; 
do 
{ 
pas = _getch(); 
switch(int(pas)) 
{ 
case 8: getx(x); 
     if(x>17) 
     { 
     --x; 
     gotox(x); 
     cout<<" "; 
     if(password.length()>0){password.erase(password.length()-1,1);} 
     --t; 
     gotox(x); 
     } 
     break; 
case 27: return 0; 
case 13: if(t>8) 
     { 
      pass = 27 
     } 
     break; 
default :if(t < 30) 
     { 
      if(int(pas)>0) 
       { 
        password.push_back(pas);cout<<"*";++t; 
       } 
       else 
       { 
         pas = _getch(); 
       } 
     }}}while(pas != 13); 

bool hasUpp = false; 
bool hasLow = false; 
bool hasDig = false; 
bool hasSym = false; 

for (int i = 0; i < t; i++) 
{ 
if (isupper(password[i])) 
    hasUpp = true; 
if (islower(password[i])) 
    hasLow = true; 
if (isdigit(password[i])) 
    hasDig = true; 
} 

if (hasLow && hasUpp && hasDig && hasSym) 
{ 
cout << "Your password is strong! " << endl; 
} 
else 
{ 
cout << "Your password is too weak! " << endl; 
} 

_getch(); 
return 0; 
} 
+0

Пожалуйста, не оставляйте ответы, содержащие только код. Объясните свое решение и как вы исправили проблему. –

Смежные вопросы