Question

In: Computer Science

(C++)Set hasDigit to true if the 3-character passCode contains a digit.

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;

Solutions

Expert Solution

// Declare required header file
 
// iostream is used for input output
 
#include 
 
// string is used to use string type
 
// character
 
#include 
 
// This header file use here for
 
// using the isdigit() function
 
// to check digits is exist or not
 
#include 
 
// Namespace std for cin cout
 
using namespace std;
 
// Create a main function of the
 
// program
 
int main()
 
{
 
// Declare hadDigit is a Boolean
 
// variable
 
bool hasDigit;
 
// Declare passCode is a string character
 
string passCode;
 
// Initially hasDigit Declare
 
// false statement
 
hasDigit = false;
 
// Prompt the passcode from console
 
cin >> passCode;
 
/* Your solution goes here */
 
// Declare the position of 3 digits
 
// passCode.at(hold the input position value)
 
// It means passCode.at() find out the value form that position.
 
// Assign the input value to digit1 from index 0
 
int digit1 = passCode.at(0);
 
// Assign the input value to digit2 from index 1
 
int digit2 = passCode.at(1);
 
// Assign the input value to digit3 from index 2
 
int digit3 = passCode.at(2);
 
// When anyone digits(digit1, digit2,digit3) contains a digit
 
if(isdigit(digit1) || isdigit(digit2) || isdigit(digit3))
 
   {
 
     // Set hasDigit to true
 
       hasDigit = true;
 
   }
 
   // When hasDigit contains true
 
if (hasDigit) {
 
// Display has a digit
 
cout << "Has a digit." << endl;
 
}
 
// Otherwise it has no digit
 
else {
 
cout << "Has no digit." << endl;
 
}
 
return 0;
 
}

Expert Solution

The following information has been provided for the question above:

  • "Screen shot of C++ code" for your understanding.
  • "Text format code" to copy and execute in your IDE.

Screen shot of C++ code:

Output:

Text format code:

// Header files section
#include <iostream>
#include <cctype>
#include <string>
using namespace std;

//Program begins with a main function
int main()
{
//Declare variable
string passCode;
//Initialize the variables
bool hasDigit = false;
passCode = "abc";

//Set hasDigit to true if the 3-character
//passCode contains a digit.
if (isdigit(passCode.at(0)) || isdigit(passCode.at(1))
  || isdigit(passCode.at(2)))
{
  hasDigit = true;
}
if (hasDigit)
{
  cout << "Has a digit." << endl;
}
else
{
  cout << "Has no digit." << endl;
}
return 0;
}


Related Solutions

(In C)Set hasDigit to true if the 3-character passCode contains a digit.
  Set hasDigit to true if the 3-character passCode contains a digit. (in C) #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h>   int main(void) { bool hasDigit; char passCode[50]; hasDigit = false; scanf("%s", passCode); /* Your solution goes here */ if (hasDigit) { printf("Has a digit.\n"); } else { printf("Has no digit.\n"); } return 0; }
(JAVA)Set hasDigit to true if the 3-character passCode contains a digit.
JAVE 3.10.1: String with digit. Set hasDigit to true if the 3-character passcode contains a digit. MUST USE THIS TEMPLATE: public class CheckingPasscodes {public static void main (String [] args) {boolean hasDigit;String passCode; hasDigit = false;passCode = "abc"; /* Your solution goes here */ if (hasDigit) {System.out.println("Has a digit.");}else {System.out.println("Has no digit.");}}}
Set hasDigit to true if the 3-character passCode contains a digit. public class CheckingPasscodes { public...
Set hasDigit to true if the 3-character passCode contains a digit. public class CheckingPasscodes { public static void main (String [] args) { boolean hasDigit = false; String passCode = ""; int valid = 0; passCode = "abc"; /* Your solution goes here */ if (hasDigit) { System.out.println("Has a digit."); } else { System.out.println("Has no digit."); } return; } }
(In JAVA)Write code to print the location of any alphabetic character in the 2-character string passCode.
Java Language Write code to print the location of any alphabetic character in the 2-character string passCode. Each alphabetic character detected should print a separate statement followed by a newline. Ex: If passCode is "9a", output is: Alphabetic at 1 import java.util.Scanner;   public class FindAlpha {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       String passCode;              passCode = scnr.next();     ...
Passwords of length 10 are formed using the character set S = {a, b, c, …,...
Passwords of length 10 are formed using the character set S = {a, b, c, …, z, A, B, …, Z, 0, 1, 2, .., 9}. Thus the set S consists of 62 different characters. For each part below, you must include a few words that describe your counting strategy. (a) How many passwords of length 10 use the character Q exactly two times? (b) How many passwords of length 10 use at most one of the “upper case” letters...
a. How many 3-digit numbers can you make using the digits from the set of (3,...
a. How many 3-digit numbers can you make using the digits from the set of (3, 4, 5, 6, 8, 2, 7) without repetitions? b. How many numbers can be made with the digits from the set of (0, 3, 5, 7, 8, 4, 9), which are greater than 0 and less than a thousand (repetitions are allowed)?
3) Which of the following statements is true or false a) Character displacement occurs when two...
3) Which of the following statements is true or false a) Character displacement occurs when two species using the same resource overlap in their distributions and become more similar to each other than they are where they don’t overlap. b) If you wanted to know if an organism which expresses a dominant phenotype is homozygous or heterozygous for a trait (e.g., round pea shape, R_), you could do a “test cross” between the unknown individual (R_) and an individual which...
Question 4: Set P to the largest digit in your 5 digit number. For example, if...
Question 4: Set P to the largest digit in your 5 digit number. For example, if your 5 digit number is 88412, then P should be set to 8. Calculate and print the value of (P+1) multiplied by itself (P+3) times. As an example, if your 5 digit number is 88412, you should print the result of multiplying 9 with itself 12 times -- this is also known as 9 to the power of 12. HINT: you are free to...
a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it...
a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it is palindrome or not. [2.5 marks] b. Write a c++ function, called GCD that receives two parameters n, d, and prints their greatest common divisor [2.5 marks] Now after you created the two functions, you have to write a main function that can call the above functions according to user choice. Write a menu that allows the user to select from the following options...
In C++, The following program reads one character from the keyboard and will display the character...
In C++, The following program reads one character from the keyboard and will display the character in uppercase if it is lowercase and does the opposite when the character is in uppercase. If the character is a digit, it displays a message with the digit. Modify the program below such that if one of the whitespaces is entered, it displays a message and tells what the character was. // This program reads one character from the keyboard and will //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT