Answer to Question #99431 in C++ for Caleb

Question #99431
Set hasDigit to true if the 3-character passCode contains a digit.
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main() {
bool hasDigit;
string passCode;

hasDigit = false;
cin >> passCode;

/* Your solution goes here */

if (hasDigit) {
cout << "Has a digit." << endl;
}
else {
cout << "Has no digit." << endl;
}

return 0;
}
1
Expert's answer
2019-12-05T10:32:44-0500
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main() {
    bool hasDigit;
    string passCode;
    hasDigit = false;

    cin >> passCode;
    /* Your solution goes here */
    for (int i=0; i<3; i++) {
        if (isdigit(passCode[i]))
            hasDigit = true;
    }

    if (hasDigit) {
        cout << "Has a digit." << endl;
    }
    else {
        cout << "Has no digit." << endl;
    }

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS