227k views
5 votes
C++

Set hasDigit to true if the 3-character passCode contains a digit.

#include
#include
#include
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 Answer

5 votes

Answer:

Add this code the the /* Your solution goes here */ part of program:

for (int i=0; i<3; i++) { //iterates through the 3-character passCode

if (isdigit(passCode[i])) //uses isdigit() method to check if character is a digit

hasDigit = true; } //sets the value of hasDigit to true when the above if condition evaluates to true

Step-by-step explanation:

Here is the complete program:

#include <iostream> //to use input output functions

using namespace std; // to identify objects like cin cout

int main() { // start of main function

bool hasDigit; // declares a bool type variable

string passCode; //declares a string type variable to store 3-character passcode

hasDigit = false; // sets the value of hasDigit as false initially

cin >> passCode; // reads the pass code from user

for (int i=0; i<3; i++) { //iterate through the 3 character pass code

if (isdigit(passCode[i])) // checks if any character of the 3-character passcode contains a digit

hasDigit = true; } //sets the value of hasDigit to true if the passcode contains a digit

if (hasDigit) { // if pass code has a digit

cout << "Has a digit." << endl;} //displays this message when passcode has a digit

else { //if pass code does not have a digit

cout << "Has no digit." << endl;} //displays this message when passcode does not have a digit

return 0;}

I will explain the program with an example. Lets say the user enters ab1 as passcode. Then the for loop works as follows:

At first iteration:

i = 0

i<3 is true because i=0

if (isdigit(passCode[i]) this if statement has a method isdigit which is passed the i-th character of passCode to check if that character is a digit. This condition evaluates to false because passCode[0] points to the first character of pass code i.e. a which is not a digit. So the value of i is incremented to 1

At second iteration:

i = 1

i<3 is true because i=1

if (isdigit(passCode[i]) this if statement has a method isdigit which is passed the i-th character of passCode to check if that character is a digit. This condition evaluates to false because passCode[1] points to the second character of pass code i.e. b which is not a digit. So the value of i is incremented to 1

At third iteration:

i = 2

i<3 is true because i=2

if (isdigit(passCode[i]) this if statement has a method isdigit which is passed the i-th character of passCode to check if that character is a digit. This condition evaluates to true because passCode[3] points to the third character of pass code i.e. 1 which is a digit. So the hasDigit = true; statement executes which set hasDigit to true.

Next, the loop breaks at i=3 because value of i is incremented to 1 and the condition i<3 becomes false.

Now the statement if (hasDigit) executes which checks if hasDigit holds. So the value of hasDigit is true hence the output of the program is:

Has a digit.

C++ Set hasDigit to true if the 3-character passCode contains a digit. #include #include-example-1
User Sroush
by
6.1k points