修改创建帐户程序

Modifying the Create Account Program

本文关键字:程序 创建 修改      更新时间:2023-10-16

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

以下是到目前为止我的main.cpp文件的代码:

#include <iostream>
#include <string>
#include "validation.h"
using namespace std;
int main()
{
cout << "Create Accountnn";
// 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(" nt");
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!nn";
}

到目前为止,我使用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);
}

我知道这是一本很难阅读的书,对此我真的很抱歉,但我不知道下一步该怎么办。如果有人能帮我,我真的很感激。

对验证函数的调用格式不正确。您试图调用代码

validation::isvalid_password();来自main,但您提供的是:

bool validation::is_valid_password(string password);

bool validation::is_valid_email(string email);

它们是声明。你需要实际调用你想要的代码。

string password;
string email;
validation::isvalid_password(password);
validation::is_valid_email(email);

您认为这两行在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_类型的函数除了检查值之外什么都不应该做(,那么您应该通过引用传递字符串:

bool validation::is_valid_password(string &password);

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

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