1

I'm writing a program that needs to read input both from a file and from user input. For example, I will need the input from a user for example a password, and then check if the password is correct by comparing it with a password from a file. After that, I want to ask for more user input again and so on. I wonder if there is any way to do this? Thanks in advance.

Edit: Thanks for the comments to let me know that I should put in some code. This is my first time here so I'm sorry for the inconvenience.

#include <iostream>
#include <cstdio>

using namespace std;

string pass;
string userpass;

void getPassword()
{
    ios_base::sync_with_stdio(0);
    freopen("PASS.TXT", "r", stdin);
    cin >> pass;
}

int main()
{
    cout << "Please enter your password: ";
    cin >> userpass;
    getPassword();
    if (userpass == pass)
    {
        char yes;
        cout << "Correct password!\nType \"Y\" to continue... ";
        /*
        cin >> yes;
        if (yes == 'Y')
        {
            cout << "Do something here";
        }
        */
        // Now I need to get user to type "Y"
        // but if I use cin then it will continue
        // to get input from the file
    }
    else
    {
        cout <<"\nWrong password...";
    }
    return 0;
}

  • 1
    Notice how all the operations you specify are *sequential*, which means you can do one operation after another in your code as well. As in: Ask user for input; Then check file; Then ask user for more input; ... – Some programmer dude Aug 30 '21 at 16:06
  • Hopefully you already know about `std::cin`, but you may be interested in the [basic_ifstream](https://en.cppreference.com/w/cpp/io/basic_ifstream) for reading from files. Very similar interface but it reads from a file instead of standard input. – Nathan Pierson Aug 30 '21 at 16:10
  • 2
    ***I wonder if there is any way to do this?*** `c++` would be a very useless language if it would not be possible. – drescherjm Aug 30 '21 at 16:16
  • 1
    Do you know how to write code to read a file? Do you know how to write code to receive input from the user? which part of this problem is difficult? – Beta Aug 30 '21 at 16:20
  • I recommend you split up many of these operations into their own function and test and solve them separately. – drescherjm Aug 30 '21 at 16:21
  • Thanks for the comments! I do know how to use std::cin, and I know how to read input from a file using freopen(), but I can't use both of these one after the other. (I think the problem is that I don't know how to close the file after open it) – Tô Huỳnh Phúc Aug 30 '21 at 16:32
  • ***I don't know how to close the file*** You usually don't have to do that. However the member function to close a file is called close(). [https://en.cppreference.com/w/cpp/io/basic_ifstream/close](https://en.cppreference.com/w/cpp/io/basic_ifstream/close) – drescherjm Aug 30 '21 at 16:37
  • [Here's a list of good C++ books](https://stackoverflow.com/a/388282/440558). Please invest in a couple of beginners books. They should have all the information you need on how work with files. – Some programmer dude Aug 30 '21 at 16:50
  • `ios_base::sync_with_stdio(0);` and `freopen("PASS.TXT", "r", stdin);` I would not use either of these. Instead see the example at the bottom of this on how to use ifstream: [https://en.cppreference.com/w/cpp/io/basic_ifstream](https://en.cppreference.com/w/cpp/io/basic_ifstream) – drescherjm Aug 30 '21 at 16:51
  • 2
    As for your current code, the main problem is `freopen("PASS.TXT", "r", stdin);`. This will permanently redirect all input from standard input to be read from the file. From that point onward you can never read input from the user again, only from the file `PASS.TXT`. – Some programmer dude Aug 30 '21 at 16:51
  • Ohh so there's no way to use this? I didn't know how to use anything else because that's what I was taught at school. I guess now I will learn to use ifstream. Thanks everyone! – Tô Huỳnh Phúc Aug 30 '21 at 16:55

2 Answers2

0

You can use std::cin [from the iostream library] for getting the user's input, and use std::ifstream [from the fstream library] to read a file. Then, you can turn the buffer's contents into a string [from the string library] using an sstream [from the sstream library] and then iterate through the file's contents and checking if they are the same as the passwords contents, excluding newlines of course, and then I close the file in the function. I optionally added std::cout to print out something. I wrapped the input in a while loop to make it repeat infinitely until the user gets the correct password, if they do i just run break to break the while loop. freopen isnt that good of an idea if you are going to capture user input.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

bool check_password(std::string password) {
   std::ifstream password_file("password.txt");
   std::ostringstream string_stream;
   string_stream << password_file.rdbuf();
   std::string file_password = string_stream.str();
   bool correct_password = true;
   for(int i = 0; i < file_password.size(); i++) {
      if(file_password[i] !=  password[i] && file_password[i] != '\n') {
         correct_password = false;
         break;
      }
   }
   password_file.close();
   return correct_password;
}

int main() {
   while(true) {
      std::cout << "Please type in your password." << std::endl;
      std::string password;
      std::cin >> password;
      std::cout << "Checking..." << std::endl;
      if(check_password(password)) {
         std::cout << "Correct password!" << std::endl;
         break;
      }
      std::cout << "Incorrect password." << std::endl;
   }
   return 0;
}
ByteBox
  • 21
  • 10
  • It kinda worked, but do I have to do the for loop to compare the strings? Is there another way around this? And I assume that if there are multiple strings in the text file then all of them will be considered as one string with "\n" between the different strings in the files? – Tô Huỳnh Phúc Aug 30 '21 at 17:20
  • There is probably a function that gets the contents of the file and ignores newlines, but i just used a for loop for convenience purposes. – ByteBox Aug 30 '21 at 18:54
0

The way to deal with files is to use cin to have an input data and std::ifstream class that is used to open files for reading and ifstream::operator>>() to read the data from the file stream.

example:

std::string input;

std::cin >> input;

std::ifstream file;

file.open("file.txt", std::ifsream::in);

std::string password;

file >> password;

if (password == input)
//TODO

else
//Incorrect password
SANDRUX
  • 31
  • 2