首页 > 解决方案 > 修改创建帐户程序

问题描述

我是一名正在学习 C++ 的学生,我需要一些帮助。我目前正在研究 Murach's C++ Programming 一书中的教科书练习。我正在做练习 7-2(第 7 章的第二个练习)。该程序的说明在这里:练习 7-2 说明我已经设法理解了其中的大部分内容,但我目前停留在第 9 步。我知道如何调用函数,但是当我运行程序时,它只允许我输入我的全名。完成此操作后,程序结束,我不输入密码或电子邮件。是的,我已经根据需要为我的变量添加了一个返回值。我怎样才能让程序让我按预期输入我的全名、密码和电子邮件?我尝试过的任何方法似乎都不起作用。我尝试返回值 0,我尝试创建一个局部变量,然后将返回值添加到所述变量,但这些都不起作用。请帮助我理解我应该做什么,因为我还是 C++ 的新手,还有很多东西要学。顺便说一句,我使用 Microsoft Visual Studios 作为我的 IDE。

这是迄今为止我的 main.cpp 文件的代码:

#include <iostream>
#include <string>
#include "validation.h"

using namespace std;

int main()
{
cout << "Create Account\n\n";

// get full name and parse first name
string full_name;
string first_name;
bool valid_name = false;
while (!valid_name) {
    cout << "Enter full name: ";
    getline(cin, full_name);

    // strip whitespace from front
    int i = full_name.find_first_not_of(" \n\t");
    if (i > -1) {
        full_name = full_name.substr(i);
    }

    // get first name
    int space_index = full_name.find(' ');
    if (space_index == -1) {
        cout << "You must enter your full name. Please try again.\n";
    }
    else {
        first_name = full_name.substr(0, space_index);
        valid_name = true;
    }
}
cout << endl;

bool validation::is_valid_password(string password);

bool validation::is_valid_email(string email);

// make sure first name uses initial cap
char letter = first_name[0];
first_name[0] = toupper(letter);
for (int i = 1; i < first_name.length(); ++i) {
    letter = first_name[i];
    first_name[i] = tolower(letter);
}

// display welcome message
cout << "Hi " << first_name << ",\n"
    << "Thanks for creating an account!\n\n";
}

到目前为止,我使用 validation.cpp 实现文件的代码:

#include "validation.h"
#include <string>
#include <iostream>
using namespace std;
using namespace validation;

bool validation::is_valid_password(string password) {
bool valid_password = false;
while (!valid_password) {

    valid_password = true;

    cout << "Enter password: ";
    getline(cin, password);

    if (password.length() < 8) {
        cout << "Password must be at least 8 characters.\n";
        valid_password = false;
    }

    int index = password.find_first_of("0123456789");
    if (index == -1) {
        cout << "Password must include a number.\n";
        valid_password = false;
    }

    bool special_character = false;
    for (char c : password) {
        if (ispunct(c)) {
            special_character = true;
            break;
        }
    }
    if (!special_character) {
        cout << "Password must include a special character.\n";
        valid_password = false;
    }

    if (!valid_password) {
        cout << "Please try again.\n";
    }
    else {
        password = password.substr(0, index);
        valid_password = true;
    }
}
cout << endl;
return false;
}

bool validation::is_valid_email(string email) {
bool valid_email = false;
while (!valid_email) {

    valid_email = true;

    cout << "Enter email: ";
    getline(cin, email);

    int at_index = email.find('@');
    if (at_index == -1) {
        cout << "The email must include an at character (@).\n";
        valid_email = false;
    }

    int dot_index = email.rfind('.');
    if (dot_index == -1) {
        cout << "The email must include a dot operator (.).\n";
        valid_email = false;
    }

    bool valid_chars = true;
    for (char c : email) {
        if (c != '@' && c != '.' && c != '_' && c != '-') {
            if (!isalnum(c)) {
                valid_chars = false;
                break;
            }
        }
    }

    if (at_index == 0) {
        cout << "The local part of the email must include at least one character.\n";
        valid_email = false;
    }

    if (dot_index - at_index == 1) {
        cout << "The server name of the email must include at least one character.\n";
        valid_email = false;
    }

    if (email.length() - dot_index - 1 != 3 && email.length() - dot_index - 1 != 2) {
        cout << "The domain name of the email must have two or three characters.\n";
        valid_email = false;
    }
    if (!valid_email) {
        cout << "Please try again.\n";
    }
    else {
        email = email.substr(0, at_index);
        email = email.substr(0, dot_index);
        valid_email = true;
    }

}
cout << endl;
return false;
}

还有我用于验证头文件的代码:

#ifndef T_FRYE_VALIDATION_H
#define T_FRYE_VALIDATION_H
#endif // !T_FRYE_VALIDATION_H
#include <string>
using namespace std;

namespace validation {
  bool is_valid_password(string password);
  bool is_valid_email(string email);
}

我知道要阅读的内容很多,对此我感到非常抱歉,但我不知道下一步该做什么。如果有人可以帮助我,我将不胜感激。

标签: c++visual-c++

解决方案


你认为这两行在main()函数内部做了什么?

int main()
{
// ...
bool validation::is_valid_password(string password);
bool validation::is_valid_email(string email);
// ...
}

那不是函数调用,那只是函数的声明。你应该有类似的东西:

int main()
{
    // ...
    std::string password;
    while (!validation::is_valid_password(password));
    std::string email;
    while (!validation::is_valid_email(email));
    // ...
}

无论如何,您的代码中还有许多其他问题......例如,如果您正在读取函数内部的内容is_valid_xxx(这本身就是一个糟糕的设计,因为is_-type 函数除了检查值之外不应该做任何事情),您应该通过通过引用的字符串:

bool validation::is_valid_password(string &password);

即使您不打算获取新值,在这种类型的函数中按值传递也是一个坏主意。通过引用 const 传递值:

bool validation::is_valid_password(const string &password);

推荐阅读