C++: Determining If a String Contains A Numeric Digit

In summary, to set the variable hasDigit to true if the 3-character passCode contains a digit, you can use a "for" loop and check each character in the string sequentially. If a digit is found, set hasDigit to true and break out of the loop. Otherwise, the loop will continue until all characters have been checked and hasDigit will remain false. You can also utilize functions from the std::string library, such as substr(), replace(), and find_first_of(), to make the process simpler.
  • #1
ineedhelpnow
651
0
Set hadDigit to true if the 3-character passCode contains a digit.

Sample program:

Code:
#include 
#include 
#include 
using namespace std;

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

   passCode = "abc";

   <student code>

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

   return 0;
}

I'm super lost on this. I could definitely use some help/hints. Thanks
 
Technology news on Phys.org
  • #2
One way to do this would be to use a "for" loop and check each character in the string sequentially to see if it is a digit or not. Once the first digit is detected, then hasDigit should be set to true and the loop should be broken, otherwise the loop will have checked all characters in the string and hasDigit will remain false.

For the "for" loop you will need the size of "passCode" as the value that the loop's index should be less than.

Also, it appears you need to include some libraries...
 
  • #3
I can't say I am a fan of std::string in C++ as there are people who still insist on using char* or CString if you are using Microsoft stuff (and all the compatibility issues of these "string" classes).

However, if you are already using std:string, I would suggest you look at the reference of that library:

string - C++ Reference

It already provided handy functions like substr(), replace() and find_first_of() that answer a number of questions you posted fairly simply.
 

FAQ: C++: Determining If a String Contains A Numeric Digit

How can I check if a string contains a numeric digit in C++?

To check if a string contains a numeric digit in C++, you can use the isdigit() function from the cctype library. This function returns a non-zero value if the character is a numeric digit, and a value of zero if it is not.

What is the syntax for using the isdigit() function in C++?

The syntax for using the isdigit() function in C++ is: isdigit(character). This function takes in a single character as an argument and returns a value of type int.

How do I handle multiple digits in a string when checking for numeric digits in C++?

To handle multiple digits in a string, you can use a loop to iterate through each character in the string and check if it is a numeric digit using the isdigit() function. If any character is found to be a numeric digit, you can set a flag to indicate that the string contains a numeric digit.

Can I use the isdigit() function to check for decimal values in a string?

No, the isdigit() function can only check for whole numeric digits. To check for decimal values, you can use the isdigit() function in combination with other functions such as isxdigit() or isalpha().

Are there any other methods for determining if a string contains a numeric digit in C++?

Yes, there are other methods for determining if a string contains a numeric digit in C++. One option is to use regular expressions to match for numeric digits in a string. Another option is to convert the string to a numeric data type, such as int or double, and catch any errors that may occur if the string cannot be converted to a numeric value.

Similar threads

Replies
4
Views
10K
Replies
1
Views
2K
Replies
1
Views
5K
Replies
3
Views
5K
Replies
5
Views
2K
Replies
22
Views
3K
Replies
8
Views
2K
Back
Top