C++ Random Phone Number Generator I was tinkering around with C++ after about 3 days of learning and decided to make a random phone number generator. 

This is the code I came up with 

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;ctime&gt;
#include &lt;cstdlib&gt;

using namespace std;
int getrandomdigit();

int main()
{
    srand(time(NULL));
    string input;
    int digit1_1;
    int digit1_2;
    int digit1_3;

    int digit2_1;
    int digit2_2;
    int digit2_3;
    int digit2_4;

    cout &lt;&lt; "Enter three digits(area code): ";
    getline(cin, input);
    cout &lt;&lt; "\n";
    cout &lt;&lt; "\tAvailable numbers in your area.";
    cout &lt;&lt; "\n\t******************************";

    cout &lt;&lt; "\n\n";

    for(int i = 0; i &lt; 10; ++i)
        cout &lt;&lt; "\nPhone number: " &lt;&lt; input &lt;&lt; "-" &lt;&lt; getrandomdigit() &lt;&lt; getrandomdigit()
        &lt;&lt; getrandomdigit() &lt;&lt; "-" &lt;&lt; getrandomdigit()
        &lt;&lt; getrandomdigit() &lt;&lt; getrandomdigit() &lt;&lt; getrandomdigit();

    cout&lt;&lt;"\n";
    return 0;

}

int getrandomdigit()
{
    return rand() % 10;
}


So I believe I understand the logic of how the random number generation works. But before I added the function getrandomdigit(), I was using the manually defined "digit" vars in an attempt to generate pseudorandom numbers. So my question remains, why does it occur when you use the integers that the numbers AREN'T randomly generated. For instance, if you were to enter an area code, it would just print the same 2 prefixes 10 times, but when you use the function, it gives you a completely random var every time the counter is run until 10.

Here's a visual example of using the digit vars

Enter three digits(area code): 203

        Available numbers in your area.
        ******************************


Phone number: 203-523-3023
Phone number: 203-523-3023
Phone number: 203-523-3023
Phone number: 203-523-3023
Phone number: 203-523-3023
Phone number: 203-523-3023
Phone number: 203-523-3023
Phone number: 203-523-3023
Phone number: 203-523-3023
Phone number: 203-523-3023


Then here is an example of using the getrandomdigit function

Enter three digits(area code): 203

        Available numbers in your area.
        ******************************


Phone number: 203-696-3428
Phone number: 203-832-2940
Phone number: 203-293-9390
Phone number: 203-483-3935
Phone number: 203-616-6955
Phone number: 203-089-8674
Phone number: 203-953-6456
Phone number: 203-516-2123
Phone number: 203-800-7836
Phone number: 203-624-7130
Here are some observations that may help you improve your code.

Don't abuse using namespace std

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid.  

Eliminate unused variables

Unused variables are a sign of poor code quality, so eliminating them should be a priority.  In this code, none of the digit_ variables are ever actually used.  My compiler also tells me that.  Your compiler is probably also smart enough to tell you that, if you ask it to do so. 

Consider using a better random number generator

Because you're using a compiler that supports at least C++11, consider using a better random number generator.  In particular, instead of rand, you might want to look at std::uniform_real_distribution and friends in the &lt;random&gt; header.

Use string concatenation

The main function includes these lines:

std::cout &lt;&lt; "\n";
std::cout &lt;&lt; "\tAvailable numbers in your area.";
std::cout &lt;&lt; "\n\t******************************";
std::cout &lt;&lt; "\n\n";


Each of those is a separate call to operator&lt;&lt; but they don't need to be.  Another way to write that would be like this:

std::cout &lt;&lt; "\n"
            "\tAvailable numbers in your area."
            "\n\t******************************"
            "\n\n";


This reduces the entire menu to a single call to operator&lt;&lt; because consecutive strings in C++ (and in C, for that matter) are automatically concatenated into a single string by the compiler.

Consider improving names

The variable input is not very descriptive.  Perhaps areaCode would be a better name. 

Add error checking

The program appears to assume that the user will enter a valid 3-digit area code, but no provision is made to assure this.  A robust program always checks user input and provides error checking and handling.

Use object orientation

Because you're writing in C++, it would make sense to have a class such as PhoneNumber to encapsulate the details of your implementation.  You may not yet have learned about objects or classes, but they're one of the main strengths of C++ and something you should learn soon if you haven't already.  Use objects where they make sense.

Understand the problem domain

In the US and Canada (which, by the formatting of the phone number, seems to be where this is intended to be used), phone numbers are created according to the North American Numbering Plan.  In that plan, the second grouping of numbers is called that Central office code.  That three-digit number must start with a number in the range of 2 through 9 and cannot start with a 0 or 1.  Your current program does not obey this scheme and generates invalid numbers.