# Random Phone Number Gen

Page 1 of 1

## 8 Replies - 1077 Views - Last Post: 21 March 2013 - 10:29 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=316201&amp;s=87e1fbce1ef58e2c30d3d04deaad342d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Asthenia

Reputation: 0
• Posts: 62
• Joined: 14-May 09

# Random Phone Number Gen

Posted 21 March 2013 - 06:18 AM

Hello, I'm trying to make a Mobile Number Generator.
I'm trying to utilise pointers so I can get using them more.

so far this is how I've laid my code out.

```#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{

int * p;
int amountOfNumbers = 0;
p = new int [11];

cout << "Please enter the amount of numbers you would like" << endl;
cin >> amountOfNumbers;

for(int i = 0; i < amountOfNumbers; i++)
{
p[0] = 0;
p[1] = 7;

for(int n = 3; n < 11; n++)
{
p[n] = rand() % 9;
}

}

system("pause");
return 0;

}

```

It's not really finished. Basically what I want it to do is, ask the user how many numbers they want, then a for loop to randomly generate a number between 0 - 9 and assign it to the next array and so fourth. Obviously making sure that each number starts off with 07. Can any one help or suggest a better way of going about it.

Is This A Good Question/Topic? 0

## Replies To: Random Phone Number Gen

### #2 baavgai

• Dreaming Coder

Reputation: 7151
• Posts: 14,894
• Joined: 16-October 07

## Re: Random Phone Number Gen

Posted 21 March 2013 - 06:40 AM

This pointer is pointless:
```int * p;
p = new int [11];

```

```int p[11];

```

Do you just want to print them out? If so, then you don't need to store them. If this is actually an exercise in pointers, then you could do someting like:
```const int SIZE = 11;
int nums[SIZE];

int *p = nums, *nend = (nums+size);
*p++ = 0;
*p++ = 7;
while(p<nend) { *p++ = rand() % 9; }

```

Though, if you needed to store them, I'd just go with a string.

### #3 jimblumberg

Reputation: 5451
• Posts: 16,977
• Joined: 25-December 09

## Re: Random Phone Number Gen

Posted 21 March 2013 - 06:42 AM

What is the purpose of your variable amountOfNumbers?

What is the purpose of your for loop that uses this variable?

What is the purpose of the inner loop?

What is the significance of the "magic number" 11 you used for the size of your array?

Really none of your program really matches your stated goals. Maybe you need to sit down and break your problem statement up into individual steps using pseudo code or flow charts.

Jim

### #4 Asthenia

Reputation: 0
• Posts: 62
• Joined: 14-May 09

## Re: Random Phone Number Gen

Posted 21 March 2013 - 06:51 AM

Basically what I want to do is set up a pointer to hold 11 ints (to represent the amount of integers in a mobile number).
Then I wanted to setup a FOR loop so for every 1 (11)digit mobile number I create it always makes sure that 07 is stored in the first arrays, then the rest randomised.

So at the start of the program I want to tell the program that I want say 10 Mobile Numbers, then I want to create 10 Mobile numbers using the first 2 arrays as 07 and the rest randomised, then printed to the screen.

I'm getting the idea that I'm going to have to store using strings for every 1 (11) digit mobile number I create.

I hope I'm explaining this well enough. Thanks

This post has been edited by Asthenia: 21 March 2013 - 06:54 AM

### #5 baavgai

• Dreaming Coder

Reputation: 7151
• Posts: 14,894
• Joined: 16-October 07

## Re: Random Phone Number Gen

Posted 21 March 2013 - 07:27 AM

You only need storage if you want to reference the value later. If you simply want to print the value, and needn't save, then you just print.

e.g.
```cout << "07" << getRandDigit() << getRandDigit() ...

```

If you have a function, printRandomNumber, then you just call that amountOfNumbers times.

### #6 Asthenia

Reputation: 0
• Posts: 62
• Joined: 14-May 09

## Re: Random Phone Number Gen

Posted 21 March 2013 - 08:28 AM

Thanks Baavgai

My code looks like this and now works

```#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

void printRandomNumber(void);
void getRandDigit(void);

int main()
{

int amountOfNumbers;

cout << "Please enter the amount of mobile numbers you want to pool" << endl;
cin >> amountOfNumbers;

for(int i = 0; i < amountOfNumbers; i++)
{
printRandomNumber();
}

system("pause");
return 0;

}

void printRandomNumber(void)
{
cout << "07", getRandDigit(), getRandDigit(), getRandDigit(), getRandDigit(), getRandDigit(), getRandDigit(), getRandDigit(), getRandDigit(), getRandDigit();
cout << endl;
return;
}

void getRandDigit(void)
{
int temp;

temp = rand() % 9;
cout << temp;

return;
}
```

Can't help but feel I've taken the easy way out though. Not only am I now not using Pointers, but I feel like I've cut a use for a for loop and just typed a function call 11 times. Is there a better way of doing this, using more complex methods.

Ideally I'd like to store these generated numbers into an array, using a pointer then display the results.

Thanks again, I appreciate your help.

This post has been edited by Asthenia: 21 March 2013 - 08:32 AM

### #7 CTphpnwb

• D.I.C Lover

Reputation: 3778
• Posts: 13,687
• Joined: 08-August 08

## Re: Random Phone Number Gen

Posted 21 March 2013 - 08:37 AM

Why are you using % 9? Don't you want 9 to be a possible number? If so you want to use % 10.

### #8 Asthenia

Reputation: 0
• Posts: 62
• Joined: 14-May 09

## Re: Random Phone Number Gen

Posted 21 March 2013 - 09:17 AM

Good point... I've changed that now. Thanks

### #9 baavgai

• Dreaming Coder

Reputation: 7151
• Posts: 14,894
• Joined: 16-October 07

## Re: Random Phone Number Gen

Posted 21 March 2013 - 10:29 AM

Why loose the loop? Why the return in printRandomNumber?

Also, by convention, getXXX usually returns something.

e.g.
```int getRandDigit() { return rand() % 10; }

void printRandomNumber() {
cout << "07";
for(int n = 3; n < 11; n++) {
cout << getRandDigit();
}
cout << endl;
}

```