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'm trying to solve a problem where I have an object resting on an inclined plane,...
I'm trying to solve a problem where I have an object resting on an inclined plane, with the angle of the plan being alpha, and the weight being w. I'm having trouble figuring out how I can calculate the component of the weight parallel to the plane. I also want to find out the weight component perpendicular to the plane. I don't want an outright answer, more of an explanation to help me understand. Thanks!
Hello, I have this question that I have been trying to solve for days. I am...
Hello, I have this question that I have been trying to solve for days. I am always able to solve the first part of the question, but I am never able to get the second part. Please help me understand how to set up this problem so I am able to solve it! Q: Let's revisit the banked curves from earlier to see another reason they are useful.We are building a road, and at one place we need to make...
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...
New to C programming and I am stuck. The program will print "no input" if no...
New to C programming and I am stuck. The program will print "no input" if no input is given. If a command line argument is given the program will print "input:" followed by the user input. Below is the code I put together to do as desired but I get errors. What do I need to fix to get my code to compile correctly? #include <stdio.h> #include <stdlib.h> int main () { char input; scanf("%c", &input); if (input == NULL)...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT