How to find characters in a string of numbers

I need to find characters in a string of numbers. Okay, the code looks slightly malicious (and poorly executed), but I made it as a joke for me and a friend (and some practice for me).

The problem starts in the first function I believe. It will find the letter if it is something like "a12345678"; however, if it is "1a2345678" it will not find it. I need it to be able to see a letter at any point during the string.

Furthermore, there's a weird problem where when I change the first function (is_number) to return "2" in the if statement it makes it to where it always sees a letter, even if I use 123456789 (and I don't have it using the "2" in any of the main code, just if it's == 1). Could someone explain the logic in this, I'd love to learn more (I'm currently a very basic coder).

Thanks for all your help!

(eventually I plan on having it email it to myself, and if you have any easy ideas on how to do that I'd appreciate that as well!)

Here's the code:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;
bool is_number(const string& s) 
{
	for (int i = 0; i < s.length(); i++)
		if (s[i] >= '0' && s[i] <= '9')
		{
			return 0;
		}
		else
		{
			return 1;
		}
} // if it is a character, it will return 1, if it is not, it will return 0.
bool is_alphabet(const string& s)
{
	for (int i = 0; i < s.length(); i++)
		if (s[i] >= '0' || s[i] < '0')
		{
			return 0;
		}
		else
		{
			return 1;
		}
} // if it is a number, it'll return 0, if it is not, it'll return 1.
void main()
{
	cout << "The Champman Experiments" << endl << endl;
	cout << "Please Enter Your Name: ";
	string name;
	cin >> name;
	cout << endl << "Hello " << name << ", could I have a last name? ";
	string lastname;
	cin >> lastname;
	cout << endl << "Thanks! Okay " << name << " " << lastname << ", for this project I need your Social Security Number!" << endl
		 << endl << "If you could please enter it here: ";
	string ssNumber;
	cin >> ssNumber;
	int alphaOrNum;
	alphaOrNum = is_number(ssNumber);
	int counts = 0;
	int counts2 = 0;
	while (counts != 9)
	{
		while (ssNumber.length() != 9)
		{
			cout << "The Social Security Number is not equal to nine digits. Do not lie to me. This is very serious. Please enter your social security number." << endl;
			cout << endl << endl << "Enter Number Here: ";
			cin >> ssNumber;
		}
		while (alphaOrNum == 1 || counts2 != 9)
		{
			counts2++;
			if (alphaOrNum == 1)
			{
				cout << "Please Enter Social Security Number in NUMBERS, not the ALPHABET. Please Enter Number Here: ";
				cin >> ssNumber;
				alphaOrNum = is_number(ssNumber);
			}
		}
		counts++;
	}
	cout << endl << "Thanks! Have a great day" << endl << endl;
	ofstream f("ssData.txt");
	if (f.fail())
	{
		system("pause");
	}
	f << name << " " << lastname << " " << ssNumber;
	system("pause");
} // asks user for name, last name, social security number. checks number to make sure it is 9 
//   characters and that only numbers are used. Saves results to ssData.txt
//---------------------------------------------------------------------------------------------------
//   Will eventually update to email results to chainmailgrb@gmail.com


//fix the fact that if you put it in 1a2345678 or 12345678a it doesn't stop it -- think that
//the thing needs to be an array (with strings)
Last edited on
This thread has various approaches to extracting digits from strings that should get you started and, hopefully, finished on the related task of extracting characters: http://www.cplusplus.com/forum/beginner/218649/#msg1008108
Last edited on
Somehow it's not really clear what you want to do.
I need to find characters in a string of numbers.
That's quite easy to do, but what then? Remove character??

1
2
3
4
5
6
7
8
bool is_number(const string& s) 
{
  for (int i = 0; i < s.length(); i++)
   if (s[i] >= '0' && s[i] <= '9')
   {
	return 0;
   }
}
This will check only the first character of the string, same problem with is_alphabet
Last edited on
Hey Thomas1965,

Could you explain why it only checks the first character of the string? That's the part I'm having trouble with. I just want it to detect if there is a character in the string given; however, I am having trouble doing it past the first character. So a12345678 will be detected, but 1a2345678 will not and I am not sure how to make it to where it does check the second one.

If you could help with this I would greatly appreciate it.

Thanks!

Blake.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
bool is_number(const string& s)
{
  for (int i = 0; i < s.length(); i++)
    if (s[i] >= '0' && s[i] <= '9')
    {
      return 0;
    }
    else
    {
      return 1;
    }
}

The problem is the return statement after the if.
The loop starts with i = 0;
In the if statement you check s[0], if it is a digit you return 0 otherwise 1. In both cases the for loop ends. If the character is at the first place it works, otherwise not.
A common pattern is to return from the loop when the condition is wrong returning false.
At the end of the function return true.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
In case you are not allowed to use isdigit in <cctype>
bool isDigit(const char ch)
{
   return (ch >= '0' && ch <= '9');
}

bool is_number(const string& s)
{
  for (int i = 0; i < s.length(); i++)
  {
    if (!isDigit(s[i])
    {
      return false;
    }
  }
  return true; // all are digits
}
That helps a ton! Thanks Thomas.
Topic archived. No new replies allowed.