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 <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;
int getrandomdigit();

int main()
    string input;
    int digit1_1;
    int digit1_2;
    int digit1_3;

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

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

    cout << "\n\n";

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

    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
  • \$\begingroup\$ What are "digit vars"? I don't get it. \$\endgroup\$
    – Andreas
    Commented Mar 24, 2018 at 14:16
  • 1
    \$\begingroup\$ @Andreas Not "digit wars", I hope! :) \$\endgroup\$ Commented Mar 24, 2018 at 14:37
  • 1
    \$\begingroup\$ I think the "digit vars" refers to the uninitialized variables? They are filled with whatever was there before in memory. It's not random. If your program is loaded in the same place every time you run it, it'll read the same data unless that data gets overwritten by some other process in the meantime. \$\endgroup\$ Commented Mar 24, 2018 at 14:38
  • \$\begingroup\$ Could you show us the "before" code? I'm guessing it had a bug. \$\endgroup\$
    – user1149
    Commented Mar 24, 2018 at 15:06
  • \$\begingroup\$ @BarryCarter the digit vars were basically what you see for getrandomdigit() \$\endgroup\$
    – wardialer
    Commented Mar 24, 2018 at 20:00

2 Answers 2


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 <random> header.

Use string concatenation

The main function includes these lines:

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

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

std::cout << "\n"
            "\tAvailable numbers in your area."

This reduces the entire menu to a single call to operator<< 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.


Don't use using namespace std

It's a bad habit to get into even for just small programs. Just don't do it.

Don't use rand()

You should use the random number library provided by the standard instead. If you're interested in some more background info then you can watch this video.


You don't need return 0 in main as the compiler will generate it automatically.

Instead of generating and appending random single digits you should create a function that generates a number in a given range.

The way you initialize the random number generator with time means fast subsequent calls will yield the same result.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.