0

I m trying to make a password protected program.My password must be read from a file and compare with the password written when you run the program.The password written from keyboard must be encrypted with ASTERIX. This is what I've done by now:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;
void main()
{
    char pass[20], mypass[20], ch;
    cout << "Enter the password: " << endl;
    int i=0;
    do
    {
        ch=cin.get();
        pass[i]=ch;
        if (ch!=27 && ch!=13 && ch!=9)
            putchar('*');
        else
            break;
        i++;
    } while (i<19);
    pass[i]='\0';
    ifstream myfile("password.txt");
    if (myfile.is_open())
    {
        while (!myfile.eof())
        {

            if (strcmp(pass, mypass)!=0)
            {
                cout << "Incorrect password." << endl;
            }

            myfile.close();
        }

   }
}
SyntaxVoid
  • 2,501
  • 2
  • 15
  • 23
  • 1
    what prevents me from opening password.txt to see what is written there? what level of security do you really need? – 463035818_is_not_an_ai Apr 15 '19 at 18:13
  • please be more precise on what exactly you need help with. Your code is actually not reading from the file. Do you want to know how to read from a file? – 463035818_is_not_an_ai Apr 15 '19 at 18:14
  • is it a complete program? there is no assignment statment for `mypass` variable. what is that you are comparing `pass` with? – AviatorX Apr 15 '19 at 18:18
  • 3
    Unrelated: `while (!myfile.eof())` is a bug. Details and solutions are covered at [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Apr 15 '19 at 18:23
  • Is just a homework, i don't need security for password.txt and this is all the program. –  Apr 15 '19 at 18:51
  • I know how to read by a file but I need the code to stock the word from password.txt and compare with the password written from keyboard by user –  Apr 15 '19 at 18:53
  • 1
    I assume you can't make things easy on yourself and use `std::string`? – user4581301 Apr 15 '19 at 18:54
  • No, I must use char type –  Apr 15 '19 at 18:56

1 Answers1

0

I would suggest making a std::string to store each char in one variable. It might go something like this:

#include <string>
void main(){
     std::string storedPassword, userEntered;
     char currentCharacter;
     ifstream file("password.txt");
     while(currentCharacter << file){
          storedPassword.append(currentCharacter);
     }
     std::cout << "Enter the password: ";
     std::cin >> userEntered;
     if(userEntered == storedPassword){
          std::cout << "\nThat is the correct password!" << std::endl;
     } else {
          std::cout << "That is incorrect." << std::endl;
     }
}

If the while loop doesn't work, I'd suggest writing an extra unique character at the end of the password in the file (something like $) and doing something like: while(currentCharacter != '$') and then doing currentCharacter = file.get() at the beginning of the while loop as well as in a do statement beforehand rather than what I had put:

do{
currentCharacter = file.get();
}
while(currentCharacter != '$'){
//do everything above and put: currentCharacter = file.get(); at the end of the loop
Cyerunix
  • 71
  • 11