2

I'm trying to declare this string as Invalid but in an input like this:

59G71341 or 8pjf7h14sx13 or 60s1v344

My output is getting approved through my string if statement and is getting listed as Valid.

Could anyone guide me to why its passing through my if statement and labeling Valid!!

I haven't learned how to use a debugger yet so bare with me.

Task description:

Declare a Boolean variable named goodPasswd. Use goodPasswd to output "Valid" if secretStr contains no more than 5 digits and secretStr's length is greater than or equal to 5, and "Invalid" otherwise.

Ex: If the input is 80796, then the output is: Valid

Ex: If the input is XBdg, then the output is: Invalid

#include <iostream>
using namespace std;

int main()
{
    string secretStr;

    bool goodPasswd = false;
    cin >> secretStr;
    int counter = 0;
    for (int i = 0; i < secretStr.length(); ++i)
    {
        if ((secretStr[i] >= 0) && (secretStr[i] <= 9))
        {
            ++counter;
        }
    }  //approves string if both true
    if ((counter <= 5) && (secretStr.length() >= 5))
    {
        goodPasswd = true;
    }
    else
    {
        goodPasswd = false;
    }

    if (goodPasswd)
    {
        cout << "Valid" << endl;
    }
    else
    {
        cout << "Invalid" << endl;
    }

    return 0;
}
thedemons
  • 1,139
  • 2
  • 9
  • 25
  • 1
    Print out the value of `(int)secretStr[0]` when `secretStr` is "123". Is it the value you expect? If not, look up "ASCII encoding" and "c++ character literal". – David Grayson Oct 23 '22 at 05:16
  • Side note, `bool goodPasswd = (counter <= 5) && (secretStr.length() >= 5);` is more concise/clearer than what you have there. Similarly you can do `cout << (goodPasswd ? "Valid" : "Invalid") << endl;` – Borgleader Oct 23 '22 at 05:17
  • [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/13253010) – thedemons Oct 23 '22 at 05:18
  • I was studying and seen that it is bad practice, but I do my work through Zybooks and I'm forced to use namespace for now. – Gabriel Zuniga Oct 23 '22 at 05:25

1 Answers1

2
if ((secretStr[i] >= 0) && (secretStr[i] <= 9))

should be

if ((secretStr[i] >= '0') && (secretStr[i] <= '9'))

0 and 9 are integers, but you are comparing characters, so you need to use the characters '0' and '9', or you could just use the isdigit function.

if (isdigit(secretStr[i]))

isdigit is declared in #include <cctype>

Not related to your question but you don't need to goodPasswd variable. Simply

if (counter <= 5 && secretStr.length() >= 5)
{
    cout << "Valid" << endl;
}
else
{
    cout << "Invalid" << endl;
}

seems a bit cleaner to me.

john
  • 85,011
  • 4
  • 57
  • 81
  • Just tried and and it solved my issue! And the isdigit function was recommended but I didn't know how to implement it in a loop that scans through the index's! Thank you for giving an example on how to use it. I will be accepting your answer when it is available. – Gabriel Zuniga Oct 23 '22 at 05:22
  • also I am doing this through ZyBooks and the questions don't allow me to edit some parts of the code. So I have to work with what they give me. – Gabriel Zuniga Oct 23 '22 at 05:24