0

Target: the valid telephone format is: 514 999-9999 (canadian national format).
We must have a regional code from a list, space and a hyphen.

Here is my code: I have an error on line dim[i]=espace;

passing const string as this argument discard qualifiers

I'm stuck, any help would be appreciated.

bool validerTelephone(const std::string& p_telephone) {
    string code_reg[28]={"403","780","604","236","250","778","902","506","514","408","204","905","519","289",
            "705","613","807","416","647","438","450","579","418","581","819","306","709","867"};
    bool etat;
    if(p_telephone.size()!=12){
        etat=false;

    }

    else {
        string mon_code_reg=p_telephone.substr (0,3);
        bool pas_trouve=true;
        int i=0;
        while (pas_trouve){
            if(mon_code_reg.compare(code_reg[i])==0)
                pas_trouve=false;


            char tire='-';
            char espace=' ';
            bool formatvalide=true;
            unsigned int dim=p_telephone.size();
            string telephone_valide=p_telephone;
            for(int i=0;i<dim;i++){
                if(i==3){
                    dim[i]=espace;

                }
                else if(i==7){
                    dim[i]=tire;

                }
                else{
                    cout<<"format telephone"<<p_telephone<<endl;
                }
            }
             formatvalide=false;
        }
        return etat;
    }
}
O'Neil
  • 3,790
  • 4
  • 16
  • 30
  • 1
    At first you have defined _dim_ as an unsigned int (`unsigned int dim=p_telephone.size();` later, you try to access it as an array (`dim[i]=espace;`). This is what is causing the error. Maybe you should create an array instead? – Adriano Martins Apr 01 '18 at 21:11
  • i have changed it to unsigned int dim[], but still same error unsigned int dim[]=p_telephone.size(); //string telephone_valide[]=p_telephone; for(int i=0;i – yassine lam Apr 01 '18 at 23:08
  • After posting my answer, I noticed that the question was tagged as C++03. Are you able to use C++11 for your project? – Samantha Apr 02 '18 at 01:48

1 Answers1

0

After posting a solution using regex and STL, I realized that OP had the question tagged as C++03, which made my answer invalid. Here is an alternative approach using sscanf. Recall that sscanf returns the number of format specifiers it matches.

bool validerTelephoneSscanf(const std::string& p_telephone)
{
    int region = 0;
    int first = 0;
    int second = 0;

    int code_reg[] = { 403,780,604,236,250,778,902,506,514,408,204,905,519,289,
        705,613,807,416,647,438,450,579,418,581,819,306,709,867 };

    if (p_telephone.length() == 12 && sscanf(p_telephone.c_str(), "%3d %3d-%4d", &region, &first, &second) == 3)
    {
        for (unsigned i = 0; i < sizeof(code_reg)/sizeof(code_reg[0]); i++)
        {
            if (code_reg[i] == region)
            {
                return true;
            }
        }
    }
    return false;
}

Original solution that uses regex and some standard library functions. It's recommended to use regex pattern-matching instead of doing parsing 'by hand', which is often tedious and error-prone.

bool validerTelephone(const std::string& p_telephone)
{
    //
    // Declare this as a vector so that we can use STL to easily search
    // it later
    //
    vector<string> code_reg = { "403","780","604","236","250","778","902","506","514","408","204","905","519","289",
        "705","613","807","416","647","438","450","579","418","581","819","306","709","867" };

    //
    // If writing a single-exit function, declare result variable false
    // and only set it to true when the success conditions are met
    //
    bool etat = false;

    //
    // The regex is delimited with start of line (^) and end of line ($)
    // because OP's original code enforced p_telephone.size() == 12
    //
    std::regex pattern("^([0-9]{3}) [0-9]{3}-[0-9]{4}$");
    std::smatch match;
    std::string region;  

    //
    // Match the input string to the pattern
    //
    if (std::regex_search(p_telephone, match, pattern)) 
    {
        // 
        // 'region' is the capture group at the start of the regex, i.e. ([0-9]{3})
        // We can access it via a member function of our std::smatch instance
        //
        region = match.str(1);

        //
        // A standard idiom to determine if the region (e.g. 514) is one of the valid
        // code regions contained in the code_reg vector
        //
        if (std::find(code_reg.begin(), code_reg.end(), region) != code_reg.end())
        {           
            etat = true;    // The telephone code region is valid, so all requirements are met
            std::cout << "format telephone " << p_telephone << '\n';
        }
    }   

    return etat;
}
Samantha
  • 975
  • 6
  • 20
  • After posting this I noticed that the question was tagged as C++03, in which case my answer is not valid. Will leave it here for now as other users might find it helpful. – Samantha Apr 02 '18 at 01:47
  • yeah, we cannot use Regex, but i will keep digging thank you. – yassine lam Apr 02 '18 at 03:28
  • @yassinelam Updated solution - let me know if this approach works. – Samantha Apr 02 '18 at 05:56
  • hi, sorry i didn't understand this expression for (unsigned i = 0; i < sizeof(code_reg)/sizeof(code_reg[0]); i++) – yassine lam Apr 03 '18 at 01:58
  • `sizeof(code_reg)/sizeof(code_reg[0])` evaluates to the number of elements of the `code_reg` array. See https://stackoverflow.com/questions/33523585/how-do-sizeofarr-sizeofarr0-work for a more complete explanation. Also, `unsigned` is short for `unsigned int`; of course, simply `int` here would also have worked. – Samantha Apr 03 '18 at 02:09
  • If you found this solution helpful, please accept it as the answer. – Samantha Apr 05 '18 at 00:54