【问题标题】:Why is my program not processing all the functions?为什么我的程序没有处理所有功能?
【发布时间】:2020-12-31 16:06:54
【问题描述】:

大家好!首先我想让大家知道我是 C++ 的初学者,所以我的代码会有很多错误。 我试图制作一个程序来刷新我的 C++ 概念。我面临的问题是程序正在询问我的电子邮件 ID,但是一旦我输入它,程序就结束了。请让我知道我做错的一切以及如何纠正它。非常感谢!

我决定使用以下算法创建一个简单的登录程序:

  1. 它要求用户提供他们的电子邮件 ID。
  2. 检查电子邮件是否已注册(在文本文件中)
  3. 如果电子邮件已注册,则会提示用户输入密码。
  4. 如果密码正确,则打印成功消息;如果没有,用户会再尝试 2 次。
  5. 如果电子邮件未注册,程序会提示用户输入新密码并告诉他们密码强度。理想的密码应该是一个大写字母、一个小写字母和一个数字,密码长度大于6个字符。

数据.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

数据.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.\n";
                    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;
}

我使用几个学期前学习的几门基本 C++ 课程的知识以及使用在线教程创建了这个程序。这不是家庭作业或任何形式的作业。

【问题讨论】:

  • 您会很高兴听到您不需要任何人的帮助来解决这个问题,只需一个您已经拥有的工具:您的调试器!这正是调试器的用途。它一次一行地运行你的程序,并向你展示正在发生的事情。了解如何使用调试器是每个 C++ 开发人员必备的技能,没有例外。在调试器的帮助下,您应该能够快速找到此程序以及您编写的所有未来程序中的所有问题,而无需向任何人寻求帮助。您是否已经尝试过使用调试器?如果不是,为什么不呢?您的调试器向您展示了什么?
  • 听起来你想要一个代码审查。这不是在这个网站上真正做的事情。您可能想在 StackExchange 上查看 Code Review
  • string readEmail(string email)的参数没用。它没有被读取并且分配任何东西只会改变本地副本。但它甚至不需要,因为你返回了结果。可悲的是,您在main 中丢弃了此结果。
  • 可能罪魁祸首:ifstream file("database.txt");你需要检查文件是否打开成功。
  • 在你修复了在 main 中忽略/忽略 readEmail() 的返回值的错误之后,我假设你实际上想出于某种目的使用 e 的值,否则询问是没有意义的。跨度>

标签: c++ data-structures file-io nested


【解决方案1】:

在您的 readEmail() 函数中,字符串 email 是一个局部变量。您通过值而不是通过引用将变量传递给函数。 此外,如果您通过引用传递它,则无需返回任何内容(该函数应为 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;
}

但是如果要返回值,则不需要参数,但需要将返回值赋予变量。

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;
}

【讨论】:

  • 对于std::string,我会使用两个选项中的第二个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-17
  • 1970-01-01
  • 2015-12-06
相关资源
最近更新 更多