C/C++ C++ - Determining If A String Contains Any Numeric Digits

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    C++ Numeric String
AI Thread Summary
The discussion focuses on determining if a 3-character passCode contains a digit without using loops. The key solution involves utilizing the `isdigit` function from the `<cctype>` header to check each character in the passCode. The suggested code sets the boolean variable `hasDigit` to true if any of the three characters are digits. The use of the boolean operator `||` (or) is highlighted as an efficient way to combine conditions. Additionally, alternatives using `if` statements are discussed, emphasizing that each character must be checked individually to ensure that `hasDigit` reflects the presence of a digit correctly. Overall, the conversation provides guidance on implementing these checks effectively within the constraints given.
needOfHelpCMath
Messages
70
Reaction score
0
Set hasDigit to true if the 3-character passCode contains a digit.

Code:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

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

	passCode = "abc";

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

	return 0;
}

Note: I cannot use loops.
 
Technology news on Phys.org
Re: i am lost may anyone guide me or show me what to use

You need to be able to determine if a given char value is a digit. In header file cctype, function isdigit is precisely what you want. Here's one solution:

hasDigit = isdigit(passCode[0]) || isdigit(passCode[1]) || isdigit(passCode[2]);

If you don't yet know about the boolean operator || (or), think about using if statement(s).
 
Re: i am lost may anyone guide me or show me what to use

You could use
Code:
find_first_of
. There's an reference with an example here.
 
Re: i am lost may anyone guide me or show me what to use

johng said:
You need to be able to determine if a given char value is a digit. In header file cctype, function isdigit is precisely what you want. Here's one solution:

hasDigit = isdigit(passCode[0]) || isdigit(passCode[1]) || isdigit(passCode[2]);

If you don't yet know about the boolean operator || (or), think about using if statement(s).

is it possible to use if statements to solve this program
 
Last edited:
First you need to find if passCode[0] is a digit:
Code:
hasDigit=false;
if (isdigit(passCode[0])) {
  hasDigit=true;
}
I hope you see that the above code is equivalent to:
Code:
hasDigit=isdigit(passCode[0]);
Next you need to test if passCode[1] is a digit:
Code:
if (isdigit(passCode[1])) {
  hasDigit=true;
}
So the following code tests whether passCode[0] or passCode[1] is a digit:
Code:
hasDigit=isdigit(passCode[0]);
if (isdigit(passCode[1])) {
  hasDigit=true;
}
I hope you see that the above code is not the same as:
Code:
hasDigit=isdigit(passCode[0]);
hasDigit=isdigit(passCode[1]);
Now you can finish with one more if statement.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
2
Views
7K
Replies
1
Views
2K
Replies
3
Views
5K
Replies
1
Views
5K
Replies
22
Views
3K
Replies
5
Views
3K
Replies
8
Views
2K
Back
Top