Ask coding questions

← Back to all posts
How to fix code?
JoyHu (1)

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

Comments
hotnewtop
Coder100 (16802)

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;
}
mwilki7 (1136)

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";
IvanChang111 (12)

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