Phone number verification problem

My code asks the user to type in a 10 digit phone number. If its less than or more than 10 it will tell you and prompt you to enter again. If you type in letters it will prompt you to try again with digits. I have a separate text file which includes a bunch of valid area codes. However, no matter how I set up my do while loop and function to validate the area code, it will not work properly. Now matter how I set up my conditions it will say valid area codes aren't valid and sometimes that non valid area codes ARE valid. I know I'm opening and reading in the file correctly. Anyone else know what's up with my code? If you need the text file let me know.

Thank you

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

//project reads in a phone number and formats it
string formatphoneNum(string phoneNum);
bool phoneVerify(string phoneNum);
bool validAreaCode(string area, string areaCodes[], int size);

int main()
{
	string phoneNum;
	string restOfLine;
	string areaCodes[500];
	string area = phoneNum.substr(0, 3);

	ifstream areaCodesFile;
	areaCodesFile.open("C:/Users/Dylan/Desktop/areaCodes.txt");
	if (!areaCodesFile.is_open())
	{
		cout << "File Not Found. Try Again" << endl;
		exit(1);
	}

	int i = 0; int count = 0;

	while (!areaCodesFile.eof())
	{
		areaCodesFile >> areaCodes[i]; i++;
		getline(areaCodesFile, restOfLine);
		count++;
	}

//loops until you type in ten digits
	cout << "Please enter your 10 digit phone number: ";
	getline(cin, phoneNum);

	do{
		if (phoneVerify(phoneNum) == 0)
		{
			cout << "Please enter digits only: ";
			getline(cin, phoneNum);
			string area = phoneNum.substr(0, 3);
		}

		else if (phoneNum.length() != 10)
		{
			cout << "Wrong length, please try again: ";
			getline(cin, phoneNum);
			string area = phoneNum.substr(0, 3);
		}

		else if (validAreaCode(area, areaCodes, count) == 0)
		{
			cout << "Area code is not valid, try again: ";
			getline(cin, phoneNum);
			string area = phoneNum.substr(0, 3);
		}


} while (phoneVerify(phoneNum) == 0 || phoneNum.length() != 10 || validAreaCode(area, areaCodes, count) == 0);

//prints the formatted phone number
	cout << "Your number formatted is: " << formatphoneNum(phoneNum) << endl;
}
//function used to format user's info
string formatphoneNum(string phoneNum)
{

	string area;
	string exchange;
	string number;

	area = phoneNum.substr(0, 3);
	exchange = phoneNum.substr(3, 3);
	number = phoneNum.substr(6, 4);

	return '(' + area + ") " + exchange + '-' + number;
}
//function used to verify length and digit usage
bool phoneVerify(string phoneNum)
{
	bool check = true;
	for (int i = 0; i < phoneNum.length() && check; i++)
		if (phoneNum[i] < '0' || phoneNum[i] > '9')
			check = false;
	return check;
}
//function used to verify valid area code
bool validAreaCode(string area, string areaCodes[], int size)
{
	for (int i = 0; i < size; i++)
	{
		if (area == areaCodes[i])
			return true;
	}

	return false;
}

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
#include <iostream>
using namespace std;

bool isCorrect (string &s){
	if(s.length() !=10) 
		return false;
	for(auto x: s) {
		if(x >'9'||x <'0') 
			return false;
	}
	return true;
}

int main(){	
	string s;
	while(1){
		cout << "Enter a 10 digit number.\n";
		cin >> s;		
		if(isCorrect(s)) break;
		else {
			cout << "wrong input. try again.\n";
		}
	} 
		
	cout << "Congrats! you entered the number sucessfully!\n";	
return 0;
}
Yes, I would like to have text file.
In the meantime look into this:
1) If file is properly terminated with newline, then loop on line 27 will read one extra time and will read and store an empty string. You should not use eof() in loop condition.
2) If you will enter 10 digits in first time, then area string will not be calculated. It is value will be equal to given on line 16: an empty string. Combining with previous point, this will allow any area code to pass.
201 - Jersey City, NJ
202 - Washington, DC
203 - New Haven, CT
204 - Melita, MB
205 - Birmingham, AL
206 - Seattle, WA
207 - Portland, ME
208 - Boise, ID
209 - Modesto, CA
210 - San Antonio, TX
212 - New York, NY
213 - Los Angeles, CA
214 - Dallas, TX
215 - Philadelphia, PA
216 - Cleveland, OH
217 - Champaign, IL
218 - Duluth, MN
219 - Gary, IN
224 - Roselle, IL
225 - Baton Rouge, LA
228 - Gulfport, MS
229 - Albany, GA
231 - Muskegon, MI
234 - North Lima, OH
239 - Fort Myers, FL
240 - Gaithersburg, MD
248 - Royal Oak, MI
250 - Hendrix Lake, BC
251 - Mobile, AL
252 - Greenville, NC
253 - Tacoma, WA
254 - Waco, TX
256 - Huntsville, AL
260 - Kendallville, IN
262 - Racine, WI
267 - Doylestown, PA
269 - Bridgman, MI
270 - Bowling Green, KY
276 - Martinsville, VA
281 - Houston, TX
289 - Welland, ON
301 - Rockville, MD
302 - Wilmington, DE
303 - Denver, CO
304 - Charleston, WV
305 - Miami, FL
306 - Montmartre, SK
307 - Cheyenne, WY
308 - Grand Island, NE
309 - Peoria, IL
310 - Los Angeles, CA
312 - Chicago, IL
313 - Detroit, MI
314 - Saint Louis, MO
315 - Syracuse, NY
316 - Wichita, KS
317 - Indianapolis, IN
318 - Shreveport, LA
319 - Cedar Rapids, IA
320 - Saint Cloud, MN
321 - Melbourne, FL
323 - Los Angeles, CA
325 - Lueders, TX
330 - Akron, OH
331 - Oswego, IL
334 - Montgomery, AL
336 - Greensboro, NC
337 - Lafayette, LA
339 - Braintree, MA
340 - Charlotte Amalie, VI
347 - Ridgewood, NY
351 - Danvers, MA
352 - Gainesville, FL
360 - Vancouver, WA
361 - Corpus Christi, TX
385 - Provo, UT
386 - Daytona Beach, FL
401 - Providence, RI
402 - Omaha, NE
403 - High River, AB
404 - Atlanta, GA
405 - Oklahoma City, OK
406 - Billings, MT
407 - Orlando, FL
408 - San Jose, CA
409 - Beaumont, TX
410 - Baltimore, MD
412 - Pittsburgh, PA
413 - Springfield, MA
414 - Milwaukee, WI
415 - San Francisco, CA
416 - Toronto, ON
417 - Springfield, MO
418 - Saint Thecle, QC
419 - Toledo, OH
423 - Chattanooga, TN
424 - Lomita, CA
425 - Kirkland, WA
430 - Overton, TX
432 - Comstock, TX
434 - Lynchburg, VA
435 - St George, UT
440 - Cleveland, OH
442 - Oceanside, CA
443 - Pikesville, MD
450 - Coteau Landing, QC
458 - Oregon
469 - Plano, TX
470 - Georgia
475 - New Haven, CT
478 - Macon, GA
479 - Clarksville, AR
480 - Mesa, AZ
484 - Exton, PA
501 - Little Rock, AR
502 - Louisville, KY
503 - Portland, OR
504 - New Orleans, LA
505 - Albuquerque, NM
506 - Baie St Anne, NB
507 - Rochester, MN
508 - Worcester, MA
509 - Spokane, WA
510 - Oakland, CA
512 - Austin, TX
513 - Cincinnati, OH
514 - Saint Genevieve, QC
515 - Des Moines, IA
516 - Lynbrook, NY
517 - Lansing, MI
518 - Albany, NY
519 - Port Burwell, ON
520 - Tucson, AZ
530 - Chico, CA
539 - Tulsa, OK
540 - Roanoke, VA
541 - Eugene, OR
551 - Bayonne, NJ
559 - Fresno, CA
561 - West Palm Beach, FL
562 - Long Beach, CA
563 - Davenport, IA
567 - Toledo, OH
570 - Scranton, PA
571 - Herndon, VA
573 - Columbia, MO
574 - Tippecanoe, IN
575 - Las Cruces, NM
580 - Lawton, OK
585 - Rochester, NY
586 - Roseville, MI
601 - Jackson, MS
602 - Phoenix, AZ
603 - Manchester, NH
604 - Vancouver, BC
605 - Sioux Falls, SD
606 - London, KY
607 - Binghamton, NY
608 - Madison, WI
609 - Atlantic City, NJ
610 - Reading, PA
612 - Minneapolis, MN
613 - Pembroke, ON
614 - Columbus, OH
615 - Nashville, TN
616 - Grand Rapids, MI
617 - Boston, MA
618 - Belleville, IL
619 - San Diego, CA
620 - Hutchinson, KS
623 - Phoenix, AZ
626 - Alhambra, CA
630 - Naperville, IL
631 - Huntington, NY
636 - Harvester, MO
641 - Mason City, IA
646 - New York, NY
647 - Toronto, ON
650 - San Mateo, CA
651 - Saint Paul, MN
657 - Huntington Beach, CA
660 - Sedalia, MO
661 - Bakersfield, CA
662 - Tupelo, MS
671 - Agana Heights, GU
678 - Atlanta, GA
681 - Davy, WV
682 - Roanoke, TX
684 - Pago Pago, AS
701 - Fargo, ND
702 - Las Vegas, NV
703 - Alexandria, VA
704 - Charlotte, NC
705 - Mattice, ON
706 - Augusta, GA
707 - Santa Rosa, CA
708 - Oak Park, IL
709 - Norris Arm, NL
712 - Sioux City, IA
713 - Houston, TX
714 - Santa Ana, CA
715 - Eau Claire, WI
716 - Buffalo, NY
717 - Lancaster, PA
718 - Brooklyn, NY
719 - Colorado Springs, CO
720 - Denver, CO
724 - Greensburg, PA
727 - Saint Petersburg, FL
731 - Jackson, TN
732 - Toms River, NJ
734 - Ann Arbor, MI
740 - Zanesville, OH
747 - Los Angeles, CA
754 - Fort Lauderdale, FL
757 - Virginia Beach, VA
760 - Vista, CA
762 - Calhoun, GA
763 - Minneapolis, MN
765 - Muncie, IN
769 - Collins, MS
770 - Atlanta, GA
772 - Port Saint Lucie, FL
773 - Chicago, IL
774 - Foxboro, MA
775 - Reno, NV
778 - West Vancouver, BC
779 - Dekalb, IL
780 - Devon, AB
781 - Lynn, MA
785 - Topeka, KS
786 - Miami, FL
787 - Trujillo Alto, PR
801 - Ogden, UT
802 - Burlington, VT
803 - Columbia, SC
804 - Richmond, VA
805 - Santa Barbara, CA
806 - Lubbock, TX
807 - Dryden, ON
808 - Honolulu, HI
810 - Flint, MI
812 - Evansville, IN
813 - Tampa, FL
814 - Erie, PA
815 - Rockford, IL
816 - Kansas City, MO
817 - Fort Worth, TX
818 - Van Nuys, CA
819 - Ferme Neuve, QC
828 - Asheville, NC
830 - New Braunfels, TX
831 - Santa Cruz, CA
832 - Houston, TX
843 - Charleston, SC
845 - Spring Valley, NY
847 - Arlington Hts, IL
848 - Eatontown, NJ
850 - Tallahassee, FL
856 - Moorestown, NJ
857 - Quincy, MA
858 - San Diego, CA
859 - Lexington, KY
860 - Hartford, CT
862 - Hopatcong, NJ
863 - Lakeland, FL
864 - Greenville, SC
865 - Knoxville, TN
867 - Pangnirtung, NU
867 - Pangnirtung, NU
867 - Pangnirtung, NU
870 - Jonesboro, AR
872 - Chicago, IL
878 - Pennsylvania
901 - Memphis, TN
902 - River Herbert, NS
902 - River Herbert, NS
903 - Tyler, TX
904 - Jacksonville, FL
905 - Milton, ON
906 - Marquette, MI
907 - Anchorage, AK
908 - Plainfield, NJ
909 - San Bernardino, CA
910 - Fayetteville, NC
912 - Savannah, GA
913 - Kansas City, KS
914 - Yonkers, NY
915 - El Paso, TX
916 - Sacramento, CA
917 - New York, NY
918 - Tulsa, OK
919 - Raleigh, NC
920 - Green Bay, WI
925 - Walnut Creek, CA
928 - Yuma, AZ
929 - Flushing, NY
931 - Clarksville, TN
936 - Conroe, TX
937 - Dayton, OH
938 - Red Bay, AL
939 - Santurce, PR
940 - Wichita Falls, TX
941 - Sarasota, FL
947 - Ortonville, MI
949 - El Toro, CA
951 - Homeland, CA
952 - Minneapolis, MN
954 - Fort Lauderdale, FL
956 - Laredo, TX
970 - Ft Collins, CO
971 - Portland, OR
972 - Dallas, TX
973 - Newark, NJ
978 - Lowell, MA
979 - Bryan, TX
980 - Locust, NC
985 - Houma, LA
989 - Saginaw, MI
Just add this to notepad and save as a txt. file. Then change URL file lookup.
Part of your problems can probably be explained by the following warnings and errors:

