Skip to content
Sign upLog in
← Back to Community

How to fix code?

Profile icon
JoyHu

the code says it didn't work because it needed an identifier. How do I fix it??

Voters
Profile icon
COLLINONEILL
Profile icon
Alluahbar
Profile icon
JoyHu
Comments
hotnewtop
Profile icon
Coder100

Hello. That is a warning. You can ignore warnings. During build-time, the compiler is just saying that you have some code that isn't being used or can be improved. Anyways, in your case, this is what is actually happening:

#include <iostream> using namespace std; int main() { int a; cout << "Input a number "; cin >> a; if (a % 5 == 0) cout << "Yes"; else if (a % 3 == 0) (a < 20); // this will get evaluated if a % 3 == 0, which does nothing cout << "Yes"; // this will be evaluated always return 0; }

Perhaps you mean:

#include <iostream> using namespace std; int main() { int a; cout << "Input a number "; cin >> a; if (a % 5 == 0) cout << "Yes"; else if (a % 3 == 0 && a < 20) cout << "Yes"; else cout << "No"; // so there is a message for EVERY statement return 0; }
Profile icon
mwilki7

I would recommend indenting your code in certain places, it may help spot errors.

I think you want to use the AND(&&) operator: else if (a % 3 == 0 && a < 20)
if you want to have multiple conditions in a single if statement.

#include <iostream> using namespace std; int main() { int a; cout << "Input a number "; cin >> a; if(a%5== 0) cout <<"Yes"; else if (a%3== 0)(a<20); cout << "Yes"; return 0; }

The problem is with the parentheses

else if (a%3== 0)(a<20); cout << "Yes";

What's really happening is this

else if (a % 3 == 0) { (a<20); // nothing really happens here } cout << "Yes";
Profile icon
IvanChang111

It works perfectly fine to me, It doesn't show any errors when I run it.