Click here to Skip to main content
15,893,790 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
e.g. if N=9 prime numbers upto N are 2,3,5,7

Can someone help me identify what is wrong with my code

What I have tried:

C++
#include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
      short N,pc=0,num=2;
      bool prime;
      while(pc!=N)
      {
       prime=true;
        for(short i=2;i<=sqrt(N);i++)
        {
           if(N%i==0)
           {
               prime=false;
               break;
           }
        }
     }
      if(prime)
       {
          pc++;
          cout<<num;
       }
      return 0;}
Posted
Updated 12-Aug-18 7:45am
v2

First problem:
Quote:
Create a program to print prime number upto N

What is the value of N in this code ?

Second problem:
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
  short N,pc=0,num=2;
  bool prime;
  while(pc!=N)
  {
    prime=true;
    for(short i=2;i<=sqrt(N);i++)
    {
      if(N%i==0)
      {
        prime=false;
        break;
      }
    }
  }  // this line is misplaced
  if(prime)
  {
    pc++;
    cout<<num;
  }
  // and should be here
  return 0;
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

Third problem:
Your code is always checking if N is prime or not. It is wrong.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
You have not set variable N to any value.
C++
short N,pc=0,num=2;
bool prime;
while(pc!=N) // what is the value of N?
{
 
Share this answer
 
You should do same basic input/output in your code to get N.

And the inequality check is a very bad idea because than N has to be equal to pc, else it runs forever. I would call it a design flaw to not call it a severe bug.

You better check with the less operator, but your code is unclear for now.
 
Share this answer
 
Comments
Patrice T 12-Aug-18 15:31pm    
See comments I put in code in S2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900