1
2
3
4
5
6
7
8
9
main.cpp|23|error: ‘exit’ was not declared in this scope|
main.cpp|44|warning: declaration of ‘area’ shadows a previous local [-Wshadow]|
main.cpp|16|warning: shadowed declaration is here [-Wshadow]|
main.cpp|51|warning: declaration of ‘area’ shadows a previous local [-Wshadow]|
main.cpp|16|warning: shadowed declaration is here [-Wshadow]|
main.cpp|58|warning: declaration of ‘area’ shadows a previous local [-Wshadow]|
main.cpp|16|warning: shadowed declaration is here [-Wshadow]|
main.cpp||In function ‘bool phoneVerify(std::string)’:|
main.cpp|85|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|


After some debugging, I noticed that "area" will save a wrong area code and when it prompts you to add a valid one, the value of area doesn't change. I am not sure why nor what to do. Any help?
You should probably study the warning messages I posted, the second message is probably a big part of your problems.


Yeah that's definitely what the problem is I just cannot figure out how to fix it. I have the string declaration/initialization after each while statement so when the user enters a new phone number, a new area code is saved. However, each time that happens it won't change the value of area.
Look at the following snippet:
1
2
3
4
5
6
	do{
		if (phoneVerify(phoneNum) == 0)
		{
			cout << "Please enter digits only: ";
			getline(cin, phoneNum);
			string area = phoneNum.substr(0, 3);


Look at the last line above. Do you realize that you are creating a new instance of a string named area inside the if statement? And when that if statement finishes that variable is destroyed?

You already have a string named area, declared at about line 16, you don't need nor want to create a new instance, just use the original. To do this get rid of the "string" that precedes area. You're doing this several places in your code (the shadowed declarations).

Okay so all you had to do was tell me to get rid of the "string", because now it works with "area = phoneNum.substr(0, 3);" in each if statement. All that headache, and that was the only issue, you could've saved me hours if you came outright and just set get rid of "string." Thank you.
Topic archived. No new replies allowed.