1

I have created a Login system and when this system matches the entered inputs(current) with previously entered Id and password (using .txt file), they both differ by an extra blank space character which is at the end (actually, Idk which character is this but, When I printed it on screen I got a blank), Help me to get rid of this unwanted character so that I can perform a If-Else comparison for user entry!

Here is my Code and Snippet of Output:

/* LOGIN MANAGEMENT SYSTEM */

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

int main()
{
/** CREATED ACCOUNT VIA SOME AUTHENTIC VERIFICATION **/
/* PASSWORD GENERATED */

std::string user_name, pwd;

std::cout << "ENTER YOUR USER_NAME: " << std::endl;
std::cin >> user_name;

std::cout << "ENTER YOUR PASSWORD: " << std::endl;
std::cin >> pwd;

std::ofstream set_user_name;
set_user_name.open("id.txt");
set_user_name << user_name;
set_user_name.close();

std::cin.ignore();

std::ofstream set_pwd;
set_pwd.open("pwd.txt");
set_pwd << pwd;
set_pwd.close();

std::cout << "********CONGO!, YOUR ACCOUNT IS CREATED********" << std::endl;

/*LOGIN*/
std::string user_username, user_pwd;

std::cout << "ENTER YOUR USER_NAME: " << std::endl;
std::cin >> user_username;

std::cout << "ENTER YOUR PASSWORD: " << std::endl;
std::cin >> user_pwd;

std::string verify_id, verify_pwd;

std::ifstream fin;
fin.open("id.txt");

char ch;
while (!fin.eof())
{
    ch = fin.get();
    verify_id += ch;
}

// verify_id.push_back('\0');

std::ifstream fin1;
fin1.open("pwd.txt");
while (!fin1.eof())
{
    ch = fin1.get();
    verify_pwd += ch;
}

verify_id += 's';
std::cout << "verify_id: " << verify_id; /* I have attached a snippet for this output
                                            which is holding a blank space at the end */

// std::cout << verify_id[3];

if (verify_id == user_username && verify_pwd == user_pwd)
{
    std::cout << "LOGIN SUCCESFUL" << std::endl;
}

if (verify_pwd == user_pwd)
{
    std::cout << "Errr! USERNAME OR PASSWORD IS INCORRECT" << std::endl;
}

return (0);
}

Output Snippet

TheDev05
  • 198
  • 10
  • 5
    https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong Admittedly C, but the concept is the same. – Yunnosch Jun 24 '21 at 20:22
  • @Yunnosch right. `fin.eof()` doesn't tell you when you're *at* the end of the file, it tells you when you tried to read *past* the end of the file. – Mark Ransom Jun 24 '21 at 20:28

1 Answers1

3

istream::get() returns the EOF character (end of file) at the end of the file.

Change your logic so that you do not append this character to the string you read.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171