I made a prime number finder, it finds the nearest prime that is above and below any integer (Does not work that well with giant numbers.)
Made this in cpp.sh
Why would I need the x=y+1-1;?
x=y; just doesnt seem to work
Why is this?
And I know my coding is far from clean or correct.

``````#include <iostream>
#include <string>
using namespace std;
int main(){

cout << "Prime nubers: " << endl;
int x=0, y=0, z=0, w=0;
cin >> x;
y = x-1+1; // for some reason this doesnt work without the +1-1
while (z!=1){
if (((x/2*2 != x) && (x/3*3 != x) && (x/5*5 != x) && (x/7*7 != x) && (x/13*13 != x)) || ((x == 7) || (x == 13) || (x == 3))){

cout << x << " is the nearest prime number!" << endl;
z=1;
}
x++;
}

while (w!=1){
if (((y/2*2 != y) && (y/3*3 != y) && (y/5*5 != y) && (y/7*7 != y) && (y/13*13 != y)) || ((y == 7) || (y == 13) || (y == 3))){
cout << y << " is the nearest prime number!" << endl;
w=1;
}
y--;
}
cout << " " << endl;
cout << " " << endl;
cout << " end of program" << endl;

return 0;
}``````

## All 14 Replies

Changing line 9 to `y = x` should make no difference. The only POSSIBLE difference I can think of MIGHT be if you entered a value that is the negative limit of an integer. Subtracting 1 would take you out the range of an integer, then adding 1 might not return you to the original value. But you're talking about a number less than negative two billion on a 32-bit system, so I doubt that's it. And I simply don't remember what happens when you subtract 1 from the minimum integer value, then add it back again. Could be a problem, could not be a problem. That was a question on the midterm of my first semester C++ class and I haven't revisited it since.

But that's clearly not YOUR problem here since checking for negative prime numbers doesn't make any sense in the first place, plus as you say, this program wasn't intended for extremely large (or extremely small) values.

So my guess is that you are mistaken that your program is giving different results when you change that line. What value are you entering and what are the results of the two programs? Like I said, they should be the same, and that's without looking at the rest of your code, just that line. Perhaps the code is not actually being re-compiled or you are not running the re-compiled code? Do a full clean and build of the code with each version of line 9. Make sure you're running the updated executable with the same input.

Now to the broader issue of testing code in general. The below points all boil down to the need for attention to detail, picking good test cases, and minimizing the chance of mistakes...

1. Attention to detail. The line is`y=x-1+1`, not `y=x+1-1`, not `x=y+1-1`. Get in the habit of quoting ideas and code as precisely and accurately as possible. This will put you in the right frame of mind. Coding is precise. Make your English precise as well.
2. Your formatting needs to be consistent. No one can read the coode you posted, including you. See below for easier to read code.
3. You neglect to mention what value you entered for input and the corresponding output for both versions of line 9, how they are different, and which one was incorrect. We cannot reproduce your output without knowing the input.

The formatted code below was produced by pasting your code into http://format.krzaq.cc/ It's one of many free online code "beautifiers" out there. Google "C++ online code formatter". Which code is easier to read? Yours or the code below? It's quick, free, painless, easy, and cuts down on avoidable mistakes. I try not to speak for others, but I'm guessing that the first thing that caught everyone's eye was the bad formatting. No one will try to debug the code above. They will either ignore it and you or they'll copy/paste it into a beautifier as I did and wonder why you did not do so in the first place. Don't take this the wrong way. It's not a criticism. Everyone STARTS by posting as you did, not knowing about codee beautifiers and the importance of formatting. Now you know. I guarantee you your programming life will become much easier now that you know about code beautifiers.

``````#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Prime nubers: " << endl;
int x = 0, y = 0, z = 0, w = 0;
cin >> x;
y = x - 1 + 1; // for some reason this doesnt work without the +1-1
while (z != 1)
{
if (((x / 2 * 2 != x) && (x / 3 * 3 != x) && (x / 5 * 5 != x) && (x / 7 * 7 != x)
&& (x / 13 * 13 != x))
|| ((x == 7) || (x == 13) || (x == 3)))
{
cout << x << " is the nearest prime number!" << endl;
z = 1;
}
x++;
}

