C++ Programming HELP NEEDED ANYONE!

I may a program using strings where I can put in a phone number in it and then it will revise that phone to where it is correct or it will tell me if its wrong. Here is the code for it.

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
#include <iostream>
#include <string>
#include <ctype.h>

using namespace std;

bool checkDigits(string data, int start_ch, int end_ch) {
    for(int i = start_ch; i < end_ch; i++)
        if(!isdigit(data[i])) return false;
        return true;
}

bool checkPhoneNumber(string &data) {
    // function checks for phone numbers like 555-555-1234 of (555) 555 1234
    bool valid = true;
    string good_number = "";

    // verify that we have at least 12 characters to work with
    if (data.length() < 12) {
        data = "number is too short!";
        return false;
    }

    // get rid of those nasty parentheses
    if(data[0] == '(') {
        data.erase(0,1);
        data.erase(3,1);
    }

    // eliminate the two non-digit locations as well
    data.erase(3,1);
    data.erase(6,1);
    // check that the number is now all digits (10 of them)
    if(data.length() == 10) { // length is good
    // now check that all we have are digits
        for(int i=0;i<10;i++)
            if(!isdigit(data[i])) valid = false;
    } else {
        data = "too many characters";
        return false;
    }

    // if we have a valid number, put it in "normal" form
    if(valid) {
        good_number = data.substr(0,3) + ":" + data.substr(3,3) + ":" + data.substr(6,4);
        // now copy it back into the user's string
        data = good_number;
    } else
        data = "number not correctly formed";
    }

int main(int argc, char * argv[]) {
    string userdata;
    int num_chars = 0;
    bool valid;
    // get the number to check
    cout << "Enter your phone number: ";
        getline(cin,userdata);
    cout << "Validating '";
        num_chars = userdata.length();
    if (checkPhoneNumber(userdata)) {
        cout << "Good number: " << userdata << endl;
    } else {
        cout << "Error processing number: " << userdata << endl;
}
}


Now I need to change this where I don't have to input the phone numbers that I want to test. Now I need to figure out a way of using an array with stored phone numbers and then the string will take the number numbers that are stored and check them if they are true or false.
The stored phone numbers could be something like these.
• 5125551234
• 512 555 1234
• 512-555-1234
• 512.555.1234
• 555-1234

I really need help I have been trying to create an array on my own but errors come up all of the time. Some code will be useful please!!!
Can any one help?
@Ernesto Nunez

I got the program working by adding a couple more lines. After the changes, the if(valid) { section now looks like this;
1
2
3
4
5
6
7
8
9
10
// if we have a valid number, put it in "normal" form
	if(valid)
	{
		good_number = data.substr(0,3) + ":" + data.substr(3,3) + ":" + data.substr(6,4);
		// now copy it back into the user's string
		data = good_number;
		return true; // Added this
	} else
		data = "number not correctly formed";
	return false; // and this 

I think that fixes the problems you were having. If that wasn't it, please explain a little more on what problems you were having..
Ok. The main program was already made. What it did was, that it took a phone number that you would input into the computer and then it would check it to see if it was a valid phone number.

But know I have to change that program to where there are four different ways of inputting phone numbers like these:
• 5125551234
• 512 555 1234
• 512-555-1234
• 512.555.1234
• 555-1234

but this time these have to store in the program already. With out needing to input them.

Now I have been trying to input all of this into an array, but it is not working out at all.

That I my thing going. I really need some help with some code soon, please.
I want help to solve this problem. please help me anybody,

problem is this

You are required to provide a practical prototype in c++ for the following class diagram. Mean you will have to provide the solution in the form of classes in c++.


plz plz help me early, i have very short time. please....
Last edited on
I am rally not understanding why you are being sarcastic.

It really doesn't help.
Topic archived. No new replies allowed.