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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
// Test.cpp : main project file.
#include "stdafx.h"
using namespace System;
/* the ToDigit() method takes the user input and converts it to uppercase, then converts letters to numbers and then returns
*/
Int32 ToDigit(Char %digit)
{
// converts the variable to uppercase
digit = Char::ToUpper(digit);
// switch statement that converts the letters to numbers
switch (digit)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
case 'A':
case 'B':
case 'C':
digit = '2';
break;
case 'D':
case 'E':
case 'F':
digit = '3';
break;
case 'G':
case 'H':
case 'I':
digit = '4';
break;
case 'J':
case 'K':
case 'L':
digit = '5';
break;
case 'M':
case 'N':
case 'O':
digit = '6';
break;
case 'P':
case 'Q':
case 'R':
case 'S':
digit = '7';
break;
case 'T':
case 'U':
case 'V':
digit = '8';
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
digit = '9';
break;
default:
return -1;
}
return 0;
}
/* The ReadDials() method prompts the user to input a number, send the number to the
* ToDigit() method which converts letters to numbers and validates the input.
*/
Int32 ReadDials(Char %D1, Char %D2, Char %D3, Char %D4, Char %D5, Char %D6, Char %D7, Char %D8)
{
// local variables
Int32 returnValue;
// prompts for user input
Console::Write("Enter a phone number (Q to quit): ");
D1 = Convert::ToChar(Console::Read());
// checks is input has been given to quit the program
if (Char::ToUpper(D1) == 'Q')
return -5;
// stores the input into the variables
D2 = Convert::ToChar(Console::Read());
D3 = Convert::ToChar(Console::Read());
D4 = Convert::ToChar(Console::Read());
D5 = Convert::ToChar(Console::Read());
D6 = Convert::ToChar(Console::Read());
D7 = Convert::ToChar(Console::Read());
D8 = Convert::ToChar(Console::ReadLine());
// beginning here each value is sent to the ToDigit() method to be convert to an upper case letter, then to a digit, and whether it is a valid digit.
returnValue = ToDigit(D1);
if (returnValue == -1)
return returnValue;
returnValue = ToDigit(D2);
if (returnValue == -1)
return returnValue;
returnValue = ToDigit(D3);
if (returnValue == -1)
return returnValue;
if (D4 != '-')
return -4;
returnValue = ToDigit(D5);
if (returnValue == -1)
return returnValue;
returnValue = ToDigit(D6);
if (returnValue == -1)
return returnValue;
returnValue = ToDigit(D7);
if (returnValue == -1)
return returnValue;
returnValue = ToDigit(D8);
if (returnValue == -1)
return returnValue;
if (D1 == '0')
return -2;
if (D1 == '5' && D2 == '5' && D3 == '5')
return -3;
return 0;
}
/* The AckowledgeCall() method takes the input from the user after it has been converted and verified
* and displays in on the console in numbers.
*/
void AcknowledgeCall(Char D1, Char D2, Char D3, Char D4, Char D5, Char D6, Char D7, Char D8)
{
Console::WriteLine("Phone number Dialed: " + D1 + D2 + D3 + D4 + D5 + D6 + D7 + D8);
Console::WriteLine();
}
int main()
{
// initializing variables
Char D1 = ' ',
D2 = ' ',
D3 = ' ',
D4 = ' ',
D5 = ' ',
D6 = ' ',
D7 = ' ',
D8 = ' ';
int returnValue;
// Loop that sends values by reference to ReadDials() method and compares the return value to see if they are valid.
do
{
returnValue = ReadDials(D1, D2, D3, D4, D5, D6, D7, D8);
if (returnValue == -5)
break;
switch (returnValue)
{
case -1:
Console::WriteLine("Error - An invalid Character was entered");
break;
case -2:
Console::WriteLine("ERROR - Phone number cannot begin with 0.");
break;
case -3:
Console::WriteLine("ERROR - Phone number cannot begin with 555.");
break;
case -4:
Console::WriteLine("ERROR - Hyphen is not in the correct position.");
break;
default:
AcknowledgeCall(D1, D2, D3, D4, D5, D6, D7, D8);
break;
}
Console::WriteLine();
} while (true);
return 0;
}
|