0

can anyone help me with my code? I don't understand where its making such problem. my code is to check if a given string is valid variable or not. My checkIdentifier function should return false for some cases and true for some cases but its only return false. my input is abcd and its will print valid but when my input is abcd# its will print invalid

#include<bits/stdc++.h>
#include<strings.h>

using namespace std;

string ishfakur;
void getInput(){
    cin>>ishfakur;
}

bool checkKeyword(){
string keyword[32]={
      "auto","double","int","struct","break","else","long",
      "switch","case","enum","register","typedef","char",
      "extern","return","union","const","float","short",
      "unsigned","continue","for","signed","void","default",
      "goto","sizeof","voltile","do","if","static","while"
   } ;
    for(int i=0;i<32;i++){
        if(ishfakur==keyword[i]){
            return true;
        }
    }
}
bool checkIdentifier(){
        if(ishfakur.length()>=30 || ishfakur.at(0)<='9' || strstr(ishfakur.c_str(),"#") || checkKeyword()==true)
            return false;
        else
            return true;
}
void giveOutout(bool st){
    if(st==true){
        printf("valid");
    }
    else
        printf("invalid");
}

int main(){

    bool status;
    getInput();
    status = checkIdentifier();
    giveOutout(status);
}
  • 2
    What condition is `ishfakur.at(0)>=0` in `checkIdentifier` checking for? – 1201ProgramAlarm Sep 21 '21 at 15:29
  • 1
    `for(int i=0;i<=32;i++)` 33 iterations in the array of 32 elements. – 273K Sep 21 '21 at 15:29
  • 3
    Unrelated: `#include` [loads the gun](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and `using namespace std;` [takes the safety off](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). This makes it very easy for you to accidentally shoot yourself in the foot. – user4581301 Sep 21 '21 at 15:34
  • please show a [mre]. What is the input to this program? What is the expected output? What is the code supposed to do? Have you tried using a debugger? – Alan Birtles Sep 21 '21 at 15:41
  • I used ishfakur.at(0)>=0 to check if the strings starts with digits. in that for loop sorry it was my mistake – Mohammad Ishfakur Sep 21 '21 at 15:45
  • I have changed my title. I didn't understand that you are saying about header can you please describe more. @user4581301 – Mohammad Ishfakur Sep 21 '21 at 15:48
  • hi @AlanBirtles I have updated – Mohammad Ishfakur Sep 21 '21 at 15:49
  • Here are two links that explain the risk you are taking in detail: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h and https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – user4581301 Sep 21 '21 at 15:49
  • should `ishfakur.at(0)>=0` be `(ishfakur.at(0)>='0' && ishfakur.at(0)<='9')`? Or just `std::isdigit(static_cast(ishfakur.at(0)))` – Alan Birtles Sep 21 '21 at 15:52
  • The TL;DR-resistant version is `#include` includes the entire C++ Standard library, with its tens (perhaps even hundreds) of thousands of identifiers you are not using and may not even know exist. `using namespace std;` allows them to conflict with any of your identifiers in the global namespace. This can lead to compiler errors and very interesting and hard to debug runtime errors. – user4581301 Sep 21 '21 at 15:53
  • can anyone help me what is the reason that its only finds "if" condition is true where no such string exist that satisfies this condition. – Mohammad Ishfakur Sep 21 '21 at 16:05
  • In almost all character encoding systems (and certainly in ASCII), the alphabet characters (valid as the first in an identifier name) have values greater than that of the `'0'` character. See the problem in your test? – Adrian Mole Sep 21 '21 at 16:18
  • I have changed still the problem not solved @AdrianMole – Mohammad Ishfakur Sep 21 '21 at 16:21
  • Now its working fine for invalid but when it has a valid string it doesn't print valid and compiles. – Mohammad Ishfakur Sep 21 '21 at 16:33
  • Thank you everyone. You guys helped me a lot. – Mohammad Ishfakur Sep 23 '21 at 11:52

1 Answers1

0

its solved Here the condition ishfakur.at(0)>='0' satisfies even when its a valid string because the ASCI values for a to z are greater than 0 so its always true. So I have changed it to ishfakur.at(0)<='9' now I am checking if its less than equals to 9 now its restricted between 0 to 9. So no chance for a valid string to be invalid. Also checkKeyword() function has returns nothing if the condition is not satisfies but it has to return either true or false as the return type is Boolean. So after the loop a default return false; added. this was the only reason the program couldn't provide desired output.

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

using namespace std;

string ishfakur;
string keyword[32]={
      "auto","double","int","struct","break","else","long",
      "switch","case","enum","register","typedef","char",
      "extern","return","union","const","float","short",
      "unsigned","continue","for","signed","void","default",
      "goto","sizeof","voltile","do","if","static","while"
   } ;
void getInput(){
    cin>>ishfakur;
}

bool checkKeyword(){
    for(int i=0;i<32;i++){
        if(ishfakur==keyword[i]){
            return true;
        }
    }
return false;
}
bool checkIdentifier(){
    bool status=true;
        if(ishfakur.length()>=30 ||
           ishfakur.at(0)<='9' ||
           strstr(ishfakur.c_str(),"#") ||
           checkKeyword()==true)
            return false;

    return status;
}
void giveOutout(){

    if(checkIdentifier()==true){
        printf("valid");
    }
    else
        printf("invalid");
}

int main(){

    getInput();
    giveOutout();
}
  • No, it's not solved. When `ifshakur` is not found, `checkKeyword` doesn't execute a `return` statement. The behavior of the program is undefined. – Pete Becker Sep 21 '21 at 17:14
  • because I only need if its true. false has no use in this code but i can add – Mohammad Ishfakur Sep 21 '21 at 17:18
  • It doesn't matter whether the code checks for `false`. Checking the value **at all** results in undefined behavior when the function runs off the end without a return statement. – Pete Becker Sep 21 '21 at 17:22