Hi, I\'m new to C++ and I was wondering if anyone can help with this problem tha
ID: 3694037 • Letter: H
Question
Hi, I'm new to C++ and I was wondering if anyone can help with this problem that I am struggling on? We are learning string and array so please use these method. Thank you advance for anyone who helps. It goes likes this:
Write a program that reads in an email address and checks for its validity. For the purpose of the program, an email address is valid if it satisfies all of the following four conditions.
1.The symbol @ must occur exactly once. For example, bar.cs.dpu.edu andbar@cs@dpu.edu are not valid email addresses.
2.There must be an alpha-numeric symbol (letters or digits; no other symbols)immediately to the left of the @ sign. For example, blue!@yahoo.com or@cs.dpu.edu are not valid email addresses, whileblue!3@yahoo.com andblue!3@#yahoo.com are valid email addresses.
3.There must be at least one . in the email address. For example, bar@cs is not a valid email address, while bar@cs.dpu and bar.cs.dpu@ are valid email addresses.
4.Every occurrence of the symbol . must have an alpha-numeric symbol (letters or digits; no other symbols) immediately on the both sides of the . symbol. For example, bar@c#.depaul.edu and bar@cs.~depaul.eduand bar@cs. and.bar@depaul are not valid email addresses.
Implementation Requirements:
In order to simplify the logic, make each of the four conditions a function, as below:
bool oneAt(const char email[]);
bool nonblankAt(const char email[]);
bool oneDot(const char email[]);
bool nonblankDot(const char email[]);
Input data (email addresses) must come from an input file, where an email address is indicated in one line. You can use this input file, "em1.txt". Put it in the same directory as your source code. It contains lines such as:
To read from an input file, you can use the following skeleton code. You can add lines or variables as you wish.
NOTE: The C++ function getline(), used with cin, is explained in the textbook (textbook p. 385-, 6th edition). It works with a file input stream as well ('fin' in the code above). It reads one entire line from the source input stream (where the maximum number of characters to be read is passed in as the 2nd argument), and place/copy the line in the char array (passed in as the 1st argument) with the last ' ' character replaced by '' -- thereby making the char array a cstring.
As in the example code above, ask the user for the name of the input file, just like the textbook sample program. This is to make your program more flexible.
Be sure to close the file at the end of the program.
If you detect an invalid entry, display to the terminal the entry and its line number. Here is an example run for the "em1.txt".
Explanation / Answer
#include <iostream>
#include <fstream> // for ifstream
#include <cstdlib> // for exit()
#include <cstring> // for string-handling functions
using namespace std;
// prototypes
//....
bool oneAt(const char email[]);
bool nonblankAt(const char email[]);
bool oneDot(const char email[]);
bool nonblankDot(const char email[]);
bool oneAt(const char email[]) {
int count = 0;
for (int i = 0; i < strlen(email); i++) {
if (email[i] == '@') {
count++;
}
}
if (count != 1) {
return false;
}
return true;
}
bool nonblankAt(const char email[]) {
int at;
for (int i = 0; i < strlen(email); i++) {
if (email[i] == '@') {
at = i;
}
}
if (at == 0 || at == strlen(email) - 1) {
return false;
}
if (isalnum(email[at - 1]) && isalnum(email[at + 1])) {
return true;
}
return false;
}
bool oneDot(const char email[]) {
int dot = 0;
for (int i = 0; i < strlen(email); i++) {
if (email[i] == '.') {
dot++;
}
}
if (dot >= 1) {
return true;
}
return true;
}
bool nonblankDot(const char email[]) {
int dotcount = 0;
int dot[10];
int truecount = 0;
for (int i = 0; i < strlen(email); i++) {
if (email[i] == '.') {
dot[dotcount] = i;
dotcount++;
}
}
if (dot[0] == 0 || dot[dotcount - 1] == strlen(email) - 1) {
return false;
}
for (int i = 0; i < dotcount; i++) {
if (isalnum(email[dot[i] - 1]) && isalnum(email[dot[i] + 1])) {
truecount++;
if (truecount == dotcount) {
return true;
}
}
}
return false;
}
int main()
{
char filename[16];
ifstream fin;
/* Receive input file name as input. */
cout << "Enter the input file: ";
cin >> filename;
/* Open the input file for reading. If error, exit the program. */
fin.open(filename);
if (fin.fail()) {
cerr << "Input file opening error. ";
exit(1);
}
int linenumber = 0;
/* Declare some variables. */
const int SIZE = 50; // should do for this program
char line[SIZE];
/* Process one line at a time */
// Read the first line from the input file
fin.getline(line, SIZE);
linenumber++;
// Process loop
cout << "***Invalid Entries***" << endl;
bool valid = true;
while (strlen(line) != 0) // as long as the line is not blank
{
valid = oneAt(line);
if (valid == true) {
valid = nonblankAt(line);
}
if (valid == true) {
valid = oneDot(line);
}
if (valid == true) {
valid = nonblankDot(line);
}
if (valid == false) {
cout << linenumber << ": ";
for (int z = 0; z < strlen(line); z++) {
cout << line[z];
}
cout << endl;
}
// Read the next line from the input file
fin.getline(line, SIZE);
linenumber++;
}
/* Close the input file before exit. */
fin.close();
system("pause");
return 0;
}
em1.txt
abbe@subatech.emn.fr
eahady@qu.edu.qa
aers@cm0.phy.nrc.ca
akahane@nirim.go.jp
.m.a.alam@bristol.ac.uk
ilham@qu.edu.qa
amar@igcar
anwand@fz-rossendorf.de
EAGA@maths.nott.ac.uk
nwa@ccmr.cornell.edu
asokakumar1@llnl3.gov
rumenia @rusys.eg.net
jeremie@debaerdermaeker@rug.ac.be
abar@max.ifd.uni.wroc.pl
bba@neu-.edu
barthe@cnrs-orleans.fr
KB001R@ACAD.DRAKE.EDU
CDBELING.hkucc
biasini@risc990.bologna.enea.it
hboehmer@plasma.ps.uci.edu
@boerner@physik.uni-halle.de
BORONSKI@highscreen.int.pan.wroc.pl
BORONSKI@apollo.int.!pan.wroc.pl
g.brauer@fz-rossendorf.de
gat20@altenforst.de
DTB@physci.uct.ac.
mbromley@ntu.edu.au
blb@prin.edu
brusa@science.unitn.it
sample output
Enter the input file: em1.txt
***Invalid Entries***
5: .m.a.alam@bristol.ac.uk
7: amar@igcar
12: rumenia @rusys.eg.net
13: jeremie@debaerdermaeker@rug.ac.be
15: bba@neu-.edu
18: CDBELING.hkucc
21: @boerner@physik.uni-halle.de
23: BORONSKI@apollo.int.!pan.wroc.pl
26: DTB@physci.uct.ac.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.