-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;
}

This program is supposed to take input data from a user and decide whether or not it is a somewhat strong password. I realize this is not near finished yet. The problem I am having is making the program read my input file and figure out whether or not any of the words in the input file are being entered as passwords, which would then tell them their password is bad.

  • 1
    You open a file, but never read from it. That might be a place to start? – crashmstr Oct 21 '14 at 17:56
  • Immediately after reading the password (and opening the never-used file), you return `false` or `true` (depending on the length of the password), ending the program before anything else can happen. Also, `main()` should return an integer - typically `0` for success, non-zero otherwise - when it *does* return. – Paul Roub Oct 21 '14 at 18:32

2 Answers2

0

I've made some modifications to your program to make it work at least with user input data. You propably wondered why you don't get any output from your program? There are two if-clauses within your program which will cause the main() function to return (= your application terminates):

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

One of the return statements is called since one of the if-clauses is always true and therefore your program will exit there.

Here is the modified code to process a single password entered by the user:

#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;
}

To extend this to read several passwords from a file, you have to read the file line by line (as explained here) and process each line.

Community
  • 1
  • 1
Lukas Thomsen
  • 3,089
  • 2
  • 17
  • 23
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;
}
Sushant
  • 124
  • 1
  • 5