2

Even at the bare minimum of 10 numbers to input, I get no errors but my code crashes immediately on running. I was also wondering, what should I do if I have a question similar to another question that I've already asked, but on another new problem?

#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int primer(int max);
int main()
{
    primer(5);
    system("pause");
    return 0;
}

int primer(int max){
    vector<int> a;
    a[1]=2;
    for (int i=2;i<=max;i++){
      bool prime=true;
      for (int ii=0;ii<a.size();ii++) {
        if (i/a[ii]==floor(i/a[ii])) {
        prime=false;
        }
      }
      if (prime==true) {
        a.push_back(i);
        }
    }
    for (int iii=0;iii<=a.size();iii++) {
    cout << a[iii] << endl;
    }
}

I get no errors but the compiled code crashes immediately. I changed it to

#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int primer(int max);
int main()
{
    primer(5);
    system("pause");
    return 0;
}

int primer(int max){
    vector<int> a;
    a.push_back(2);
    for (double i=2;i<=max;i++){
      bool prime=true;
      for (int ii=0;ii<a.size();ii++) {
        if (i/a[ii]==floor(i/a[ii])) {
          prime=false;
        }
      }
      if (prime) {
        a.push_back(i);
        }
      }
    for (int iii=0;iii<=a.size();iii++) {
      cout << a[iii] << endl;
      return a.size();
    }
}

I addressed all of your problems. It still returns no errors and still crashes.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
TimeCoder
  • 1,379
  • 3
  • 12
  • 13
  • You need to post the error that you get. Also you need to not use variables like `i`, `ii`, and `iii`, especially all in the same code. – Falmarri Jan 19 '11 at 23:16
  • 1
    Did you try to debug it at all? – Oliver Charlesworth Jan 19 '11 at 23:17
  • The second independent clause of the first sentence pointed out that I have no errors. I just edited and repeated it at the end to reinforce it. – TimeCoder Jan 19 '11 at 23:18
  • 1
    You get no compiler errors? **Really???** – John Dibling Jan 19 '11 at 23:18
  • 1
    If you are running into stumbling blocks, one after another like this. You need to read a book. http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Benjamin Lindley Jan 19 '11 at 23:20
  • 4
    @TimeCoder: Please take this as constructive criticism, as it is intended. The problem is not that you're posting many, related posts. That's fine. The problem is that it appears that you don't have even an elementary grasp of C++, and you're trying to learn it from the ground up by posting on SO. So isn't "Learn to Program C++ in 21 Minutes" We expect you to know the language. At least the basics. – John Dibling Jan 19 '11 at 23:21
  • @John Yes, really. I know what I thought were the basics of C++. How to recieve input, read and write files, use string, char, int, and float variables, how to use functions, make for and while loops, and that obviously seems not to be enough. @Oli, it's not exactly a bug, but yes, I attempted to try to fix it. – TimeCoder Jan 19 '11 at 23:21
  • @TimeCoder: Impossible. Look again. `primer` is declared to return an `int`, but doesn't. – John Dibling Jan 19 '11 at 23:22
  • @John. sometimes without using `-wall` etc. g++ allows that. I had a similar problem once and was hitting my head in the wall as to what was happening –  Jan 19 '11 at 23:23
  • @Muggen: Interesting. How can that possibly be a feature, and not a bug?! – John Dibling Jan 19 '11 at 23:25
  • @John, I guess its a feature only newcomers can see cause most of the professionals use `-wall -pedantic` etc. Actually I just tried it with a simple compile `g++ -o file file.cpp` and it didn't give a warning (aka successful compilation). gcc 4.4.4/x86_64-slackware-linux –  Jan 19 '11 at 23:29
  • 1
    @TimeCoder "I addressed all of your problems." - No, you didn't. – moinudin Jan 19 '11 at 23:31
  • @marcog, I edited near the time that you posted. Thank you for mentioning that boolean mistake. – TimeCoder Jan 19 '11 at 23:33
  • 1
    @TimeCoder Doesn't crash for me. See [here](http://ideone.com/TzGzO). You're still doing things wrong though, but let's first get it to compile for you. – moinudin Jan 19 '11 at 23:38

3 Answers3

5

What makes you think you can do this?

vector<int> a;
a[1]=2;
Falmarri
  • 47,727
  • 41
  • 151
  • 191
  • 3
    WTF? We just addressed that in his previous question from a few minutes ago! http://stackoverflow.com/questions/4741629/c-vector-elements-count/4741642#4741642 – chrisaycock Jan 19 '11 at 23:21
1
vector<int> a;
a[1]=2;

You can't access a[1] until you've reserved space for it. You should probably use a.push_back(2) to append 2 to the end of a.

You have declared primer to return int, yet it returns nothing. Either make it void or return the number of primes.

i/a[ii]==floor(i/a[ii]) isn't going to do what you expect. i/a[ii] performs integer division. You should cast i to double before dividing.

if (prime==true) can be changed to simply if (prime), no need to compare a boolean to true.

Please improve your coding style. Use proper indentation and more commonly used variable names: i, j, k instead of i, ii, iii.

moinudin
  • 134,091
  • 45
  • 190
  • 216
1

Here is another bug:

for (int iii=0;iii<=a.size();iii++) {
  cout << a[iii] << endl;
  return a.size();
}

My understanding is that you can only return once from a function, main included. The execution will not loop here because of the return statement.

Did you really want a return statement inside a for loop?

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154