TheDev05
TheDev05

Reputation: 196

Why the text which is extracted from a .txt file holds a blank space character at the end and How to remove that?

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

Upvotes: 1

Views: 255

Answers (1)

Pete Kirkham
Pete Kirkham

Reputation: 49311

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.

Upvotes: 3

Related Questions