-1

Issue: After iterating through a cin password and saving instances of integers in the password to an integer variable(number). I use an if statement to search for conditions: if there are 5 or less numbers in the password AND the password is less than ten characters in length. The statement seems to be evaluating to true when only one of the conditions is true instead of both.

Result I am getting:

Test input not evaluating correctly

Question: Declare a Boolean variable named goodPassword. Use goodPassword to output "Valid" if codeWord contains no more than 5 digits and codeWord's length is less than 10, and "Invalid" otherwise.

My Code

#include <iostream>
using namespace std;

int main() {
    string codeWord;
    bool goodPassword;

    unsigned int i;

    int number;
    cin >> codeWord;
   
    for (i=0; i<codeWord.size(); ++i){
        //check each index for number and declare size limit
        if( isdigit(codeWord[i]) ){
            i=number;
        }  
    } /* Type your additional code here */
   
    if ((sizeof(number) < 6) && (codeWord.size()<10)){
        goodPassword = true;
    }
    else {
        goodPassword=false;
    }
         
    if (goodPassword) {
        cout << "Valid" << endl;
    }
    else {
        cout << "Invalid" << endl;
    }
   
    return 0;
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108

2 Answers2

3

You're not counting the number of digits in codeWord correctly.

You need to initialize number to 0. Then in the loop over the characters of codeWord, add 1 to it whenever the number is a digit.

Then you shouldn't check its size, just check its value.

    int number = 0;
    cin >> codeWord;
   
    for (i=0; i<codeWord.size(); ++i){
        //check each index for number and declare size limit
        if( isdigit(codeWord[i]) ){
            number++;
        }  
    } /* Type your additional code here */
   
    if ((number < 6) && (codeWord.size()<10)){
        goodPassword = true;
    }
    else {
        goodPassword=false;
    }
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • An additional precaution just in case a negative `char` sneeks into `codeWord`: `isdigit(static_cast(codeWord[i]))` – Ted Lyngmo Aug 15 '23 at 21:47
-1

Sizeof not working as intended The sizeof function check the number of bytes in a number not the length, to get the length of the number by converting it to a string and counting the number of characters.

So the code would be

#include <iostream>
using namespace std;

int main() {
    string codeWord;
    bool goodPassword = false; // Initialize to false

    unsigned int i;
    string numberString; // Store digits as a string

    cin >> codeWord;

    for (i = 0; i < codeWord.size(); ++i) {
        // Check each index for a number and store digits in numberString
        if (isdigit(codeWord[i])) {
            numberString += codeWord[i];
        }
    }

    // Convert numberString to an integer
    int number = stoi(numberString);

    if (numberString.size() < 6 && codeWord.size() < 10) {
        goodPassword = true;
    }

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

    return 0;
}