473,378 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. int knum; // Highes number
  6. int i = 0; // loop counter
  7.  
  8. int main() {
  9.     cout << endl;
  10.     cout << "Please enter a number: ";
  11.     cin >> knum; // this is the last time I see something happen
  12.  
  13.     while (i <= sqrt(static_cast<double>(knum))) {
  14.           if (knum % i != 0) {
  15.                    cout << i << " ";
  16.                    }
  17.           i++;
  18.                    }
  19.  
  20. int getch();
  21.  

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!
Jan 27 '07 #1
5 1538
horace1
1,510 Expert 1GB
the problem is probably in
Expand|Select|Wrap|Line Numbers
  1. if (if (knum % i != 0) {
  2.  
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?
Jan 27 '07 #2
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...!
Jan 27 '07 #3
horace1
1,510 Expert 1GB
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.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. int knum; // Highes number
  6. int i = 2; // loop counter
  7.  
  8. int main() {
  9. cout << endl;
  10. cout << "Please enter a number: ";
  11. cin >> knum; // this is the last time I see something happen
  12.  
  13. while (i < knum) {
  14.       // should only be divisable by inself and 1
  15.       if (knum % i == 0) {
  16.           cout << "divisable by " << i << " not prime ";
  17.           getchar(); getchar();
  18.           return -1;
  19.          }
  20.       i++;
  21. }
  22. cout << i << " prime ";
  23. getchar(); getchar();
  24.  
Jan 27 '07 #4
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....
Jan 27 '07 #5
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?
Jan 28 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: jeff elkins | last post by:
Howdy, This may not belong here, if so apologies... I'm a python newbie, but have completed a console app that I'd like to run under X. Reading recent postings here, wxpython seemed a...
4
by: Jeremy | last post by:
I have this javascript code that decided that it didn't want to work anymore. At one point in time it did work. I don't remember changing anything that would have caused it to stop working. But...
32
by: Protoman | last post by:
I have a function that calculates the mean of the some numbers; I need it to accept any number of parameters (one needs to be the number of the other parameters) Can you help me out here? Here's...
12
by: Herby | last post by:
Iv compiled my current C++ project as \clr as i want to start putting in some specific C++\CLI code to serialize my MFC objects to .NET equivalents. The program crashes on startup, something to do...
3
by: kathleen | last post by:
Hi there, I'm new to this forum & to programming in general, and am really stuck with a piece of buggy code. Can anyone help? I have several problems which may or may not be related. I have...
9
by: Mark Rae | last post by:
Hi, This time, I'm looking for a regular expression which says "the string must contain exactly seven or exactly eight digits" e.g. 123456 fails 1234567 passes 12345678 passes 123456789...
3
MonolithTMA
by: MonolithTMA | last post by:
Greetings all, I am new here, and aside from my introductory post, this will be my first. I am working on an Inventory program for a class I am taking. I've searched the forums here and have...
4
by: =?Utf-8?B?VkIgSm9ubmll?= | last post by:
I am at my witless end here, please help! I have an ASP.Net aspx web page, hosted on Windows Server 2003, that receives a query string with the path to an autocad drawing file selected from a...
9
by: derker | last post by:
hi, i am going crazy trying to figure this out. it seems like it should be real easy and probably is, but im kind of a nube flying by night so any thoughts or suggestions would be greatly...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.