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:
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!
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!
Comment