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 << "\n\t******************************";
cout << "\n\n";
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