Generate numbers with a range and store to file.

This is a discussion on Generate numbers with a range and store to file. within the C++ Programming forums, part of the General Programming Boards category; Can some one please help me here, i have been looking around the internet for a while and no luck ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    2

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Why would you even want to add 05 at the beginning?

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    7
    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. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    2
    @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. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    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. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    7
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reserve a range in virtual memory at link time
    By pheres in forum Windows Programming
    Replies: 11
    Last Post: 12-10-2009, 11:15 AM
  2. Please help me as fast as possible
    By Xbox999 in forum C Programming
    Replies: 5
    Last Post: 11-30-2009, 06:53 PM
  3. using char to store an integer in the range [128,127]
    By nikhil22 in forum C Programming
    Replies: 5
    Last Post: 07-26-2008, 10:44 AM
  4. Using a file like Virtual Memory
    By firetheGlazer in forum C Programming
    Replies: 3
    Last Post: 07-01-2008, 05:44 PM
  5. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21