CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2008
    Location
    Montreal, Quebec, Canada
    Posts
    49

    [RESOLVED] Simple program crashes

    Hi. I'm completely new to C++ (and to this site) and I am having trouble with the following code:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    long cnum=1;
    const unsigned long fin = 100000;
    const unsigned long group = 100;
    unsigned long cursec=0;
    
    
    ofstream destination("results.txt");
    
    
    
    bool checkPrime (unsigned long num)
    {
          for (int i=2;i<num;++i)
          {
                if ((num % i) == 0)
                       return false;
          }
                return true;
    }
    
    
    class sector
    {
          public:
          sector ()
                 {
                       destination << (cursec*group)+1 << " - " << (cursec+1)*group << " ";
                       ++cursec;
                 }
    
          ~sector ()
                  {
                        destination << "/n";
                  }
    };
    
    void fill ()
    {
         int primes=0;
         int a;
         a = (int) group;
         bool sector[a];
         for (int i=0;cnum<=(cursec*(group+1));++i)
         {
             if (checkPrime(cnum))
             {
                     ++primes;        
                     sector[i]=true;
             }
         }
         destination << primes;
    }
             
             
    
    int main ()
    {
        for (;cnum<=fin;)
        {
           sector asect;
           fill();
        }
    }
    What I want the program to do is write a file that shows how many prime numbers are in a given size of numbers (100) over a large amount of numbers (100000).
    I can compile the program but when I run it it crashes.
    There is no real purpose to tis program other than being a learning exercise and helping me understand why this doesn't work would be much appreciated (along with tips on my coding style).

  2. #2
    Join Date
    Apr 2008
    Posts
    133

    Re: Simple program crashes

    for a start..your function fill(..) will go into an infinite loop since you dont increment cnum inside your for loop.

  3. #3
    Join Date
    May 2008
    Location
    Montreal, Quebec, Canada
    Posts
    49

    Re: Simple program crashes

    Thanks. I made a few changes and the only thing is that the program does an infinite loop in the checkPrime function.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    unsigned long cnum=1;
    const unsigned long fin = 100;
    const unsigned long group = 10;
    unsigned long cursec=0;
    
    
    ofstream destination("results.txt");
    
    
    
    bool checkPrime ()
    {
          for (int n=2;n<cnum;++n)
          {
                cout << "In checkPrime \n";
                if ((cnum % n) == 0)
                       return false;
          }
                return true;
    }
    
    
    class sector
    {
          public:
          sector ()
                 {
                       cout << "sector instance created \n";
                       destination << (cursec*group)+1 << " - " << (cursec+1)*group << " ";
                       ++cursec;
                 }
    
          ~sector ()
                  {
                        cout << "sector instance destroyed \n";
                        destination << "\n";
                  }
    };
    
    void fill ()
    {
         cout << "fill entered \n";
         int primes=0;
         int a;
         a = (int) group;
         bool sector[a];
         for (int i=0;cnum<=(cursec*(group+1));++i)
         {
             if (checkPrime())
             {
                     cout << "(In fill)  checkPrime was true, now changing numbers";
                     ++primes;
                     ++cnum;        
                     sector[i]=true;
                     system("pause");
             }
         }
         destination << primes;
    }
             
             
    
    int main ()
    {
        for (;cnum<=fin;)
        {
           cout << "first for loop entered\n";
           sector asect;
           fill();
        }
    }
    All of the "cout"s are there so that I know where the program is. I'll take them out when I get the program to work.

  4. #4
    Join Date
    May 2008
    Location
    Montreal, Quebec, Canada
    Posts
    49

    Re: Simple program crashes

    Nevermind, I got it.
    the
    ++cnum
    was in the wrong place.

    thanks again.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Simple program crashes

    You should learn how *not* to use global variables. They make debugging much harder.

  6. #6
    Join Date
    May 2008
    Location
    Montreal, Quebec, Canada
    Posts
    49

    Re: Simple program crashes

    i figured that.
    how about functions though, should I keep them in classes or make them global (if that is what a function witout a class is called).

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Simple program crashes

    Well, that depends on the function. If a group of functions operate on a common set of data, and logically operations on that data should only occur through those functions, then you have a good candidate for a class. The question to ask is, "does it make sense to think of this data as an object?"

    If the answer is no, or if there is no consistent bit of data, but the functions are still related, consider using a namespace instead. Or just make them global if you wish, there's no harm in it.

  8. #8
    Join Date
    May 2008
    Location
    Montreal, Quebec, Canada
    Posts
    49

    Re: Simple program crashes

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured