Question

In: Computer Science

I am trying to solve a c++ problem over c strings where I have to input...

I am trying to solve a c++ problem over c strings where I have to input an email address and check if it is valid. After completing my code I keep getting errors that my program will not run, specifically the lines with my for loops. Can you please look at my code and tell me what is wrong and how I can fix the code? I included below the assignment instructions and my current code.

Assignment Instructions

Write a method that will have a C++ string passed to it. The string will be an email address and the method will return a bool to indicate that the email address is valid.

Email Validation Rules: ( These rules are not official.)

1) No whitespace allowed

2) 1 and only 1 @ symbol

3) 1 and only 1 period allowed after the @ symbol. Periods can exist before the @ sign.

4) 3 and only 3 characters after the period is required.

Program will prompt for an email address and report if it is valid or not according to the rules posted above. The program will loop and prompt me to continue.

My Current Code:

#include<iostream>
#include <string>
using namespace std;

string emailAddress;


bool validEmail(string emailAddress)
{
   int whiteSpace = 0;
   int at = 0;
   int period = 0;
   int characterCount = 0;
  

   for (int index = 0; index < emailAddress.length; index++) // Error
   {
       if (emailAddress[index] == ' ')
           whiteSpace++;

       else if (emailAddress[index] == '@')
           at++;
   }

   if (whiteSpace > 0 || at != 1)
       return false;
  
   else
   {
      
       for (int index = at + 1; index < emailAddress.length; index++) // Error
       {
           if (emailAddress[index] == '.')
               period++;

       }
      
       if (period != 1)
           return false;
      
       else
       {
           for (int index = period + 1; index < emailAddress.length; index++) //Error
           {
               characterCount++;
           }

           if (characterCount == 3)
               return true;
           else
               return false;
       }
   }
}

int main()
{
   while (true);
   {
       cout << "Enter an email address and I will check if it's valid" << endl;
       getline(cin, emailAddress);

       validEmail(emailAddress);

       if (true)
           cout << "This email address is valid";
       else
           cout << "This email address is not valid";

   }
  
  
   return 0;
}

Solutions

Expert Solution

#include<iostream>
#include <string>
using namespace std;

string emailAddress;

bool validEmail(string emailAddress)
{
int whiteSpace = 0;
int at = 0;
int period = 0;
int characterCount = 0;
int at_index=0;
int period_index=0;
  

for (int index = 0; index < emailAddress.length(); index++)
{
if (emailAddress[index] == ' ')
whiteSpace++;

else if (emailAddress[index] == '@')
{
at++;
at_index=index;
//cout<<at;
}
}

if (whiteSpace > 0 || at != 1)
{
// cout<<"hai";
return false;
  
}
  
else
{
  
for (int index = at_index + 1; index < emailAddress.length(); index++)
{
if (emailAddress[index] == '.')
{
period_index=index;
period++;
}

}
  
if (period != 1)
{
// cout<<"hmm";
return false;
}
  
else
{
for (int index = period_index + 1; index < emailAddress.length(); index++)
{
characterCount++;
}

if (characterCount == 3)
{
// cout<<"hello";
return true;
}
else
{
// cout<<"hellooo";
return false;
}
}
}
}

int main()
{
bool val;
while (true)
{
cout << "\nEnter an email address and I will check if it's valid" << endl;
getline(cin, emailAddress);

val=validEmail(emailAddress);

if (val)
cout << "\nThis email address is valid";
else
cout << "\nThis email address is not valid";

}
  
  
return 0;
}


Related Solutions

I am trying to solve this problem, but I don't know which test to use: For...
I am trying to solve this problem, but I don't know which test to use: For average risk funds only, test the proportion of funds with Sales Charges is not 50% use α = 0.10. Explain your conclusion. Here is the contingency table with the data: Sales charge Yes No total Risk Low 6 8 14 Avg 42 34 76 High 24 23 47 Total 72 65 137 Context of the problem: Mutual funds are the most common way people...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
I am trying to solve this question: "You are working on a school project at the...
I am trying to solve this question: "You are working on a school project at the library when your friend Jane taps you on the shoulder. She cannot seem to connect to a certain website that she needs for her class. Fortunately, you know enough about Windows and networking to help troubleshoot the problem. You open a Windows command prompt and ......" The dots at the end of the story indicates that I need to continue the story. However, I...
In C programming, I am trying to search for the names of people that in this...
In C programming, I am trying to search for the names of people that in this DOISigned.txt file, however I am having trouble getting the first and last names of the multiple people named john, my current code only searches for John once and then it terminates,here is my current code #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #define BUF_SIZE 0x3000 char buf[BUF_SIZE]; int main() {    char* inputFile = "DOISigners.txt";    FILE* fp;    fp = fopen(inputFile, "r");...
I am not sure where, to begin with, this problem... "You have found the following historical...
I am not sure where, to begin with, this problem... "You have found the following historical information for DEF Company:                          Year1       Year 2        Year 3        Year 4 Stock Price        $46.29         $49.74          $54.55          $57.07 EPS                  $2.04 $2.9    $2.81    $3.78 Earnings are expected to grow at 8 percent for the next year. Using the company's historical average PE as a benchmark, what is the target stock price in one year?"
I have homework on this but I am not sure how to solve it and which...
I have homework on this but I am not sure how to solve it and which formula to use in excel, can u please help me Rebecca is considering buying a 2019 Genesis G70 costing $37,900 and finds that the retaining values of the vehicle over the next four years are as follows: Percent of the total value retained after 24 months: 71% Percent of the total value retained after 48 months: 53% If her interest rate is 5% compounded...
I am trying to write a code in C for a circuit board reads in a...
I am trying to write a code in C for a circuit board reads in a temperature from a sensor on the circuit board and reads it onto a 7-segment LED display. How exactly would you code a floating 2 or 3 digit reading onto the LED display? I am stuck on this part.
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
I have the problem in bold below. I am completely confused. I am hoping that I...
I have the problem in bold below. I am completely confused. I am hoping that I will see how you did this and will be able to follow it. Please remember that I have just started this so please don't use any complex features that an absolute beginner (like me) wouldn't know. Additional information:   /*This is what everyone owes before tax and tip:     Person 1: $10     Person 2: $12     Person 3: $9     Person 4: $8     Person 5: $7     Person 6:...
/* I been trying to solve this problem . this is my own coding /here is...
/* I been trying to solve this problem . this is my own coding /here is the HW INSTRUCTION Let the user to try of maximum 8 times to find the random number For every try display if the user find or not the number and the number of tries At the end of each game display to the user the number of tries to find the number Allow the user to play a maximum of 4 times, by asking...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT