8 Replies - 239 Views - Last Post: 21 March 2013 - 10:29 AM Rate Topic: -----

#1 Asthenia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • 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  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • 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];



Instead:
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.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 Asthenia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-May 09

Re: Random Phone Number Gen

Posted 21 March 2013 - 06:51 AM

Thanks for your reply baavgai. I'm having trouble thinking about how to code this.

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

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 Asthenia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 Asthenia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-May 09

Re: Random Phone Number Gen

Posted 21 March 2013 - 09:17 AM

Good point... I've changed that now. Thanks :)
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1