1

I'm learning about functions in C++ and I saw this code on Tutorialspoint which tells us whether the input is an int or a string.

Link to the Tutorialspoint article : https://www.tutorialspoint.com/cplusplus-program-to-check-if-input-is-an-integer-or-a-string

This is the original code:

#include <iostream>
using namespace std;
//check if number or string
bool check_number(string str) {
   for (int i = 0; i < str.length(); i++)
   if (isdigit(str[i]) == false)
      return false;
      return true;
}
int main() {
   string str = "sunidhi";
   if (check_number(str))
      cout<<str<< " is an integer"<<endl;
   else
      cout<<str<< " is a string"<<endl;
      string str1 = "1234";
   if (check_number(str1))
      //output 1
      cout<<str1<< " is an integer";
   else
      //output 2
      cout<<str1<< " is a string";
}

The original one works perfectly fine, but my code either only shows ouput 1 or only shows output 2 no matter whether you enter an int or a string.

My code:

Note : my code was written on an online compiler. Link to the compiler : https://www.onlinegdb.com

#include <iostream>
using namespace std;
//the function which checks input
bool check(string s){
    for(int i = 0; i < s.length(); i++)
    if(isdigit(s[i]) != true)
    return false;

return true;     
    
}
//driver code
int main(){
    string str = "9760";
    if(check(str)){
        //output 1
        cout<<"Thanks! the word was " <<str;
    }
    else{
        //output 2
        cout<<"Oops! maybe you entered a number!";
    }
}    

Ouput when executing my program : Thanks! the word was 9760

Link to the code project: https://onlinegdb.com/HkcWVpFRU

Thank you!

  • 6
    `i > s.length();` looks very wrong – UnholySheep Jul 01 '20 at 08:15
  • @UnholySheep Thanks for the help but still the same thing. –  Jul 01 '20 at 08:18
  • Given the C++ tag check this thread https://stackoverflow.com/q/8888748/6865932 – anastaciu Jul 01 '20 at 08:33
  • @AustinParker No, it's not the same thing. If you change to `i < s.length()` your code will do the wrong thing, but it will at least do different things depending on the input. With `i > s.length()` it will always return true. – super Jul 01 '20 at 08:40
  • @super I meant that the same problem is still occurring. Anyways, it is solved now. Thanks to SzyomonO! –  Jul 01 '20 at 08:44

1 Answers1

0

You are checking if the char is a digit and returning false if it is, you should change it to

bool check(string s){
    for(int i = 0; i < s.length(); i++)
        if(isdigit(s[i])
           return false;
return true;     
}

a sidenote, if you want to check for false you can do (!bool) instead of (bool != true) it looks cleaner

SzymonO
  • 432
  • 3
  • 15
  • 3
    Perfect illustration of the importance of proper naming: `check` ... what? I had spotted the bug at `if (check(str))`, you found it inside of the function: we're both right and wrong and this ambiguity shows that this is not self-documenting code. – PiCTo Jul 01 '20 at 08:24
  • 1
    @AustinParker It checks if the character is a digit and returns a boolean value. If you ever have doubts about a function I recommend the cppreference page. https://en.cppreference.com/w/cpp/string/byte/isdigit – SzymonO Jul 01 '20 at 08:25
  • @PiCTo ummm.. I dont exactly get what you're trying to say there... the check(str) is only trying to apply the function's purpose to str... Can you explain the bug? –  Jul 01 '20 at 08:26
  • 3
    @AustinParker That the naming convention for the function is wrong. We see a function called `check` but what does it check? It should be named `checkIfStringContainsNumber`. Sometimes it's better to use a longer name that is self explanatory. – SzymonO Jul 01 '20 at 08:27
  • 1
    @AustinParker That it isn't obvious what your code (in particular your function) is doing: naming it `is_digit`, `represents_digit`, or any clear name would have saved you from this bug. Properly naming functions, variables, ... is a very important skill as it makes programs clearer, which in turn makes it harder for bugs to hide. – PiCTo Jul 01 '20 at 08:28
  • @PiCTo I'm sorry, I will keep that in mind from now on.. –  Jul 01 '20 at 08:30
  • @SzymonO Lastly, What was the problem with isdigit(s[i]) == false ? –  Jul 01 '20 at 08:32
  • 2
    @AustinParker There is no problem with it. It will work exacly the same but `!isdigit(s[i])` is much shorter, better looking and trust me that every programmer that reads it will get what the `!` at the beggining means – SzymonO Jul 01 '20 at 08:34
  • @SzymonO but the isdigit(s[i]) didnt work, Your solution worked. –  Jul 01 '20 at 08:45