Need help with C++ String program

Hello I recently came across this problem for my programming class that requires me to build a program that takes in phone numbers in varying formats and out puts it as (xxx) xxx-xxxx . I've built something that works however my professor pointed out that my program should only include digits before formatting in case a user where to use other characters such as ' . ' . My questions is how do I get this to work with what I have now or do I need to start over from scratch?



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  #include <iostream>
#include <string>
using namespace std;

// main routine
int main()
{
	// local variables
	bool ValidNumber = false;
	string phoneNumber;

	while (ValidNumber == false)
	{
		// Ask the user for input
		cout << "Please enter your phone number " << endl;
		std::getline(cin, phoneNumber);

		ValidNumber = true;

		//  Validate the phone number
		for (std::string::iterator it = phoneNumber.begin(); it != phoneNumber.end();)
		{
			//  Check for characters to ignore
			if (*it == '(' || *it == ')' || *it == '-' || *it == ' ')
			{
				it = phoneNumber.erase(it);
			}
			//  Check for numbers since we really want to keep them
			else if (*it >= '0' || *it <= '9')
			{
				++it;
			}
			//  Check for invalid characters not removed
			else
			{
				ValidNumber = false;
			}
		}

		//  phone number must have 10 digits
		if (phoneNumber.size() != 10)
		{
			ValidNumber = false;
		}

		if (ValidNumber == false)
		{
			cout << "The phone number must have 10 digits.\n";
		}
	}


	//  Split the phone numbers
	std::string areacode;
	std::string prefix;
	std::string number;

	areacode = phoneNumber.substr(0, 3);
	prefix = phoneNumber.substr(3, 3);
	number = phoneNumber.substr(7, 4);

	std::cout
		<< "The properly formatted number is: ("
		<< areacode
		<< ") "
		<< prefix
		<< '-'
		<< number
		<< '\n';

	return 0;
}
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/cctype/isdigit/
This is what i came up with as an answer to my previous worries, however now it deletes the 7th digit for some unknown reason. Please help!!

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>

using namespace std;

// main routine
int main()
{
	// local variables
	bool ValidNumber = false;
	string phoneNumber;

	while (ValidNumber == false)
	{
		// Ask the user for input
		cout << "Please enter your phone number " << endl;
		std::getline(cin, phoneNumber);

		ValidNumber = true;
		
		phoneNumber.erase(
			std::remove_if(phoneNumber.begin(), phoneNumber.end(), &isalpha),
			phoneNumber.end());
		phoneNumber.erase(
			std::remove_if(phoneNumber.begin(), phoneNumber.end(), &ispunct),
			phoneNumber.end());
		//  Validate the phone number
		for (std::string::iterator it = phoneNumber.begin(); it != phoneNumber.end();)
		{
			//  Check for characters to ignore
			if (*it == '(' || *it == ')' || *it == '-' || *it == ' ')
			{
				it = phoneNumber.erase(it);
			}
			//  Check for numbers since we really want to keep them
			else if (*it >= '0' || *it <= '9')
			{
				++it;
			}
			//  Check for invalid characters not removed
			else
			{
				ValidNumber = false;
			}
		}

		//  phone number must have 10 digits
		if (phoneNumber.size() != 10)
		{
			ValidNumber = false;
		}

		if (ValidNumber == false)
		{
			cout << "The phone number must have 10 digits.\n";
		}
	}


	//  Split the phone numbers
	std::string areacode;
	std::string prefix;
	std::string number;

	areacode = phoneNumber.substr(0, 3);
	prefix = phoneNumber.substr(3, 3);
	number = phoneNumber.substr(7, 4);

	std::cout
		<< "The properly formatted number is: ("
		<< areacode
		<< ") "
		<< prefix
		<< '-'
		<< number
		<< '\n';

	return 0;
}
closed account (48T7M4Gy)
Why does it need to be so complicated? I'd start from scratch :)

Let the user type in whatever they like.
Store the input as a string.
Create a new string made up of only the digits.
Accept the string if it has 10 characters (digits).
Then apply the 'proper' formatting.
For the life of me I cant figure out how to copy the string but with only the digits. What I have now I've pieced together. and i'm so close this would work if it didn't delete the 7th digit for whatever reason...
closed account (48T7M4Gy)
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
#include <iostream>

//  (xxx) xxx-xxxx
int main()
{
    std::string line;
    std::string raw_number;
    
    std::getline(std::cin, line);
    
    for(int i = 0; i <line.length(); i++)
    {
        if(isdigit(line[i]))
            raw_number += line[i];
    }
    
    if(raw_number.length() == 10)
    {
        std::cout
        << "Properly formatted number is: "
        << '(' << raw_number.substr(0,3) << ')'
        << ' ' << raw_number.substr(3,3)
        << '-' << raw_number.substr(6,4)
        << '\n';
    
    }
    else
        std::cout << "wrong number\n";
    
    return 0;
    
}
1234567890
Properly formatted number is: (123) 456-7890
Program ended with exit code: 0
(def890)
wrong number
Program ended with exit code: 0


Whatever you decide to do if you look closely at mine you'll probably solve the missing digit. :)
Last edited on
[Wrong Answer]
Last edited on
however now it deletes the 7th digit for some unknown reason

The wrong offset here:
 
    number = phoneNumber.substr(7, 4);
should be
 
    number = phoneNumber.substr(6, 4);


Instead of being concerned with all the many types of characters which are not valid, wouldn't it make sense to check for digits only?
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
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>

bool notdigit(char ch) { return ! std::isdigit(ch); }

int main()
{
    // local variables
    bool ValidNumber = false;
    std::string phoneNumber;

    while (!ValidNumber)
    {
        // Ask the user for input
        std::cout << "Please enter your phone number " << std::endl;
        std::getline(std::cin, phoneNumber);

        ValidNumber = true;
        
        // remove anything except digits
        phoneNumber.erase(
            std::remove_if(phoneNumber.begin(), phoneNumber.end(), notdigit ),
            phoneNumber.end());

        //  phone number must have 10 digits
        if (phoneNumber.size() != 10)
        {
            ValidNumber = false;
        }

        if (!ValidNumber)
        {
            std::cout << "The phone number must have 10 digits.\n";
        }
    }

    //  Split the phone numbers
    std::string areacode = phoneNumber.substr(0, 3);
    std::string prefix   = phoneNumber.substr(3, 3);
    std::string number   = phoneNumber.substr(6, 4);

    std::cout
        << "The properly formatted number is: ("
        << areacode
        << ") "
        << prefix
        << '-'
        << number
        << '\n';

}
WOW!!! I can't believe I missed that... (6,4).. sigh.. Thank you so much for you help and patience.
Topic archived. No new replies allowed.