-1

When i run this program (i am using codeblock and its fully upgraded), it shows a box with: ''''.exe has stopped working A problem caused the program to stop working correctly. Windows will close the program and notify if a solution is available.''''

#include <iostream>

#include <math.h>

#include <conio.h>

using namespace std;

int main()
{

    int no, hlf, arr[no], arrno;

    cout << "ENTER A NUMBER";

    cin >> no;

    hlf = ceil(no/2);


    for(int i = 1;i <= no;i++)
    {
        for(int j = 2;j <= hlf;j++)
        {
            int ij = i/j;
            if(j != i && ij == 0)
            {
                goto cont;
            }
            else
            {
                continue;
            }
        }
        arr[arrno] = i;
        arrno++;
        cont: ;
    }

    for(int k = 0;k <= arrno;k++)
    {
        cout << arr[k] << "  ";
    }

    getch();
    return 0;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Sri
  • 53
  • 5
  • 8
    That means your program *crashed*. You should use a *debugger* to find out where and help you figure out why. Perhaps you should also read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert. – Some programmer dude Jul 29 '17 at 15:38
  • 1
    I suggest that you write down all the variables and their values and then walk the code updating the values as they change. you will discover problems with your code quickly. – john elemans Jul 29 '17 at 15:39
  • 2
    As a hint though: In the absence of loops, execution of your programs goes from top to bottom. Your program will not go back and do things like redefining variable retroactively. Think about that means when you use `no` before it is initialized. – Some programmer dude Jul 29 '17 at 15:39
  • ***Furthermore*** uninitialized local variables are truly *uninitialized*. Their values will be *indeterminate*. Now think about that when you use e.g. `arrno` before it's initialized. Using uninitialized variables in C++ actually leads to [*undefined behavior*](http://en.cppreference.com/w/cpp/language/ub)! Also, you seem to have forgotten that array-indexes start from *zero*. – Some programmer dude Jul 29 '17 at 15:47
  • 1
    Lastly, C++ doesn't have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array). Altogether it seems that you need a couple of [good beginners book to read](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Jul 29 '17 at 15:49
  • *When i run this program* -- `int no, arr[no];` -- This is not legal C++. Arrays in C++ cannot have arrays declared using a variable to denote the number of entries. As it stands now, the program won't compile with a compiler that adheres to this rule (Visual C++, for example). Even if you get the rest of your code to work, drop `arr[n]` and learn the proper ways to declare dynamic arrays in C++ (using `std::vector`). – PaulMcKenzie Jul 29 '17 at 16:02
  • You could replace the `goto` with a `break`. – Thomas Matthews Jul 29 '17 at 16:17

2 Answers2

0

There are few mistakes in your code

  1. no need of #include <conio.h> and getch();

  2. Array arr[no] declaration is wrong. It should be int arr[50];

Here is the corrected code that runs fine.

#include <iostream>
#include <math.h>

using namespace std;

int main()
{

    int no, hlf, arrno;
    int arr[50];


    cout << "ENTER A NUMBER";

    cin >> no;

    hlf = ceil(no/2);



    for(int i = 1;i <= no;i++)
    {
        for(int j = 2;j <= hlf;j++)
        {
            int ij = i/j;
            if(j != i && ij == 0)
            {
                goto cont;
            }
            else
            {
                continue;
            }
        }
        arr[arrno] = i;
        arrno++;
        cont: ;
    }

    for(int k = 0;k <= arrno;k++)
    {
        cout << arr[k] << "  ";
    }


    return 0;
}

Here is the Screenshot, program runs just fine but i have not check your logic

0

thanks guys, i got the answer. it was my bad, i didn't post that i need to print prime numbers. its my first question in a web forum. never used one. ps -> thanks again

include

include

using namespace std;

int main() {

int numb = 12, half;

int arra[50], arrno = 0;

half = ceil(numb/2);

for(int r = 2;r <= numb;r++)
{
    for(int t = 2;t <= half;t++)
    {
        if(r%t != 0 || t == r) continue;
        else goto rpp;
    }
    arra[arrno] = r;
    arrno++;
    continue;
    rpp:
        continue;
}

for (int v = 0;v < arrno;v++)
{
    cout << arra[v] << "  ";
}
return 0;

}

Sri
  • 53
  • 5