A robust program always checks user input and provides error checking and handling. The program appears to assume that the user will enter a valid 3-digit area code, but no provision is made to assure this. The variable input is not very descriptive. 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. Another way to write that would be like this: std::cout << "\n" Std::cout << "\tAvailable numbers in your area." The main function includes these lines: std::cout << "\n" In particular, instead of rand, you might want to look at std::uniform_real_distribution and friends in the header. Consider using a better random number generatorīecause you're using a compiler that supports at least C 11, consider using a better random number generator. Your compiler is probably also smart enough to tell you that, if you ask it to do so. In this code, none of the digit_ variables are ever actually used. #Phone number generator code#Unused variables are a sign of poor code quality, so eliminating them should be a priority. Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid. Here are some observations that may help you improve your code. Then here is an example of using the getrandomdigit function Enter three digits(area code): 203 Here's a visual example of using the digit vars Enter three digits(area code): 203 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. So my question remains, why does it occur when you use the integers that the numbers AREN'T randomly generated. But before I added the function getrandomdigit(), I was using the manually defined "digit" vars in an attempt to generate pseudorandom numbers. So I believe I understand the logic of how the random number generation works. << getrandomdigit() << getrandomdigit() << getrandomdigit() << getrandomdigit() << "-" << getrandomdigit() This is the code I came up with #include Ĭout << "Enter three digits(area code): " Ĭout << "\tAvailable numbers in your area." Ĭout << "\n\t******************************" Ĭout << "\nPhone number: " << input << "-" << getrandomdigit() << getrandomdigit() I was tinkering around with C after about 3 days of learning and decided to make a random phone number generator.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |