-2

recently I took it upon myself to start working on the Project Euler challenges. I am on problem seven, and have encountered a strange error in my code. In my code, I have a variable called, count that counts the number of prime numbers found, but when I try to print it, it prints out as null.

#include <iostream>

int main()
{
  bool isPrime = true;

  for(int i = 1; i <= 10000000000;)
  {
      int count;
      for(int factor = 1; factor = i; factor++)
      {

          if(i%factor == 0)
          {
              isPrime = false;
              break;
          }
      }

      if(isPrime) {count = count + 1;}

      std::cout << count + "-";
      if(count == 10001)
      {
          std::cout << count;
          std::cout << i + "Final \n";
          break;
      }
      i++;
  }


}

When the line std::cout << count + "-"; runs, the output is:

--------------------------------------------------------------------------------------------------------

etc. I think it has something to do with the initialization of counts, but I'm not sure. Thanks for any help!

James Kirk
  • 11
  • 3
  • 2
    You either need a better compiler / need to turn up your warning setting / or actually pay attention to the warnings. Your code throws 5 different warnings for incorrect code: http://coliru.stacked-crooked.com/a/6ef7a57438ba8b8c – NathanOliver Sep 28 '17 at 16:29
  • 1
    "_Initialized Variable couts as Nothing_" But.. The variable `count` is **not** initialized.. Do you know what initialization is? – Algirdas Preidžius Sep 28 '17 at 16:30
  • @NathanOliver Thanks for letting me know, this is my first time using g++, as I just installed it 4.5 hours ago. Good to know. – James Kirk Sep 28 '17 at 16:48
  • A good starting place for g++'s warnings is to add -Wall -Wextra to the command line. I also add -pedantic. If you want to make it so that you cannot ignore the warnings, a good choice while learning, add -Werror to turn warnings into errors. – user4581301 Sep 28 '17 at 16:55

2 Answers2

3

In addition to the errors mentioned above, in the line of code

for(int factor = 1; factor = i; factor++)

You are assigning factor to have a value of I rather than checking an equality like this:

factor == i;

I assume this is what you are trying to do.

mreff555
  • 1,049
  • 1
  • 11
  • 21
  • This should be a comment (-1), but you can't comment yet and this is really useful information for the asker (+1). Comes out even. – user4581301 Sep 28 '17 at 17:00
  • Now that I can comment, let me defend myself by saying that it would have been a comment if I had access to do so at the time. – mreff555 Sep 29 '17 at 19:23
1

Use

std::cout << count << "-";

By using the "+" operator you're concatenating a string with nothing, thus outputting just the "-" string.

Alternatively you can do

std::cout << std::to_string(count) + "-";
Scott Mudge
  • 957
  • 6
  • 18
  • Oh, that's interesting. I have done a lot of Java, but not much C++. Thanks for the help! – James Kirk Sep 28 '17 at 16:31
  • I wouldn't suggest your alternative suggestions. It doesn't gain you anything and is expensive. – NathanOliver Sep 28 '17 at 16:31
  • @JamesKirk Why would you assume the semantics of Java, and C++ would be the same? I suggest reading a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Sep 28 '17 at 16:33