How to detect if only numerical digits are present in strings.


Write a program which accepts a phone number in the form of a string and detects whether it's valid.
The phone number is 12 digits long expressed as ###-###-####
Detects if dash is not present
Detects that numerical digits are present in all # slots

I need help in the "Detects that numberical digits are present in all # slots" protocol.

#include <iostream>
#include <string>
using namespace std;
int main()
{
string phoneNumber;
cout<<"Enter your phone number in the form of ###-###-####: "<<endl;
cin>>phoneNumber;
if (phoneNumber[3]!='-' || phoneNumber[7]!='-')
{
cout<<"Not Valid";
}
}
integralfx's (implied) suggestion is definitely good for a homework assignment.

If you wanted to take this further - and if you are permitted to use C++11 - then you might like to consider "regular expressions":
http://www.cplusplus.com/reference/regex/
This is big in languages like java, and it's a welcome inclusion in the C++ language.
Thanks for your help thus far, but I am still rather confused...

Can you write out code the example you are using? I still cannot get it to detect when a phone number is all digits except for phoneNumber[3] and phoneNumber[7] in order to make a valid or invalid phone number.
You can loop through the individual characters of phoneNumber testing whether each (i.e. phoneNumber[i], where i is the loop variable) is a digit with isdigit( ) - just as integralfx suggested.

You probably need to check that phoneNumber has the requisite number of digits first - the string class has a member function (actually, two member functions) for doing that.

I think you would benefit more if I didn't just post code.



I've changed it a lot to the point of this. I can get it to say the phone number is invalid when the '-' are not present where they need to be but I still cant get it to detect all numbers...

#include <iostream>
#include <string>
using namespace std;
int main()
{
unsigned int i;
string phoneNumbe;
int value =atoi(phoneNumber.c_str());
cout<<"Enter your phone number in the format ###-###-####: "<<endl;
cin>>phoneNumber;
for (i=11; i<phoneNumber.size(); i++)
{
if (phoneNumber [3] == '=' && phoneNumber[7]=='=')
{
cout<<"Valid Phone Number";
break;
}
else
{
cout<<"Phone Number is Not Valid";
}
}
}
Why do you have a line saying int value = ...?

Why does your loop start with i = 11?

Why are you still not using isdigit()?
Why do you have a line saying int value =...?
In an attempt to use isdigit() i received an error saying that it is not possible to change a string to an int.

Why does your loop start with i=11?
I only want one cout stating that the phone number is valid or invalid, if i started with i=0 it means that i would receive 12 cout statements. It was a quick fix and i realize that I make it (i=0; i<1; i++)

Why are you still not using isdigit()?
I can not determine if it should be in the 'for' statement or out of it and i cannot seem to get that code to work properly for what i need.

I am rather new to this and i realize this code is giving me more problems than it probably should.
closed account (ybf3AqkS)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <cctype>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string format = "###-###-####";
    cout << "Enter your phone number in the format " << format << ":\n";

    string phone_number;
    cin >> phone_number;

    if(phone_number.size() != format.size())
    {
        cout << "Bad format\n";
        return 0;
    }

    bool good = true;

    for(int i=0; i<format.size(); ++i)
    {
        if(format[i] == '#')
        {
            if(isdigit(phone_number[i]) == false)
            {
                good = false;
                break;
            }
        }
        else if(format[i] == '-')
        {
            if(phone_number[i] != '-')
            {
                good = false;
                break;
            }
        }
    }

    good ? cout << "Good format\n" : cout << "Bad format\n";
}

Topic archived. No new replies allowed.