Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

c++ - Why is my program not processing all the functions?

Good day everyone! First off all I want to let you all know that I am a beginner at C++, so my code will have a lot of errors. I was trying to make a program to refresh my concepts of C++. The problem I am facing is the program is asking me the email ID, but as soon as I input it, the program ends. Please let me know everything I am doing wrong and how to correct it. Thank you so much!

I decided to create a simple login program with the following algorithm:

  1. It asks the user for their email ID.
  2. Checks if the email is registered (in a text file)
  3. If the email is registered, the user is prompted for the password.
  4. If the password is correct, a success message is printed; if not, the user s given 2 more attempts.
  5. If the email is not registered, the program prompts the user to enter a new password and tells them the password strength. An ideal password should have an uppercase letter, a lowercase letter and a digit, with the password length more than 6 characters.

data.h:

#include <iostream>
#include <string>

using namespace std;

#ifndef DATA_H
#define DATA_H


struct newAccount{


    string email, password; //declaring email and password of the user
};

    string readEmail(string email); //reads in the email id provided
    void checkEmail(); //checks if the entered email address exists in the system
    int addEmail(); //checks if the entered email address exists in the system

    void checkPassword(); //checks if the password matches an already registered email id
    void makeNewPassword(string& password); //this function helps the user create a secure password


#endif

data.cpp:

#include <iostream>
#include <string>
#include <fstream>
#include "data.h"

using namespace std;

newAccount tempAccount;

string readEmail(string email) //reads in the email id provided
{
    cout << "Enter an email address: ";
    getline(cin, tempAccount.email);
    email = tempAccount.email;
    return tempAccount.email;
}

void checkEmail()
{
    ifstream file("database.txt");
    string str;
    while (getline(file, str))
    {
        if (str == tempAccount.email)
        {
            cout << "This email is already registered. Please enter your password: ";
            getline(cin, tempAccount.password);
            checkPassword();
        }

        else
        {
            cout << "This email is not registered. Please create a new password: ";
            makeNewPassword(tempAccount.password);
        }
    }
}

int addEmail() //checks if the entered email address exists in the system
{
    ofstream myFile("database.txt");
    if (myFile.is_open())
    {
        myFile << tempAccount.email << endl;
        myFile.close();
    }
    else
        cout << "Unable to open file";
    return 0;
}

void checkPassword() //checks if the password matches an already registered email id
{
    ifstream file("database.txt");
    string str;
    while (getline(file, str))
    {
        if (checkEmail)
        {
            if (str == tempAccount.password)
            {
                cout << "Login successful! ";
                getline(cin, tempAccount.password);
            }

            else
                for (int i = 4; i > 1; i--)
                {
                    cout << "Incorrect password! You have " << i - 1 << " tries remaining.
";
                    if (str == tempAccount.password)
                        break;
                }
        }
    }
}

void makeNewPassword(string &password) //this function helps the user create a secure password
{
    int n = password.length();
    bool hasLower = false, hasUpper = false, hasDigit = false;

    for (int i = 0; i < n; i++)
    {
        if (islower(password[i]))
            hasLower = true;
        if (isupper(password[i]))
            hasUpper = true;
        if (isdigit(password[i]))
            hasDigit = true;
    }

    // Displaying the strength of password

    cout << "Strength of password you have entered is ";

    if (hasUpper && hasDigit && hasLower && (n >= 6)) // considering a strong must be of length 6 or more
        cout << "strong" << endl;
    else if ((hasLower || hasUpper) && hasDigit && (n >= 6))
        //when at least a lower case or uppercase is used along with digit
        cout << "moderate" << endl;
    else
        cout << "weak" << endl;
}

main.cpp

#include <iostream>
#include <fstream>
#include "data.h"

using namespace std;

int main(){
    string e, p;
    readEmail(e);
    checkEmail();
return 0;
}

I have created this program with the knowledge of a couple of basic C++ courses I took a few semesters ago, and using online tutorials. This is not a homework or an assignment of any kind.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your readEmail() function, the string email is a local variable. You passed the variable to the function by value, not by reference. Also, if you pass it by reference, then there's no need to return anything (the function should be void).

void readEmail(string& email) //reads in the email id provided
{
    cout << "Enter an email address: ";
    cin >> email;
}

int main() {
    string e, p;
    readEmail(e);
    checkEmail();
    return 0;
}

But if you want to return the value, than there's no need for parameter, but you need to give that return value to your variable.

string readEmail() //reads in the email id provided
{
    cout << "Enter an email address: ";
    cin >> email;
    return email;
}
int main() {
    string e = readEmail();
    checkEmail();
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...