Data validation for vowels and consonants
May 12, 2014 at 1:45am UTC
Hey folks-
I'm trying to make a function that verifies if the char entered is consonant, if not, prompts user to enter another char and when it meets the criteria, return the char. I was able to do this using switch statements (which works, and i'll paste it below) but I wanted to know if there was an easier, perhaps more elegant solution for accomplishing this goal. Thanks for any 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
char isCons () // data validation for consonants
{
char userCons;
bool notCons = false ;
cout << "Please enter a consonant: " ;
cin >> userCons;
while (!notCons) {
switch (userCons) {
case 'b' : userCons = 'b' ;
notCons = true ;
break ;
case 'c' : userCons = 'c' ;
notCons = true ;
break ;
case 'd' : userCons = 'd' ;
notCons = true ;
break ;
case 'f' : userCons = 'f' ;
notCons = true ;
break ;
case 'g' : userCons = 'g' ;
notCons = true ;
break ;
case 'h' : userCons = 'h' ;
notCons = true ;
break ;
case 'j' : userCons = 'j' ;
notCons = true ;
break ;
case 'k' : userCons = 'k' ;
notCons = true ;
break ;
case 'l' : userCons = 'l' ;
notCons = true ;
break ;
case 'm' : userCons = 'm' ;
notCons = true ;
break ;
case 'n' : userCons = 'n' ;
notCons = true ;
break ;
case 'p' : userCons = 'p' ;
notCons = true ;
break ;
case 'q' : userCons = 'q' ;
notCons = true ;
break ;
case 'r' : userCons = 'r' ;
notCons = true ;
break ;
case 's' : userCons = 's' ;
notCons = true ;
break ;
case 't' : userCons = 't' ;
notCons = true ;
break ;
case 'v' : userCons = 'v' ;
notCons = true ;
break ;
case 'w' : userCons = 'w' ;
notCons = true ;
break ;
case 'x' : userCons = 'x' ;
notCons = true ;
break ;
case 'y' : userCons = 'y' ;
notCons = true ;
break ;
case 'z' : userCons = 'z' ;
notCons = true ;
break ;
case 'B' : userCons = 'B' ;
notCons = true ;
break ;
case 'C' : userCons = 'C' ;
notCons = true ;
break ;
case 'D' : userCons = 'D' ;
notCons = true ;
break ;
case 'F' : userCons = 'F' ;
notCons = true ;
break ;
case 'G' : userCons = 'G' ;
notCons = true ;
break ;
case 'H' : userCons = 'H' ;
notCons = true ;
break ;
case 'J' : userCons = 'J' ;
notCons = true ;
break ;
case 'K' : userCons = 'K' ;
notCons = true ;
break ;
case 'L' : userCons = 'L' ;
notCons = true ;
break ;
case 'M' : userCons = 'M' ;
notCons = true ;
break ;
case 'N' : userCons = 'N' ;
notCons = true ;
break ;
case 'P' : userCons = 'P' ;
notCons = true ;
break ;
case 'Q' : userCons = 'Q' ;
notCons = true ;
break ;
case 'R' : userCons = 'R' ;
notCons = true ;
break ;
case 'S' : userCons = 'S' ;
notCons = true ;
break ;
case 'T' : userCons = 'T' ;
notCons = true ;
break ;
case 'V' : userCons = 'V' ;
notCons = true ;
break ;
case 'W' : userCons = 'W' ;
notCons = true ;
break ;
case 'X' : userCons = 'X' ;
notCons = true ;
break ;
case 'Y' : userCons = 'Y' ;
notCons = true ;
break ;
case 'Z' : userCons = 'Z' ;
notCons = true ;
break ;
default : cout << "Invalid data. Please enter a consanant: " ;
cin >> userCons;
}
}
return userCons;
}
May 12, 2014 at 2:00am UTC
Untested, but I think this is right:
1 2
const std::string Vowels = "aeiou" ; // Screw the 'y'
bool isConsonant = (Vowels.find(std::tolower(userCons)) == std::string::npos);
Topic archived. No new replies allowed.