Created
May 24, 2012 17:58
-
-
Save brand-it/2783135 to your computer and use it in GitHub Desktop.
Phone number validations made in C++ not very good but it works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <iomanip> | |
using namespace std; | |
char d1, d2, d3, d4, d5, d6, d7, d8; | |
char digitConvertion(char d){ | |
char value = d; | |
switch (toupper(d)){ | |
case 'A': case 'B': case 'C': | |
value = '2'; | |
case 'D': case 'E': case 'F': | |
value = '3'; | |
case 'G': case 'H': case 'I': | |
value = '4'; | |
case 'J': case 'K': case 'L': | |
value = '5'; | |
case 'M': case 'N': case 'O': | |
value = '6'; | |
case 'P': case 'Q': case 'R': case 'S': | |
value = '7'; | |
case 'T': case 'U': case 'V': | |
value = '8'; | |
case 'W': case 'X': case 'Y': case 'Z': | |
value = '9'; | |
} | |
return value; | |
} | |
bool isNumber(char& d){ | |
if(d == '1' || d == '2' || d == '3' || d == '4' || d == '5' || d =='6' || d == '7' || d == '8' || d == '9' || d == '0'){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
void whichOne(){ | |
if(!isNumber(d1)){ | |
cout << "The first digit is a not a number" << endl; | |
} | |
if (!isNumber(d2)){ | |
cout << "The Second digit is a not a number" << endl; | |
} | |
if (!isNumber(d3)){ | |
cout << "The Third digit is a not a number" << endl; | |
} | |
if (!isNumber(d5)) { | |
cout << "The Fifth digit is a not a number" << endl; | |
} | |
if(!isNumber(d6)){ | |
cout << "The Sixth digit is a not a number" << endl; | |
} | |
if (!isNumber(d7)){ | |
cout << "The Seventh digit is not a number" << endl; | |
} | |
if (!isNumber(d8)){ | |
cout << "The Ninth digit is not a number" << endl; | |
} | |
} | |
bool validNumber(){ | |
if(isNumber(d1) && isNumber(d2) && isNumber(d3) && isNumber(d5) && isNumber(d6) && isNumber(d7) && isNumber(d8)){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
void clean(){ | |
d1 = digitConvertion(d1); | |
d2 = digitConvertion(d2); | |
d3 = digitConvertion(d3); | |
d5 = digitConvertion(d5); | |
d6 = digitConvertion(d6); | |
d7 = digitConvertion(d7); | |
d8 = digitConvertion(d8); | |
} | |
string phoneToString(){ | |
string phone = ""; | |
phone += d1; | |
phone += d2; | |
phone += d3; | |
phone += d4; | |
phone += d5; | |
phone += d6; | |
phone += d7; | |
phone += d8; | |
return phone; | |
} | |
string firstThree(){ | |
string three; | |
three += d1; | |
three += d2; | |
three += d3; | |
return three; | |
} | |
void main() { | |
bool invalidPhone = true; | |
string phone; | |
cout << "Please type your phone number thanks you (Q to Quit): "; | |
while(invalidPhone){ | |
cin >> d1 >> d2 >> d3 >> d4 >> d5 >> d6 >> d7 >> d8; | |
if(d1 == 'Q'){ | |
invalidPhone = false; | |
} | |
clean(); | |
if(d4 != '-'){ | |
cout << "Your phone number should look like this 555-4444" << endl; | |
cout << "However your phone looks like this " << phoneToString() << endl; | |
} else if (d1 == '0') { | |
cout << "Your phone number can't begin with a zero" << endl; | |
} else if (firstThree() == "555"){ | |
cout << "Your phone number can't begin with 555" << endl; | |
} else if (!validNumber()) { | |
cout << "You used a invalid character please use a valid charcter" << endl; | |
whichOne(); | |
} else { | |
invalidPhone = false; | |
} | |
if(invalidPhone == true){ | |
cout << "Please try again (Q to Quit): "; | |
} | |
} | |
cout << "You have put in a valid phone number it is " << phoneToString() << endl; | |
system("pause"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment