0

I have to different CPP files in a program I'm writing for fun and in my main file I call an algorithm from my IsValid cpp file. My IsValid algorithm also needs to get a variable from my main file, email, and then I'll run it through the loop I wrote.

I was wondering how to accomplish this since if I heard that it's bad coding practice to include cpp files and if I did it would cause an include loop anyways. Does any one have any suggestions on how to solve this issue.

Code Below:

#include <iostream>
#include <fstream>
#include "Validation.h"

using namespace std;

#define MAX_SIZE 260
#define NOT_FOUND -1


void main()
{
    //High level alg

    // O) Open input file and output file
    ifstream input("Email.txt");
    ofstream output("Result.txt");

    // Oa) While not at the end of input
    while(!input.eof())
    {
        char email[MAX_SIZE];
        Validation A(email);
        // Ob)Read email input
        input.getline(email, MAX_SIZE);

        // Validat email
        if (A.Valid(email))

        // Write results to output file
        output<< "1" << endl;
        else
        output << "0" << endl;
    }

    system("pause");
}

Second file:

#include <iostream>
#include <string>
#include "Validation.h"

using namespace std;

#define NOT_FOUND -1

bool IsValid(char* email)
{
    int Length, atIndex, LocalLength, DomainLength, aCheck = 0;
    char* Invalid = "()[]\\;:,<>";

    // Searches for the NULL terminator at the end of the char array and returns Length of the arry
             Validation a(email);
    Length = a.GetLength;

    if(Length <= 256)
    {
        // Finds the @ char and returns an int variable labeling its position
        Validation b(email, Length);

        atIndex = b.Find;

        aCheck++;

        if(atIndex != NOT_FOUND)
        {
            Validation c(email, atIndex);

            LocalLength = c.CheckLengthLocal;
            DomainLength = c.CheckLengthDomain;

            aCheck++;

            if(LocalLength <= 64 && DomainLength <= 253)
            {
                aCheck++;
                Validation d(email, Invalid, atIndex);

                if(d.ValidCharsL == true && d.ValidCharsD == true)
                {
                    aCheck++;
                    Validation e(email, atIndex, Length);

                    if(c.CheckRuleLocal== true && e.CheckRuleDomain == true)
                    {
                        aCheck++;

                        return true;
                    }
                }
            }
        }
    }
    if(aCheck != 5)
        return false;
}

Third file:

#define MAX_SIZE 260
#define NOT_FOUND -1

#include <iostream>

#pragma once

using namespace std;

struct Validation
{
    //Returns the length of text by scanning for null-term
int GetLength(char* text)
{
    int i;
    for(i = 0; text[i] != '\0'; i++)
    {

    }
    return i;
}

Validation::Validation(char* email)
{
    char* emailC = email;

}

Validation::Validation(char* email, int Length)
{
    char* emailC = email;
    int Size = Length;
}

Validation::Validation(char* email, int atIndex, int Length)
{
    char* emailC = email;
    int Size = Length;
    int IndA = atIndex;
}

Validation::Validation(char* email, char* Invalid, int atIndex)
{
    char* emailC = email;
    char* InNum = Invalid;
    int IndA = atIndex;
}

bool Valid(char * email)
{
    char* emailT = email;
}

bool CheckRuleLocal(char* text, int Achar)
{
    int invalid = 0;
    if(text[0] == '.'|| text[Achar - 1] == '.')
        invalid++;
    if(text[0] == '-'|| text[Achar - 1] == '-')
        invalid++;
    for(int i = 0; i < Achar && invalid == 0; i++)
    {
        if(text[i] == '.' && text[i + 1] == '.')
            invalid++;
        if(text[i] == '-' && text[i + 1] == '-')
            invalid++;
    }

    if(invalid > 0)
        return false;
    else
        return true;
}

bool CheckRuleDomain(char* text, int Achar, int length)
{
    int invalid = 0;
    if(text[Achar + 1] == '.'|| text[length - 1] == '.')
        invalid++;
    if(text[Achar + 1] == '-'|| text[length - 1] == '-')
        invalid++;
    for(int i = Achar + 1; i < MAX_SIZE && invalid == 0; i++)
    {
        if(text[i] == '.' && text[i + 1] == '.')
            invalid++;
        if(text[i] == '-' && text[i + 1] == '-')
            invalid++;
    }

    if(invalid > 0)
        return false;
    else
        return true;
}


bool ValidCharsL(char* text, char* invalidCharacters, int Achar)
{
    int invalid = 0;

    for(int i = 0; i < Achar && invalid == 0; i++)
    {
        for(int t = 0; t < GetLength(invalidCharacters); t++)
        {
            if(text[i] == invalidCharacters[t])
                invalid++;
        }
    }
    if(invalid > 0)
        return false;
    else
        return true;
}

bool ValidCharsD(char* text, char* invalidCharacters, int Achar)
{
    int invalid = 0;

    for(int i = Achar + 1; text[i] != '\0'; i++)
    {
        for(int t = 0; t < GetLength(invalidCharacters); t++)
        {
            if(text[i] == invalidCharacters[t])
                invalid++;
        }
    }
    if(invalid > 0)
        return false;
    else
        return true;
}

//Finds the position of @ and returns an int  that will store value if not found returns -1
int Find(char* text, int arraySize)
{
    int AIndex = 0;
    for(int i = 0; i <= arraySize; i++)
    {
        if(text[i] == '@')
            AIndex = i;
    }
        if(AIndex > 0)
            return AIndex;
        else
            return NOT_FOUND;

}


int CheckLengthLocal(char* text, int Achar)
{
    int count = 0;
    for(int i = 0; i != Achar; i++)
    {
        count ++;
    }
    return count;
}

int CheckLengthDomain(char* text, int Achar)
{
    int count = 0;
    for(int i = Achar + 1; text[i] != '\0'; i++)
    {
        count ++;
    }
    return count;
}
};
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
ProgInDev
  • 1
  • 1
  • 1
    Godddddddddddd.. so long post... – Nawaz Apr 09 '11 at 17:16
  • ...and so badly formatted. Welcome to StackOverflow, by the way. You may want to peruse the FAQ to get a sense of how to post good questions so that people can straightforwardly understand them and provide timely answers (though it doesn't specifically address the "wall of code" problem you present here): http://stackoverflow.com/faq – Jollymorphic Apr 09 '11 at 17:24
  • I've attempted for format your code so it's more readable, but I doubt you're going to get a lot of response anyway. I think you need to create a *small* example to show what you're trying to accomplish. – Jerry Coffin Apr 09 '11 at 17:26
  • From your code, it looks like you are passing email in as a function parameter. Isn't that sufficient to do what you want? – Jeremy Friesner Apr 09 '11 at 17:28
  • I hope you don't get offended, but your code is so wrong in so many places that probably it would be better to start over by reading a good C++ book. You can find a list of quite good ones here http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 6502 Apr 09 '11 at 17:30
  • It won't be so bad when someone who has permissions to edit fixes the formatting – alternative Apr 09 '11 at 18:24

2 Answers2

0

It is generally frowned upon to tie separate units and functions together with global variables. But if you do one way is to use an extern declaration in a header file with the actual variable being declared in one of your cpp files.

Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
0

Create header files for your cpp files and put the function prototypes you want to expose to other units in them. You can leave the actual implementations in the cpp files. Include the headers in cpp units that need the headers to compile.

I didn't go through all your code but it doesn't look like you have the separate variable you think. The variable you are passing from main is the real variable. The name used in the prototype is just a place holder that gets replaced. The units need to be compiled and the linker will sort it out.

Maybe start with something a little simpler, one or two functions in a separate unit, until you get the hang of it?

Duck
  • 26,924
  • 5
  • 64
  • 92