0

I am new to programming and I want to get an unlimited input of numbers from user, and I need to compare each number to 4 or 7; if the number is 4 or 7, I will increase counter.

The issue is that the if condition if(arr[i]!='4' || arr[i]!='7') is executed even if the number is 4 or 7.

I tried to implement another if by comparing ASCII code >>> if(arr[i]!=0x34 || arr[i]!=0x37) but this solution also doesn't work.

Can you help me to find the issue?

#include <iostream>
#include <string>
#include <string.h>

using namespace std;

int main()
{
    int counter=0;
    char arr[]={};                 
    cin >> arr;

    for (int i=0 ; i<strlen(arr)-1 ; i++)
    {
          if(arr[i]!='4' || arr[i]!='7')
          {
             cout << "NO" << endl;
             counter=0;                 
             break;
          }else
            counter++;
    }
    cout << counter << endl;
    if(counter==4 || counter==7)
    cout << "YES" << endl;

}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
logan_92
  • 117
  • 9
  • If `arr[i]=='4'`, what is the value of `arr[i]!='7'`? – 1201ProgramAlarm Mar 29 '20 at 17:54
  • Obviously your expression `if(arr[i]!='4' || arr[i]!='7')` is always true; Did you mean `if(arr[i]!='4' && arr[i]!='7')` ? – pptaszni Mar 29 '20 at 17:55
  • You obviously want to increment your counter if, in plain English: "arr[i] is equal to 4 or arr[i] is equal to 7". You write this in C++ as `(arr[i] == '4' || arr[i] == '7')`. To find the negation of a boolean statement, you apply [De Morgan's Laws](https://en.wikipedia.org/wiki/De_Morgan%27s_laws), or simply leave the "not" outside the parentheses. `!(arr[i] == '4' || arr[i] == '7')` is just as valid as `(arr[i] != '4' && arr[i] != '7')`. – JohnFilleau Mar 29 '20 at 17:59
  • Every number is either not equal to 4 or not equal to 7. That's your mistake. – john Mar 29 '20 at 18:32
  • thank you guys , it solved by (arr[i] != '4' && arr[i] != '7') . my mistake was || – logan_92 Mar 29 '20 at 22:41

1 Answers1

2

Your program has undefined behavior, because this

char arr[]={};                 

creates an array of length zero. This is not even allowed in standard C++. If your compiler didn't complain about it, then that is because it is using a non-standard language extension.

In any case, the array has length zero. You then try to write to it with

 cin >> arr;

which causes the array to be accessed out-of-bounds, no matter how long the given input is. This will cause undefined behavior and you will not have any guarantee whatsoever on how the program will behave.

Never use char arrays to store strings, especially not user input. Use std::string instead:

 std::string arr;
 cin >> arr;

Then instead of strlen(arr) you should use arr.size().


I am pretty sure you have further logic errors in your program (see question comments). But before you fix the program logic, you should make sure that you are writing a valid program in the first place.

walnut
  • 21,629
  • 4
  • 23
  • 59
  • than you so much for your help , strlen(arr) >> for array of char arr.size >> for string sizeof(arr) >> what is the usage of it?? – logan_92 Mar 29 '20 at 21:11
  • @logan_92 `sizeof(arr)` gives the size of the `std::string` object itself, not the length of the string it contains. You should rarely need to use `sizeof` for anything. If you want to know the size of some container-like object, e.g. an array, then use `std::size` instead. – walnut Mar 29 '20 at 21:13