for this function i need help with the email part. I am trying to string compare domain with 3 different string possibilities (.com,.org,.edu).

void add(char firstName[][32],char number[][9],char email[][32],char lastName[][32],int count)
{
    int i;
    int length;
    char domain[3];

    cout << "enter their name\n";
    cin  >> firstName[count];
    cin  >> lastName[count];
    cout << "enter their number\n";
    cin  >> number[count];
    cout << "enter their email\n";
    cin  >> email[count];
    length=strlen(email[count]);
    if(length>=4)
    {
        for(i=0;i<4;i++)
        {
            domain[i]=email[count][length-(i+1)];
        }
    }
    if (strcmp(domain,"moc.")!=0 || strcmp(domain,"gro.")!=0 || strcmp(domain,"ude.")!=0)
    {
        cout <<"invalid email";
    }
}

Recommended Answers

All 4 Replies

The domain name can't be moc gro or ude all at the same time. So at least two of the three conditions must be true and given the OR operator that means the whole condition is true. How about using the == operator instead of the != operator and in the body of the if statement proceed instead of bail, and use an else to bail if you don't find a suitable domain name?

if (strcmp(domain,"moc.") == 0 || strcmp(domain,"gro.") == 0 || strcmp(domain,"ude.") == 0)
{
    cout << " a valid domain name was found" << endl;
    //proceed
}
else
{
    cout << "invalid domain name" << endl;
}

here is my full code so far... it is still giving me errors, and telling me stack around variable domain is corrupted

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

void mainMenu();
void add(char[][32],char[][9],char[][32],char[][32],int);
int main()
{
	const int PEOPLE=20;

	char firstName[PEOPLE][32];
	char lastName[PEOPLE][32];
	char phoneNumber[PEOPLE][9];
	char email[PEOPLE][32];
	int count=0;

	bool cheese=false;
	while(!cheese)
	{
		mainMenu();
		char menu;
		cin  >> menu;

		if (menu == 'A'||menu == 'a')
		{
			if (count<PEOPLE)
			{
				add(firstName,phoneNumber,email,lastName,count);
				count++;
			}
		}
		if (menu == 'D'||menu == 'd')
		{
			
		}
		else if (menu == 'F'||menu == 'f')
		{
			cout<<"roar2";
		}
		else if (menu == 'L'||menu == 'l')
		{
			cout<<"roar3";
		}
		else if (menu == 'S'||menu == 's')
		{
			cout<<"roar4";
		}
		else if (menu == 'E'||menu == 'e')
		{
			return 0;
			
		}
	}
		return 0;
}


void mainMenu()
{
	cout << "----Main Menu----\n";
	cout << "A - Add Person\n";
	cout << "D - Delete Person\n";
	cout << "F - Find and Display Person\n";
	cout << "L - List All People\n";
	cout << "S - Save List\n";
	cout << "E - Exit\n";
}

void add(char firstName[][32],char number[][9],char email[][32],char lastName[][32],int count)
{
	int i;
	int length;
	char domain[3];

	cout << "enter their name\n";
	cin  >> firstName[count];
	cin  >> lastName[count];
	cout << "enter their number\n";
	cin  >> number[count];
	cout << "enter their email\n";
	cin  >> email[count];
	length=strlen(email[count]);
	if(length>=4)
	{
		for(i=0;i<4;i++)
		{
			domain[i]=email[count][length-(i+1)];
		}
	}
	if (strcmp(domain,"moc.")==0 || strcmp(domain,"gro.")==0 || strcmp(domain,"ude.")==0)
	{
		cout <<"good";
	}
	else
	{
		cout <<"bad";
	}
}

domain is declared with capacity of 3 but you are trying to read 4 non null chars into it. This will cause an error that may be called several different things, including possibly a stack error. Declare domain to have capacity of 5 instead of 3 and be sure to put a null char at index 4 so domain becomes a null terminated char array so it can be used in strcmp() and see what happens.

awesome! thanks so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.