I'm working on a C++ project and have run into an issue that is leaving me puzzled. I am to create a phone number generator that has the user enter the first 4 numbers, and then generate all possible phone numbers that follow these two rules: The last 6 digits must equal 33. The 4th and 5th digit cannot both be even or both be odd.

This is what I've come up with so far:

    #include <iostream>

    using namespace std;


    int main()
    {//begin main
        srand(time(0));
        const int MAX_DIGITS = 10;
    int num[MAX_DIGITS] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};


        cout<<"enter the first digit: ";
        cin>>num[0];
        cout<<"Enter the second digit: ";
        cin>>num[1]; 
        cout<<"Enter the third digit: ";
        cin>>num[2]; 
        cout<<"Enter the fourth digit: ";
        cin>>num[3];


    for (int e=0;e<MAX_DIGITS;e++)
    {
        for(int f=0;f<MAX_DIGITS;f++)
        {     
            for(int g=0;g<MAX_DIGITS;g++)
            {    
                for(int h=0;h<MAX_DIGITS;h++)
                {    
                    for(int i=0; i<MAX_DIGITS;i++)
                    {
                        for(int j=0;j<MAX_DIGITS;j++)
                        {
                            if ((num[e]+num[f]+num[g]+num[h]+num[i]+num[j]) == 33  && (num[3]%2 != 0 && num[4]%2 != 0) )
                            {
                            cout<<num[0]<<num[1]<<num[2]<<num[3]<<num[e]<<num[f]<<num[g]<<num[h]<<num[i]<<num[j]<<endl;
                            }
                        }       
                    }
                }
            }   
        }
    }

It all makes sense to me so far, but the program is displaying some numbers multiple times, and I'm not entirely certain how to make sense of the even/odd rule.

I'm still a rookie to programming and I'm sure that there may be a more efficient way to do this, but I'm trying my best and this has left me puzzled. Any help would be appreciated.

Thanks in advance!

EDIT: My question is this, how do I get the generator to display the numbers with the even/odd rule applied? My best idea was to use the modulus operator (%) to see if the remainder of the numbers divided by two was zero, and if so, the numbers were even. This is where I stumble a bit though, because I'm not perfectly certain how to implement this. Sorry for not being more specific the first time.

有帮助吗?

解决方案

1) You're not ever changing the values in the num array, so testing to see if it contains a valid number doesn't work because the initial values you set don't fit the rules.
2) The validation is checking to see if both numbers are odd, not one or the other.

Here's a version that seems to work. The changes I made are to actually change the num array and then use a helper function to validate the numbers in the array so you don't have a mess inside your loops. I removed the srand call since you aren't using random numbers and the input of the first 4 digits to make testing easier for me. You can add that back if you like.

#include <iostream>

const int MAX_DIGITS = 10;
bool IsValid(int num[MAX_DIGITS])
{
    int sum = 0;
    for(int z = 4; z < MAX_DIGITS; ++z)
    {
        sum += num[z];
    }
    if(sum != 33)
    {
        return false;
    }
    int numodd = 0;
    for(int z = 3; z < 5; ++z)
    {
        numodd += (num[z] % 2);
    }
    if(numodd != 1)
    {
        return false;
    }
    return true;
}

int main()
{
    int num[MAX_DIGITS];
    num[0] = 5;
    num[1] = 5;
    num[2] = 5;
    num[3] = 1;

    for (int e=0;e<MAX_DIGITS;e++)
    {
        num[4] = e;
        for(int f=0;f<MAX_DIGITS;f++)
        {     
            num[5] = f;
            for(int g=0;g<MAX_DIGITS;g++)
            {    
                num[6] = g;
                for(int h=0;h<MAX_DIGITS;h++)
                {    
                    num[7] = h;
                    for(int i=0; i<MAX_DIGITS;i++)
                    {
                        num[8] = i;
                        for(int j=0;j<MAX_DIGITS;j++)
                        {
                            num[9] = j;
                            if(IsValid(num))
                            {
                                for(int z = 0; z < MAX_DIGITS; ++z)
                                {
                                    if(z == 3 || z == 6)
                                    {
                                        std::cout << '-';
                                    }
                                    std::cout << num[z];
                                }
                                std::cout << std::endl;
                            }
                        }       
                    }
                }
            }   
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top