Question

Write a C++ program to allow an user to enter username and password, and then compare...

Write a C++ program to allow an user to enter username and password, and then compare the entered values with the values which stored in a file called login.txt. Display an message "Login successful" if the values are matched, otherwise display "Login fail!. This program allow the user to have three times of tries, if exceeded the number of times, display a message "Number of times is exceeded, please contact administrator


(Assume, the stored username is "INTI-IU and password is "1234567)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;

// Function to read user id and password from file
// Returns user id and password by reference
void readFile(string &userId, string &password)
{
// ifstream class object declared to write data from file
ifstream fileRead;

// Opens both the file for reading
fileRead.open("loginData.txt");

// Checks if the file unable to open for reading display's error message and stop
if(!fileRead)
{
cout<<"\n ERROR: Unable to open the file for reading.";
exit(0);
}// End of if condition

// Reads user id and password
fileRead>>userId>>password;
// Close the file
fileRead.close();
}// End of function

// Function to accept user id and password from user
// Returns user id and password by reference
void acceptIdPassword(string &id, string &pass)
{
fflush(stdin);

// Accepts user id and password
cout<<"\n Enter user id: ";
cin>>id;
cout<<"\n Enter password: ";
cin>>pass;
}// End of function

// Function to check user id and password and returns success status
int login(string userId, string password, string id, string pass)
{
// Checks if system userId and user entered id is equal
if(userId.compare(id) == 0)
{
// Checks if system password and user entered password is equals
if(password.compare(pass) == 0)
// Returns 1 for login successful
return 1;
// Otherwise returns 3 for invalid password
else
return 3;
}// End of if condition

// Otherwise returns 2 for invalid user id
else
return 2;
}// End of function

// main function definition
int main()
{
// Counter for 3 attempts
int counter = 0;
// To store system and user entered user id and password
string userId, password, id, pass;

// Calls the function to read user id and password from file
readFile(userId, password);

// Loops 3 attempts
do
{
// Calls the function to accept user id and password from user
acceptIdPassword(id, pass);

// Calls the function to check login status
// and stores return login status
int status = login(userId, password, id, pass);
// Increase the number of attempts counter by one
counter++;

// Checks if status is 1 then login successful come out of the loop
if(status == 1)
{
cout<<"\n Login successful.";
break;
}// End of if condition

// Otherwise checks if status is 2 then display invalid user id
// continue the loop
else if(status == 2)
cout<<"\n Login fail! \t Invalid user id.";

// Otherwise checks if status is 3 then display invalid password
// continue the loop
else if(status == 3)
cout<<"\n Login fail! \t Invalid user password.";

  // Checks if counter value is more than 3 then display error message
if(counter == 3)
cout<<"\n Number of times is exceeded, please contact administrator";
}while(counter < 3); // End of do - while loop


}// End of main function

Sample Output 1:

Enter user id: pyari

Enter password: 1234567

Login fail! Invalid user id.
Enter user id: INTI-IU

Enter password: 123

Login fail! Invalid user password.
Enter user id: INTI-IU

Enter password: 1234567

Login successful.

Sample Output 2:

Enter user id: Pyari

Enter password: 1234567

Login fail! Invalid user id.
Enter user id: INTI-IU

Enter password: 12345

Login fail! Invalid user password.
Enter user id: INTI-IU

Enter password: 123456789

Login fail! Invalid user password.
Number of times is exceeded, please contact administrator

Add a comment
Know the answer?
Add Answer to:
Write a C++ program to allow an user to enter username and password, and then compare...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Create a program to verify a user name and password given by a user 2....

    1. Create a program to verify a user name and password given by a user 2. Create a text file named "correctData.txt" (You will NOT upload this file during submission). Place a username on line 1 in the txt file and a password on line 2 in the file. This will be used for testing. 3. Create the following functions and call them starting from main: • void login () - This function is responsible for displaying the prompts asking...

  • Write a code using python that simulates a Registration of a Username and Password with the...

    Write a code using python that simulates a Registration of a Username and Password with the following conditions the user can set its username display "Input username" input Username display "Input Password" input Password The password must be at least 5 characters and no longer than 10 characters if the password doesn't follow this instructions Display "Can't accept this password" once the password its stored Verify that the program works by Testing it input username Input Password The user has...

  • in python 5.23 Login Create a program that prompts the user for a username and password....

    in python 5.23 Login Create a program that prompts the user for a username and password. The correct username should be python and the correct password should be csci 135. See below for example output. (Different messages depending on if user name or password are correct.) Only allow the user to make 3 attempts. Welcome to my secret program! Please enter your username: Trish Please enter your password: Duce Invalid username and password Please enter your username: Trish Please enter...

  • Write a C++ program which accepts username and password from user and then print on the...

    Write a C++ program which accepts username and password from user and then print on the screen whether the user is authenticated or not. Also, upon entering the wrong credentials (username and password) three times, the program must exit. The program must print the appropriate messages if the user enters wrong username or password.

  • I am creating a program that will allow users to sign in with a username and...

    I am creating a program that will allow users to sign in with a username and password. Their information is saved in a text file. The information in the text file is saved as such: Username Password I have created a method that will take the text file and convert into an array list. Once the username and password is found, it will be removed from the arraylist and will give the user an opportunity to sign in with a...

  • Can anyone help me with rewriting my pseudocode? Below is the pseudocode, and after that is...

    Can anyone help me with rewriting my pseudocode? Below is the pseudocode, and after that is the completed program. Pseudocode: DISPLAY Login information PROMPT for username/password            output "Enter Username"            input userName          output "Enter password: "         input password //If successful, display information pertaining to the specific user, as well as prompt for logout    IF User type 'quit'      Exit Program    VALIDATE User Credentials   IF NOT Validated      Check number of Invalid Attempts         IF user attempts equal three THEN            DISPLAY error message...

  • Can someone fix the program below so it does what the picture says it won't work...

    Can someone fix the program below so it does what the picture says it won't work for me. import java.util.Scanner; public class Userpass { static String arr[]; static int i = 0; public static void main(String[] args) { String username, password; int tries = 0, result; do { System.out.print("Enter the username: "); username = readUserInput(); System.out.print("Enter the password: "); password = readUserInput(); result = verifyCredentials(username, password); tries++; if (result == -1) System.out.println("The username is incorrect!\n"); else if (result == -2)...

  • Write a program that lets the user enter 10 values into an array. The program should...

    Write a program that lets the user enter 10 values into an array. The program should then display the largest and the smallest values stored in the array. The program should display the following message to the user at the beginning "This program will ask you to enter ten values. Then it will determine the largest and smallest of the values you entered.Please enter 10 integers separated by spaces: " And then after user entered the numbers, it should display...

  • 2. Write a C++ program that asks the user to enter a integer between 1 and...

    2. Write a C++ program that asks the user to enter a integer between 1 and 10. Continue to prompt the user while the value entered does not fall within the range. When the user is successful display a congratulatory message. Save file as OneToTen.

  • Allow a user to enter any number of double values up to 15. The user should...

    Allow a user to enter any number of double values up to 15. The user should enter 99999 to quit entering numbers. Display an error message if the user quits without entering any numbers; otherwise, display each entered value and its distance from the average. DistanceFromAverage.java

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT