+ 1

Can someone tell me what's wrong with this? It says something about the switch == false isn't right, but I can't see anything.

#include <iostream> using namespace std; int main(){ int div=2; int num; bool sw = true; cout<<"Please enter a number:"<<endl; cin>>num; while(div<num&&num%div>0){ if(num%div>0){ div++; } else{ switch == false; } } if(sw==true){ cout<<num<<" is a prime number."; } else{ cout<<num<<" is not a prime number."; } return 0; }

5th Aug 2018, 6:25 PM
Thomas Wald
Thomas Wald - avatar
3 Respostas
+ 3
Don't you think that the if statement inside the while is redundant and prevents the else part from getting evaluated? Or maybe you should remove the second condition of the while? How do you fix that Thomas Wald ? while ( (div < num) && (num % div > 0) ) { if(num % div > 0) { div++; } else { sw = false; } }
5th Aug 2018, 8:25 PM
Babak
Babak - avatar
+ 1
switch is a statement + its not defined ass a bool i think you meant to put sw (since you already defined it above )
5th Aug 2018, 6:31 PM
Sterben
Sterben - avatar
0
try sw= false, as that's the name of your boolean and the assignment operator is "=" not "=="
5th Aug 2018, 6:32 PM
hinanawi
hinanawi - avatar