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

  • Context: C/C++ 
  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    C++ Numeric String
Click For Summary

Discussion Overview

The discussion revolves around determining if a string contains any numeric digits in C++, specifically within a 3-character passCode. Participants explore various methods to achieve this without using loops, focusing on the use of functions from the cctype header and conditional statements.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant suggests setting hasDigit to true if any character in passCode is a digit, using the isdigit function from cctype.
  • Another participant proposes using the find_first_of method as an alternative approach, referencing an example.
  • There is a reiteration of using the isdigit function, with a focus on checking each character individually and the implications of using boolean operators.
  • A participant clarifies that the code testing passCode[0] and passCode[1] for digits is not equivalent to simply checking each character independently without considering the logical conditions.

Areas of Agreement / Disagreement

Participants express various methods to solve the problem, but there is no consensus on a single approach. Multiple competing views on how to implement the solution remain present.

Contextual Notes

Some participants mention the limitations of not using loops and the necessity to understand boolean logic and conditional statements, but these aspects remain unresolved in terms of their implications for the overall solution.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
51K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 1 ·
Replies
1
Views
5K
Replies
12
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K