#include<iostream>
using namespace std;
int main()
{
char sEmail[10];
char *pAt = NULL;
char *pDot = NULL;
int iAtFlg = 0;
int iDotFlg = 0;
int iLineNum = 1;
cout << "Line Num : " << iLineNum;
iLineNum ++;
while(cin >> sEmail)
{
iAtFlg = 0;
iDotFlg = 0;
if (sEmail[0] == '@')
{
cout << "Error 1: First char is @" << endl;
cout << endl << "Line Num : " << iLineNum << " ";
iLineNum ++;
}
else
{
pAt = strchr(sEmail, '@');
if (NULL == pAt)
{
cout << "Error 2: No @" << endl;
cout << endl << "Line Num : " << iLineNum << " ";
iLineNum ++;
}
else
{
pDot = strchr(sEmail, '.');
if (NULL == pDot)
{
cout << "Error 3: No ." << endl;
cout << endl << "Line Num : " << iLineNum << " ";
iLineNum ++;
}
else
{
if(pDot - pAt <= 1)
{
cout << "Error 4: @ next is ." << endl;
cout << endl << "Line Num : " << iLineNum << " ";
iLineNum ++;
}
else
{
if ('\0' == *(pDot + 1))
{
cout << "Error 5: Last char is ." << endl;
cout << endl << "Line Num : " << iLineNum << " ";
iLineNum ++;
}
else
{
cout << "Email " << sEmail << "is an legitimate" << endl;
}
}
}
}
}
}
return 0;
}

本文介绍了一个简单的C++程序,用于检查用户输入的电子邮件地址是否符合基本的格式要求。程序通过查找‘@’和‘.’字符来判断邮件地址的有效性,并针对几种常见的错误情况给出提示。
1176

被折叠的 条评论
为什么被折叠?



