hello everyone, this is my first post. I have been teaching myself c++ for the last few weeks and have gotten more familiar with some of it.
i thought to myself it would be interesting to try writing up some code that goes through numbers to see if they are prime or not, and if the number is prime, it outputs it on the screen.

the way i coded it was like this... and i feel like it is VERY close to being right, but whenever i run it, it just outputs EVERY number, not just the prime numbers...

can anyone let me know what i need to change in this code to make sure that it ONLY outputs the primenumbers, and so that it continues the loop afterwards so that it keeps looking for prime numbers up to the max value of an unsigned integer...

here is the code

#include <iostream>
#include <string>

using namespace std;

int main()
{
    unsigned int isitprime = 3;
    unsigned int basenumber = 2;
    unsigned int placekeeper = 2;
    
    float askit = (float)isitprime/(float)basenumber;
    
    do{
          
          if(askit==placekeeper)
          {
                                placekeeper = 2;
                                basenumber = 2;
                                ++isitprime;
          }
          if(askit!=placekeeper)
          {
                                do
                                {
                                            ++placekeeper;
                                }
                                while(placekeeper<isitprime);
                                
                                if(placekeeper<=isitprime)
                                {
                                                          placekeeper = 2;
                                                          ++basenumber;
                                }
                                if(basenumber==isitprime)
                                {
                                                         cout << isitprime << ", ";
                                                         ++isitprime;
                                                         basenumber = 2;
                                                         placekeeper = 2;
                                }
          }
    }
    while (isitprime<=222222222);
}

now that i look at the code, i'm thinking i could probably take out the #include <string> part.... but thats irrelevant. what is wrong with this code? i haven't been able to figure it out yet.

hello everyone, this is my first post. I have been teaching myself c++ for the last few weeks and have gotten more familiar with some of it.
i thought to myself it would be interesting to try writing up some code that goes through numbers to see if they are prime or not, and if the number is prime, it outputs it on the screen.

the way i coded it was like this... and i feel like it is VERY close to being right, but whenever i run it, it just outputs EVERY number, not just the prime numbers...

can anyone let me know what i need to change in this code to make sure that it ONLY outputs the primenumbers, and so that it continues the loop afterwards so that it keeps looking for prime numbers up to the max value of an unsigned integer...

here is the code

#include <iostream>
#include <string>

using namespace std;

int main()
{
    unsigned int isitprime = 3;
    unsigned int basenumber = 2;
    unsigned int placekeeper = 2;
    
    float askit = (float)isitprime/(float)basenumber;
    
    do{
          
          if(askit==placekeeper)
          {
                                placekeeper = 2;
                                basenumber = 2;
                                ++isitprime;
          }
          if(askit!=placekeeper)
          {
                                do
                                {
                                            ++placekeeper;
                                }
                                while(placekeeper<isitprime);
                                
                                if(placekeeper<=isitprime)
                                {
                                                          placekeeper = 2;
                                                          ++basenumber;
                                }
                                if(basenumber==isitprime)
                                {
                                                         cout << isitprime << ", ";
                                                         ++isitprime;
                                                         basenumber = 2;
                                                         placekeeper = 2;
                                }
          }
    }
    while (isitprime<=222222222);
}

now that i look at the code, i'm thinking i could probably take out the #include <string> part.... but thats irrelevant. what is wrong with this code? i haven't been able to figure it out yet.

I don't think I understand what you're trying to do and what each variable represents, but one thing that may be asking for trouble is this line:

if(askit==placekeeper)

You're comparing a float and an unsigned into using == and the float was derived by typecasting two ints to two floats and dividing. That has the potential for round-off error, so generally you try not to compare floats using ==.

You may need to explain the algorithm. It may be obvious to others, but I don't understand the logic.

I think you need to re think your design.

Use for loops instead. Have a function that checks is a number is
prime. Is it returns true then print that number.

for(int i = 0; i < MAX; i++)
    if( isPrime(i) ) cout << i <<" is prime : ";

Here is something:

uses
    crt;
var
   i, ii, control, divcount, count : integer;
begin
     clrscr;
     readln(control);

     for i := 1 to 30000 do
         begin

              for ii := 1 to i do
                  begin
                       if(count=control)then
                       begin
                               readln;
                               exit;
                       end;
                       if (i mod ii = 0) then
                          divcount := divcount + 1;
                  end;
              if divcount = 2 then
                 begin
                      writeln(i);


                            count := count + 1;

                      divcount := 0;
                 end
              else
                  divcount := 0;
         end;

     readln;
end.

Its not in C++, but you can do that...

Google seive prime. Its easy to implement and its fast.
Here is the basic idea.

0) Fill an array with i = 0 to i < MAX
1) Start with number 2. Any multiple of 2 is not a prime so set all numbers multiple of 2 to 0 in the array.
2) Look at the next number non-zero number. Which should be 3.
any multiple of 3 is not a prime, so set it to 0.
3) Look at the next non 0 number, which will be 5. Any multiple of
5 is not a prime, so set any multiple of it to 0 in the array.

4) repeat until i < MAX is false;

This article has been dead for over six months. Start a new discussion instead.