我正在通過檢查以下條件在代碼塊上撰寫代碼以檢查有效的電子郵件地址:
1.必須至少有一個大寫字母。2.應該有8個以上50個以下的字符 3.應該有@符號
我使用了 3 個 while 回圈來檢查各個條件,但在輸入電子郵件地址后,程式停止了。這是我的代碼,有人知道是什么問題嗎?
在此處輸入代碼
#include<iostream>
using namespace std;
#include<stdio.h>
#include<conio.h>
void check_mail()
{
int i = 0;
char email[25];
int measure = 0;
cout<<" \n \n \n enter an email address ::";
gets(email);
while(email[i] != '\0')//for checking uppercsae //
{
if( (int)email[i] >= 65 && (int)email[i] <= 90)
{
measure = 1;
}
if( measure != 1)
{
cout<<"\n there is no uppercase letter in the email address ";
break;
}
}
while(email[i] != '\0') //checking @ sign//
{
if((int)email[i] == 64)
{
cout<<" \n found the @ character at :: "<<i<<endl;
}
}
int counter = 0;
while(email[i] != '\0')
{
counter = counter 1 ;
}
if(counter >=8 && counter <=50)
{
cout<< "\n valid number of characters are present in the mail :: ";
}
else if(counter <8)
{
cout<<" \n number of characters are less than 8 ";
}
else if(counter >=51 )
{
cout<<"\n the elements are greater than 50 ";
}
else
{
cout<<"\n enter a valid email address::";
}
}
int main()
{
cout<<" \n \n enter a email address ";
check_mail();
return 0;
}
uj5u.com熱心網友回復:
下面的代碼是您的代碼的有效且更好的實作方式:
#include <iostream>
#include <string>
bool check_mail(const std::string email)
{
if (email.size() < 8 || email.size() > 50) return false;
int upper_letters = 0;
for (int i = 0; i < email.size(); i )
{
if (std::isupper(email[i])) upper_letters ;
if (email[i] == '@')
{
if (i < 8) return false;
else if (upper_letters == 0) return false;
return true;
}
}
return false;
}
int main()
{
std::cout << " \n \n Enter an email address ";
std::string email; std::cin >> email;
std::cout << check_mail(email) << std::endl;
return 0;
}
如果您需要知道究竟是什么導致電子郵件被拒絕,您可以執行以下操作:
#include <iostream>
#include <string>
enum email_states { correct, under_char, over_char, no_upper, no_at_the_rate };
email_states check_mail(const std::string email)
{
if (email.size() < 8) return email_states::under_char;
else if (email.size() > 5) return email_states::over_char;
int upper_letters = 0;
for (int i = 0; i < email.size(); i )
{
if (std::isupper(email[i])) upper_letters ;
if (email[i] == '@')
{
if (i < 8) return email_states::under_char;
else if (upper_letters == 0) return email_states::no_upper;
return email_states::correct;
}
}
return email_states::no_at_the_rate;
}
int main()
{
std::cout << " \n \n Enter an email address ";
std::string email; std::cin >> email;
std::cout << check_mail(email) << std::endl;
return 0;
}
對于第二個代碼,如果輸出是:
0 - correct
1 - under_char
2 - over_char
3 - no_upper
4 - no_at_the_rate
此外,using namespace std被認為是一種不好的做法。有關這方面的更多資訊,請查看為什么“使用命名空間標準”被認為是一種不好的做法。
uj5u.com熱心網友回復:
您可以考慮使用std::string和利用標準庫:
#include <iostream>
#include <string>
constexpr int kMinEmailCharacters = 8;
constexpr int kMaxEmailCharacters = 50;
constexpr char kAtSign = '@';
bool IsValidEmail(const std::string &email) {
auto email_length = email.length();
auto contains_uppercase = std::count_if(email.begin(), email.end(), isupper);
auto contains_at_sign = email.find(kAtSign) != std::string::npos;
return email_length > kMinEmailCharacters &&
email_length < kMaxEmailCharacters && contains_uppercase &&
contains_at_sign;
}
int main() {
std::cout << "Enter email: ";
std::string user_email;
std::cin >> user_email;
auto valid_email = IsValidEmail(user_email);
std::cout << "Valid email: " << (valid_email ? "true" : "false") << '\n';
return 0;
}
示例用法 1:
Enter email: [email protected]
Valid email: false
示例用法 2:
Enter email: [email protected]
Valid email: true
uj5u.com熱心網友回復:
代碼中有一些邏輯錯誤:
- 在第一個回圈中,當第一個字符不是大寫字母時,您會列印“電子郵件地址中沒有大寫字母”。您需要檢查所有字母才能知道是否有大寫字母
- 在第一個回圈之后,
i已經在字串的末尾,下一個回圈將不會有一次迭代。
進一步
- 采用
std::string conio.h只是窗戶。可以使用時不需要它std::cin。while(email[i] != '\0')容易出錯(您需要正確管理索引,而您的代碼無法做到這一點)。改用基于范圍的回圈- 盡量避免幻數。
- 不要使用 C 風格的演員表
- 特別是不需要將字符轉換為
int. 您可以直接比較'A'和'Z'。 - 在檢查輸入的大小是否為或
<8沒有其他情況之后,這是多余的。>=8 && <=50>51else
帶有此修復程式的代碼:
#include<iostream>
using namespace std;
void check_mail()
{
int i = 0;
std::string email;
int measure = 0;
cout<<" \n \n \n enter an email address ::";
//gets(email);
std::cin >> email;
for (char c : email) {
if( c >= 'A' && c <= 'Z') { measure = 1; }
}
if( measure != 1) {
cout<<"\n there is no uppercase letter in the email address ";
//break;
}
for (char c : email) {
if(c == '@') {
cout<<" \n found the @ character at :: "<<i<<endl;
}
}
int counter = email.size();
if(counter >=8 && counter <=50) {
cout<< "\n valid number of characters are present in the mail :: ";
}
else if(counter <8) {
cout<<" \n number of characters are less than 8 ";
}
else if(counter >=51 ) {
cout<<"\n the elements are greater than 50 ";
}
}
int main()
{
cout<<" \n \n enter a email address ";
check_mail();
return 0;
}
為輸入“[email protected]”產生預期的輸出:
enter a email address
enter an email address ::
found the @ character at :: 0
valid number of characters are present in the mail ::
我想對于最后一個輸出,您還想添加email.size()到輸出中。
uj5u.com熱心網友回復:
您考慮使用 Regex 來檢查電子郵件:
// C program for the above approach
#include <iostream>
#include <regex>
using namespace std;
// Function to check the email id
// is valid or not
bool isValid(const string& email)
{
// Regular expression definition
const regex pattern(
"(\\w )(\\.|_)?(\\w*)@(\\w )(\\.(\\w )) ");
// Match the string pattern
// with regular expression
return regex_match(email, pattern);
}
// Driver Code
int main()
{
// Given string email
string email = "[email protected]";
// Function Call
bool ans = isValid(email);
// Print the result
if (ans) {
cout << email << " : "
<< "valid" << endl;
}
else {
cout << email << " : "
<< "invalid" << endl;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/433858.html
標籤:C
下一篇:C 左移操作位操作
