# Thread: Generate numbers with a range and store to file.

1. ## Generate numbers with a range and store to file.

Can some one please help me here, i have been looking around the internet for a while and no luck in brushing the dust off my cpp skills.

Can someone post the source for this easy program.
I want a program that generates numbers from 0000001 to 9999999 and store them to text file with each value on a new line and also before storing to the textfile it needs to add a prefix of 050.

eg. inside number.txt

0500000001
0500000002
0500000003
..
..
..
..
0509999999

please take 5min and help me here someone.

2. Why?

It would be better if you spent an hour looking at iomanip for you to figure out your own homework.

This isn't a code-on-demand service.

3. Why would you even want to add 05 at the beginning?

4. In an attempt to better my skills I decided to try and answer your problem. Now before I post my little work I hope that you understand that I am very new to c++, so my work is probably far from being optimized or being the 'best' solution. However, it works.

The hardest part of the code was finding a way to count the amount of digits a number had, in which the forum member Anon gave me the idea of counting the amount of times I could divide my number by 10, and it worked. So my thanks goes to Anon for solving this obstacle in my program.

The last problem is the giant size of the file created. I do not know if it is normal, and fear that it isn't, but the .txt file is 114 MB big. I find this odd but also cannot explain it. I hope that this code is still helpfull thought.

Code:
```#include <iostream>
#include <cstring>
#include <fstream>

using namespace std;

int intnum(int oi)
{
if (oi!=0) {
int in = 0;
while (oi>0) {
in++;
oi /= 10;
}
return in;
}
else {
return 1;
}
}

int main()
{
char vs1[15]="0";
char vs2[15]="";
char vs3[15]="";
int nd;
int tn;
int vx;

ofstream nfile ( "numbers.txt" );
for (vx=1;vx<10000000;vx++){
strcpy(vs3,vs2);
for(tn=(7-intnum(vx));tn>0;tn--){
strcat(vs3,vs1);
}
nfile<<"050"<<vs3<<vx<<"\n";
}
nfile.close();
cout<<"Done.";
cin.get();

}```
Cheers,
Smiley

5. @salem : please keep your wise comments for your personal blog, dont forget this is community to help each other. moreover its not a homework, im not even studying anywhere.

@jerimo : 050 is the area code for phone numbers.

@xSmiley : solution and the way you achieved is something new and thanks a million.
also I figured another easier way to do it, i will paste the code as soon as i login to my linux ops.

6. So it's your "JOB" ?

> Can someone post the source for this easy program.
If you wanted better help, post your code next time and stop bleating "I tried x and y for z hours and nothing worked"

You got an answer based on the perceived level of effort you had put into the problem - in other words, not much.

smiley is just a drive-by kid out to impress, rather than actually educate or inform.
Did you learn anything from the code, or are you just happy that you got your free cookie and can go back to the beach?

Oh, and while I'm on a roll...
http://catb.org/esr/faqs/smart-questions.html#id382403
http://catb.org/esr/faqs/smart-questions.html#homework

7. smiley is just a drive-by kid out to impress, rather than actually educate or inform.
With all do respect, that's slightly insulting. I apologies if my code offended you by being overly basic and inefficient. However, I fail to see the need to be called a "kid out to impress." I attempted to help him how I could, and I understand that my code isn't documented , but I assumed that it was simplistic enough to be self-explanatory. Also, even if the code does not help him, it helped me. However selfish that it is, I wrote it to my benefit to learn. I have no qualms confirming that I am not very skilled in c++. In short, I understand your vehemence in the fact that he simply asked for code without providing his attempts, but please don't let it spill unto others.

Carbonr, I do hope you post it. I would much enjoy to browse over a more efficient way to do it. I have no doubts that I'll learn more than one trick by it.

Cheers,
Smiley.

8. Originally Posted by xSmiley
I apologies if my code offended you by being overly basic and inefficient.
It is not the quality of your code that Salem was criticising. Rather, it is the fact that you basically did someone else's homework, especially when that person does not appear to have made any real attempt to do it.

I can understand that you want to practice the same question, but in that case you could just try it yourself first, and if it works, give the OP hints on what you did before you finally post it when it is clear that the deadline has passed, or that the OP has arrived at a solution.

9. Hey carbonr,
It is not that Salem said anything bad. You have to realize that a lot of people want code. But they want it for bad reasons as well. That is why we have policies. One of those is homework policy.
Is this bad? No. Ask how much people post here for homework assignments and you would agree with us that a policy needs to exist.

I would strongly suggest that there is some kind of notification when new users (below 10 posts lets say) post. Like "READ HOMEWORK POLICY!" before they post so we can avoid these situations.

At the same point, "kid out to impress" was not necessary. And probably is wrong also. In the same way you don't inform and "educate" xSmiley that he shouldn't answer questions like those and respect the homework policy as well as all the policies.

10. You are completely right Laserlight, I approached the whole topic in the wrong way. In the future, I will keep my efforts more personal and attempt to be more helpful. I should have kept some restraint.

C_ntua, I'll admit I had not read the homework policy, I will though. However obvious that it may seem now, I hadn't thought that people would come on here in search of an easy way out of their homework. As I am not personally taking classes associated with c++ or other programing languages, the thought had escaped me.

I also realise that I was in the wrong to have freely given an answer to his problem, however simplistic that is was. In this regard I can understand Salem's bluntness towards the original poster, given his seniority, Salem must have witness countless topics such as these.

I digress, I see my mistake and will not commit it again.

Cheers, Smiley