Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,120 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,786 people online right now. Registration is fast and FREE... Join Now!




Prime Number Finder Errors!

 
Reply to this topicStart new topic

Prime Number Finder Errors!

pngrafxx126
10 Apr, 2007 - 05:42 PM
Post #1

New D.I.C Head
*

Joined: 7 Nov, 2006
Posts: 33


My Contributions
Hello all,

I started this program where 2 arrays are made an array for the prime number list and an array for the non-prime numbers. Thing is, after the user inputs through the cin, the program terminates and I don't know why. Someone please help me.

CODE

#include <cstdio>
#include <cstdlib>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{

    int N, elim, i;
    int x, y = 0;
    long *primArray;
    long *elimArray;

    top:            // Questions
        cout << "Welcome to Programming Assignment 5! The final instalment for the Spring 2007 semester" << endl;
        cout << "Input the range you would like to find the prime integers with: " << endl;
        cin >> N;      // N is the ending integer to find the prime numbers
        
        primArray = new long[ N ];        // declares the primArray array.

        if (N < 1)
        {
            cout << "Error! Cannot be less than 1!" << endl;
            goto top;
        }
        else
        {
            goto primeCheck;
        }

    primeCheck:
        for (i = 2; i < N; i++)
        {
            for (int a = 0; a < N; a++)
            {
                if ( i == elimArray[1, a] )
                {
                    goto primeCheck;
                }
                else
                {
                    primArray[1,x] = i;
                    x++;
                }
            }
        goto elimCheck;
        }

    elimCheck:
        for (int j = i; j < N; j++)
        {
            elim = i * j;
            elimArray[1,y] = elim;
            y++;

            if (elim < N)
            {
                j = N + 1;
            }            
            goto primeCheck;    
        }
        
    delete [] primArray;
    delete [] elimArray;
    
    return 0;
    
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Prime Number Finder Errors!
10 Apr, 2007 - 06:26 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Does it give any error messages on termination? Also, I'll have to caution strongly against the use of the goto statement and labels...
User is offlineProfile CardPM
+Quote Post

pngrafxx126
RE: Prime Number Finder Errors!
10 Apr, 2007 - 06:40 PM
Post #3

New D.I.C Head
*

Joined: 7 Nov, 2006
Posts: 33


My Contributions
it just tells me that the program has to terminate and you have to click ok. then it goes back to the program screen. whats wrong with goto's? what do u suggest
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Prime Number Finder Errors!
10 Apr, 2007 - 06:45 PM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
There are several pitfalls of using the goto statement - one of the largest of which is the fact that it leads to unstructured code. I would suggest using functions and properly structured loops.

Here is a quick look:

http://en.wikipedia.org/wiki/GOTO

User is offlineProfile CardPM
+Quote Post

pngrafxx126
RE: Prime Number Finder Errors!
10 Apr, 2007 - 06:50 PM
Post #5

New D.I.C Head
*

Joined: 7 Nov, 2006
Posts: 33


My Contributions
ic. as u can see im not much of a coder. im going to need a hint on exactly wut u mean by properly structured loops what do u suggest i should replace the primeCheck and elimCheck labels with?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Prime Number Finder Errors!
10 Apr, 2007 - 10:57 PM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
well as far as I can tell your program does SOMETHING.... problem is, you don't let it interact with the user enough to tell what it does. try adding a few debugging print statments here and there to see what your program is doing (or load it into a debugger and trace it step by step... but I think a few cout line will work here).

You really should try to avoid the goto statment.

lets look at a bit of your code:

CODE


//Here is the lable that you use with your goto...
top:            // Questions
        cout << "Welcome to Programming Assignment 5! The final instalment for the Spring 2007 semester" << endl;
        cout << "Input the range you would like to find the prime integers with: " << endl;
        cin >> N;      // N is the ending integer to find the prime numbers
        
        primArray = new long[ N ];        // declares the primArray array.

        if (N < 1)
        {
            cout << "Error! Cannot be less than 1!" << endl;
            goto top;
/* MEMORY LEAK!!!
*if the user enters N<1 then a new array will be made,
*and the old one is not removed...  */

        }
        else
        {
            goto primeCheck;
        }


lets code that using safer practices:
CODE

top:
do
{
        cout << "Welcome to Programming Assignment 5! The final instalment for the Spring 2007 semester" << endl;
        cout << "Input the range you would like to find the prime integers with: " << endl;
        cin >> N;      // N is the ending integer to find the prime numbers
} while (N<1); //loop until the user gets it right.
primArray = new long[ N ];        // declares the primArray array.
Now the array is only declared once, no matter how thick headed the user (though if the user chooses TOO large and array there might be trouble).
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:59PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month