newbie woes: 21 line C++ code crashes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Caffiend
    New Member
    • Jan 2007
    • 21

    newbie woes: 21 line C++ code crashes

    Hi there! I'm brand new to programming, save for some adventuring with BASIC 10 years ago. I have been learning from "C++ Without Fear" by Brian Overland, and it is really well written, but I still have dificulty grasping many concepts. I am trying to write code that will take an input number, then print all the prime numbers between 1 and that number. At this point, the code compiles in Dev C++ without problem, but after inputting the number it crashes. Here's my code:
    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int knum; // Highes number
    int i = 0; // loop counter
    
    int main() {
        cout << endl;
        cout << "Please enter a number: ";
        cin >> knum; // this is the last time I see something happen
    
        while (i <= sqrt(static_cast<double>(knum))) {
              if (knum % i != 0) {
                       cout << i << " ";
                       }
              i++;
                       }
        
    int getch();
    }

    Can anyone help me figure out what I am doing wrong? I will admit I am almost completely ignorant with respect to C++, so please forgive me if my code makes no sense whatever!

    BTW, I look forward to getting to know everyone here and increasing once more the proportion of my time spent on the couch staring blankly at the LCD!
    Last edited by horace1; Jan 27 '07, 09:50 AM. Reason: added code tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    the problem is probably in
    Code:
    if (if (knum % i != 0) {
    the modulus operator % in the expression knum % i gives the remainder of knum / i and as i has been initialised to 0 so you get a division by 0

    what you trying to caculate?

    Comment

    • Caffiend
      New Member
      • Jan 2007
      • 21

      #3
      I'm trying to calculate all the prime numbers between 1 and knum.... BTW, thanks for point out initializing i = 0, i changed it to i = 1, and it doesn't crash, which is excellent, but now when I entered 100 as knum, it prints "3 6 7 8 9" and then closes... I'm probably trying to do this completely the wrong way...!

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by Caffiend
        I'm trying to calculate all the prime numbers between 1 and knum.... BTW, thanks for point out initializing i = 0, i changed it to i = 1, and it doesn't crash, which is excellent, but now when I entered 100 as knum, it prints "3 6 7 8 9" and then closes... I'm probably trying to do this completely the wrong way...!
        you should start divsions (i) at 2 and test knum % i for 0, i.e. if the remanider is 0 the number is not a prime, e.g.
        Code:
        #include <iostream>
        #include <math.h>
        using namespace std;
        
        int knum; // Highes number
        int i = 2; // loop counter
        
        int main() {
        cout << endl;
        cout << "Please enter a number: ";
        cin >> knum; // this is the last time I see something happen
        
        while (i < knum) {
              // should only be divisable by inself and 1
              if (knum % i == 0) {
                  cout << "divisable by " << i << " not prime ";
                  getchar(); getchar();
                  return -1;
                 }
              i++;
        }
        cout << i << " prime ";
        getchar(); getchar();
        }

        Comment

        • Caffiend
          New Member
          • Jan 2007
          • 21

          #5
          Thanks Horace, I'm getting closer... what I'm trying to do specifically is not just to determine if a number is prime or not, but to actually print onscreen all the prime numbers between 1 and knum, so if knum is 10, then I want to print onscreen "1 3 5 7"... Here is my revised code:

          #include <iostream>
          #include <math.h>
          using namespace std;

          int knum;
          int i = 2;

          int main() {
          cout << endl;
          cout << "Please enter a number: ";
          cin >> knum;

          while (i < knum) {
          if (knum % i == 0) {
          i++;
          }
          else {
          cout << i << ", ";
          i++;
          }
          }
          getchar(); getchar();
          }

          But now I am getting a compiler error: "Permission denied Id returned 1 exit status" I have no idea what that means, other than it doesn't work....

          Comment

          • Caffiend
            New Member
            • Jan 2007
            • 21

            #6
            Silly me, that error was from having the file running while trying to compile, but now I am getting a display of numbers, mostly sequential, but every so often it skips one. Most of the numbers are certainly NOT prime... where have I gone wrong?

            Comment

            Working...