while (w != 1)
{
if (((y / 2 * 2 != y) && (y / 3 * 3 != y) && (y / 5 * 5 != y) && (y / 7 * 7 != y)
&& (y / 13 * 13 != y))
|| ((y == 7) || (y == 13) || (y == 3)))
{
cout << y << " is the nearest prime number!" << endl;
w = 1;
}
y--;
}
cout << " " << endl;
cout << " " << endl;
cout << " end of program" << endl;

return 0;
}``````

So to sum up, try your programs again. I doubt changing line 9 makes a difference. I can't imagine why it would.

Thank you very much, I just started to learn c++ and people like you have have helped me throughout the process.

I can't tell you how much this helped!

Your coding habit not to check presedence is bad buddy.
On line 9, its better to group like `y = x - 1 + 1;` AS `y = x - (1+1);`
Though it does not change much but line 12 and 24 is totally wrong way to go. You may be a new dev but this simple stuff should be known.

You must also check with division by zero. try catch to handle things.

commented: Possibly the worst post of the year so far -3
1. `y = x - (1+1)` is NOT the same as `y = x - 1 + 1` You should learn about operator precedence and L-R execution before criticising
2. Where exactly do you suppose OP should check for division by zero? That code does not have a single example of division with a variable as its divisor. They are all non-zero constants.
3. "totally wrong way to go" without suggesting a better way is unhelpful at best.

Peter_36 : I think you can safely ignore Richard-36's nonsense.

Wouldn't most compilers optimize `y = x-1+1;` to `y = x;` anyway?

JamesCherrill.

1.

y = x - (1+1) is NOT the same as y = x - 1 + 1 You should learn about operator precedence and L-R execution before criticising

WRONG: You need to understand operator precedence mate. Operator precedence is NOT ONLY L-R execution. Click Here.
I suppose you are a real developer but hay mate study up. As much as the compiler optimising your code, its not the job of the compiler to write our wrongs.Period!
EXAMPLE:

``````main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;

e = (a + b) * c / d;      // ( 30 * 15 ) / 5
cout << "Value of (a + b) * c / d is :" << e << endl ;

e = ((a + b) * c) / d;    // (30 * 15 ) / 5
cout << "Value of ((a + b) * c) / d is  :" << e << endl ;

e = (a + b) * (c / d);   // (30) * (15/5)
cout << "Value of (a + b) * (c / d) is  :" << e << endl ;

e = a + (b * c) / d;     //  20 + (150/5)
cout << "Value of a + (b * c) / d is  :" << e << endl ;

return 0;
}``````

Out put:
Value of (a + b) c / d is :90
Value of ((a + b)
c) / d is :90
Value of (a + b) (c / d) is :90
Value of a + (b
c) / d is :50

Where exactly do you suppose OP should check for division by zero? That code does not have a single example of division with a variable as its divisor. They are all non-zero constants.

2.

Telling OP about division by zero should not warrant your aggressive behavior. People are here to learn but not to be spoonfed. I have been with DaniWeb since the 2006/7 and I do know what the motive was/is. This place was a learning bunk for new and experienced Devs. We used to tell OP about all that when dealing with MATHS. I only alerted the OP about that possibility.

3.

My humble advise to you is make your research / do a revision before showing your flaw about order of precedence.

its not the job of the compiler to write our wrongs

I never said that it was. Whether or not a compiler should optimize is an opinion. Whether or not it actually does is a fact.

WRONG: You need to understand operator precedence mate.

And you need to actually try the given example before making a nonsensical claim. A simple test of the statements y = x - (1+1) and y = x - 1 + 1 shows that they give different results.

Reverend Jim

Wouldn't most compilers optimize y = x-1+1; to y = x; anyway?

Are you coding as an efficient coder or a compiler boy?
Your question does not make sence. Its not what variable is left at the end. Its about what VALUE is in that variable.
EXAMPLE:
`y = x-1+1; to y = x;` is not about `x`, Its about what value is assigned to `y`.
what about the was no `x`?
`y = 2 - 2+2;` OR `y = n-q+g`?

You should try and understand the code more than relying on compiler to write your wrongs.
Also compiler setting up the right precednce for you does not make your code right. Different compilers are optimized differently. Write good code and it will not bug you.

Thanks.

Oh Richard, don't blame me or Rev Jim. You made a post with 3 points; the first was simply wrong, the second was critical while being unhelpful, and the third was irrelevant at best, downright wrong in this context. After two very experienced people pointed out your mistakes you responded by digging yourself in deeper. It's time to let this go.
JC

Ha ha,
Very experienced? Your case about precedence was just wrong! I gave you a link to refresh yourself.
Also how do you deem something `irrelevant`? I forgot there were monks here who thinks of all the knowledge.

I will prefer debunking my reply than calling something as irrelivant.
Your claim was wrong. Man up and deal with it. I am surprised about your defendsiveness without a click in your mind that your
`y = x - (1+1) is NOT the same as y = x - 1 + 1 You should learn about operator precedence and L-R execution before criticising` claim was utterly wrong according to the C++ link I provided.

Reverend Jim

A simple test of the statements y = x - (1+1) and y = x - 1 + 1 shows that they give different results.

``````   #include <iostream>
int main(){
int x=20;
int y = x - (1+1); // Add 1+1 then - x;
int w =( x - 1) + 1; // x - 1 then add 1
int s =  x - 1 + 1;  //COMPILER DESIDE
std::cout << y <<"\n";
std::cout << w <<"\n";
std::cout << s;
return 0;
}``````

OUTPUT:
18
20
20
That was exactly my point. You dont leave things to compiler to determine your code outcome.
Therefore it is advisable to take matters into one's hand.
I was only telling the OP to do his own groupings!
I have given few examples !
You guys need to learn to ask questions thank jumping on band wagon calling each other

experienced.
Childish))

commented: This is just waste of time. I'm out. -3

Man up and deal with it

Seriously? You're going there? A true test of manhood is being willing to admit you're wrong and moving on instead of doubling down on your bad post. James isn't wrong. You are. Nothing wrong with making a mistake. We all make 'em. It's how you handle it. Accusing the guy who was right of being unmanly because he supposedly can't admit HIS mistake when you're the one who is wrong and he was right? 'Nuff said right there.

If you TRULY think you're right, which I seriously doubt, I suggest you apply your "corrections" to the OP's code and see if they make the program better or worse. You HAVE done that already, right? If you haven't, I have. The OP's code with the original line 9 works for 14 as input, correctly finding 17 and 13 as the nearest prime numbers. If you change line 9 to YOUR line, it will find 17 and 11, not 13. Your suggestion breaks the OP's program. I believe that "debunks" your reply, as requested. You gave bad advice that the OP should not follow. James pointed that out. Or just run the code below...

``````int x = 5; // substitute whatever you want for x
int y = x - 1 + 1;
cout << y << endl; // 5
y = x;
cout << y << endl; // 5
y = x - (1 + 1); // your "correction"
cout << y << endl; // 3:  Why does your "correction" yield a bad result?``````

As far as your links go, "proving" that operator precedence is not always left to right is easy and irrelevant because James never thought or said it was, so what's the point? The issue is whether `y=x-1+1` is equivalent to `y=x-(1+1)`. You say it is, he says it isn't. Do your really want to continue saying you're right and he's wrong? Your other "points" about compiler optimization and your examples that have nothing to do with the OP's code or your suggestions are simply a diversion from the original issue, which is that you told the OP to stick parentheses where they shouldn't go. Everyone here knows that when you mix multiplication/division with addition/subtraction, it's no longer left to right. Completely irrelevant to the issue at hand. No one on here needs the compiler to fix anything. Reverend Jim brought up that question, and it's a good question. I don't see how anyone can read that question as saying we should write buggy code and let the compiler fix it.

It's all pointless though. This thread jumped the shark with your "manhood" throwdown.

AssertNull
Are you sound!? Sorry but I have to ask you that simple question.
Go back and read my comments from the start. You joining the bandwagon will not do you any good. Secondly what I suggested to the OP was a piece of advice. To know about precedence and I have given numerous examples.
Do you read a thread before commenting.
Also just to remind you I did not join Danyweb yesterday ok? I have been with this great community nearly 11yrs.
I was ranked 45 in all approved ratings 3yrs ago.
This community was not meant for bandwagons or dog fight and aggressiveness but people sharing ideas.

I gave the OP an information that it's always better to group Math. Therefore providing an example. Besides I have given other examples and talk about the precedence left and right groupings.
What the heck in the name of kboard you don't get it!?

This is crazy. I AM OUTTA HERE!

OK everybody. This is getting too heated. I'm closing this thread.
JC

commented: My guess would be Troll. +14
commented: MY guess: bitten by a trump mosquito +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.