C++ Random Phone Number Generator

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
8
down vote

favorite
3












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()

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 << "Enter three digits(area code): ";
getline(cin, input);
cout << "n";
cout << "tAvailable numbers in your area.";
cout << "nt******************************";

cout << "nn";

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

cout<<"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






share|improve this question





















  • What are "digit vars"? I don't get it.
    – Andreas
    Mar 24 at 14:16






  • 1




    @Andreas Not "digit wars", I hope! :)
    – Cris Luengo
    Mar 24 at 14:37






  • 1




    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.
    – Cris Luengo
    Mar 24 at 14:38










  • Could you show us the "before" code? I'm guessing it had a bug.
    – Barry Carter
    Mar 24 at 15:06










  • @BarryCarter the digit vars were basically what you see for getrandomdigit()
    – wardialer
    Mar 24 at 20:00
















up vote
8
down vote

favorite
3












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()

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 << "Enter three digits(area code): ";
getline(cin, input);
cout << "n";
cout << "tAvailable numbers in your area.";
cout << "nt******************************";

cout << "nn";

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

cout<<"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






share|improve this question





















  • What are "digit vars"? I don't get it.
    – Andreas
    Mar 24 at 14:16






  • 1




    @Andreas Not "digit wars", I hope! :)
    – Cris Luengo
    Mar 24 at 14:37






  • 1




    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.
    – Cris Luengo
    Mar 24 at 14:38










  • Could you show us the "before" code? I'm guessing it had a bug.
    – Barry Carter
    Mar 24 at 15:06










  • @BarryCarter the digit vars were basically what you see for getrandomdigit()
    – wardialer
    Mar 24 at 20:00












up vote
8
down vote

favorite
3









up vote
8
down vote

favorite
3






3





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()

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 << "Enter three digits(area code): ";
getline(cin, input);
cout << "n";
cout << "tAvailable numbers in your area.";
cout << "nt******************************";

cout << "nn";

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

cout<<"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






share|improve this question













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()

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 << "Enter three digits(area code): ";
getline(cin, input);
cout << "n";
cout << "tAvailable numbers in your area.";
cout << "nt******************************";

cout << "nn";

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

cout<<"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








share|improve this question












share|improve this question




share|improve this question








edited Mar 24 at 15:08









200_success

123k14142399




123k14142399









asked Mar 24 at 13:35









wardialer

554




554











  • What are "digit vars"? I don't get it.
    – Andreas
    Mar 24 at 14:16






  • 1




    @Andreas Not "digit wars", I hope! :)
    – Cris Luengo
    Mar 24 at 14:37






  • 1




    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.
    – Cris Luengo
    Mar 24 at 14:38










  • Could you show us the "before" code? I'm guessing it had a bug.
    – Barry Carter
    Mar 24 at 15:06










  • @BarryCarter the digit vars were basically what you see for getrandomdigit()
    – wardialer
    Mar 24 at 20:00
















  • What are "digit vars"? I don't get it.
    – Andreas
    Mar 24 at 14:16






  • 1




    @Andreas Not "digit wars", I hope! :)
    – Cris Luengo
    Mar 24 at 14:37






  • 1




    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.
    – Cris Luengo
    Mar 24 at 14:38










  • Could you show us the "before" code? I'm guessing it had a bug.
    – Barry Carter
    Mar 24 at 15:06










  • @BarryCarter the digit vars were basically what you see for getrandomdigit()
    – wardialer
    Mar 24 at 20:00















What are "digit vars"? I don't get it.
– Andreas
Mar 24 at 14:16




What are "digit vars"? I don't get it.
– Andreas
Mar 24 at 14:16




1




1




@Andreas Not "digit wars", I hope! :)
– Cris Luengo
Mar 24 at 14:37




@Andreas Not "digit wars", I hope! :)
– Cris Luengo
Mar 24 at 14:37




1




1




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.
– Cris Luengo
Mar 24 at 14:38




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.
– Cris Luengo
Mar 24 at 14:38












Could you show us the "before" code? I'm guessing it had a bug.
– Barry Carter
Mar 24 at 15:06




Could you show us the "before" code? I'm guessing it had a bug.
– Barry Carter
Mar 24 at 15:06












@BarryCarter the digit vars were basically what you see for getrandomdigit()
– wardialer
Mar 24 at 20:00




@BarryCarter the digit vars were basically what you see for getrandomdigit()
– wardialer
Mar 24 at 20:00










2 Answers
2






active

oldest

votes

















up vote
14
down vote



accepted










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 << "nt******************************";
std::cout << "nn";


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."
"nt******************************"
"nn";


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.






share|improve this answer






























    up vote
    8
    down vote













    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.



    Misc



    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.






    share|improve this answer





















    • you might want to link to codereview.stackexchange.com/questions/101525 for random number generation
      – WorldSEnder
      Mar 24 at 18:12











    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190365%2fc-random-phone-number-generator%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    14
    down vote



    accepted










    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 << "nt******************************";
    std::cout << "nn";


    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."
    "nt******************************"
    "nn";


    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.






    share|improve this answer



























      up vote
      14
      down vote



      accepted










      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 << "nt******************************";
      std::cout << "nn";


      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."
      "nt******************************"
      "nn";


      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.






      share|improve this answer

























        up vote
        14
        down vote



        accepted







        up vote
        14
        down vote



        accepted






        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 << "nt******************************";
        std::cout << "nn";


        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."
        "nt******************************"
        "nn";


        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.






        share|improve this answer















        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 << "nt******************************";
        std::cout << "nn";


        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."
        "nt******************************"
        "nn";


        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.







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Mar 24 at 21:06









        NoOneIsHere

        1827




        1827











        answered Mar 24 at 16:09









        Edward

        44.1k374201




        44.1k374201






















            up vote
            8
            down vote













            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.



            Misc



            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.






            share|improve this answer





















            • you might want to link to codereview.stackexchange.com/questions/101525 for random number generation
              – WorldSEnder
              Mar 24 at 18:12















            up vote
            8
            down vote













            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.



            Misc



            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.






            share|improve this answer





















            • you might want to link to codereview.stackexchange.com/questions/101525 for random number generation
              – WorldSEnder
              Mar 24 at 18:12













            up vote
            8
            down vote










            up vote
            8
            down vote









            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.



            Misc



            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.






            share|improve this answer













            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.



            Misc



            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.







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Mar 24 at 15:07









            yuri

            3,3862832




            3,3862832











            • you might want to link to codereview.stackexchange.com/questions/101525 for random number generation
              – WorldSEnder
              Mar 24 at 18:12

















            • you might want to link to codereview.stackexchange.com/questions/101525 for random number generation
              – WorldSEnder
              Mar 24 at 18:12
















            you might want to link to codereview.stackexchange.com/questions/101525 for random number generation
            – WorldSEnder
            Mar 24 at 18:12





            you might want to link to codereview.stackexchange.com/questions/101525 for random number generation
            – WorldSEnder
            Mar 24 at 18:12













             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190365%2fc-random-phone-number-generator%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Python Lists

            Aion

            JavaScript Array Iteration Methods