Char D1

Only available on StudyMode
  • Topic: Char D1, Char D2, Caterpillar Inc.
  • Pages : 6 (599 words )
  • Download(s) : 231
  • Published : January 15, 2013
Open Document
Text Preview
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int ReadDials (char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8); int ToDigit (char &digit);
void AcknowledgeCall (char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8);

int main ()
{
// Char variables for the 8 digits of the phone number.
char d1, d2, d3, d4, d5, d6, d7, d8;

while (1)
{
int errorCode = ReadDials(d1, d2, d3, d4, d5, d6, d7, d8);

if (errorCode == -5)
break; // Exit the while loop.

switch(errorCode)
{
case -1:
cout << "ERROR - An invalid character was entered" << endl << endl; break;

case -2:
cout << "ERROR - Phone number cannot begin with 0" << endl << endl; break;

case -3:
cout << "ERROR - Phone number cannot begin with 555" << endl << endl; break;

case -4:
cout << "ERROR - Hyphen is not in the correct position" << endl << endl; break;

default:
AcknowledgeCall(d1, d2, d3, d4, d5, d6, d7, d8); break;
}
}

char ch, line[80];
cin.getline(line, 80);

cout << "Press any key to continue...";
cin.get(ch);

return 0;
}

int ReadDials (char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8) {
cout << "Enter a phone number (Q to quit): ";
cin >> d1; // Input the first digit.

if (d1 == 'Q')
return -5; // User wants to quit.

// Input the rest of the phone number.
cin >> d2 >> d3 >> d4 >> d5 >> d6 >> d7 >> d8;

// Convert each input character, except fourth input character, to // corresponding digit.
if (ToDigit(d1) == -1)
{
return -1;
}

if (ToDigit(d2) == -1)
{
return -1;
}

if (ToDigit(d3) == -1)
{
return -1;
}

if (ToDigit(d5) == -1)
{
return -1;
}

if (ToDigit(d6) == -1)
{
return -1;
}

if (ToDigit(d7) == -1)
{
return -1;
}

if (ToDigit(d8) == -1)
{
return -1;
}

// Fourth input character is expected to be a hyphen.
if (d4 != '-')
return -4;

// Valid number can not start with a 0.
if (d1 == '0')
return -2;

// Valid number can not start with 555.
if ((d1 == '5') && (d2 == '5') && (d3 == '5')) return -3;

// Valid number input by user.
return 0;
}

int ToDigit (char &digit)
{
digit = toupper(digit);

switch(digit)
{
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
// A valid numeric character; no transformation needed. 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' :...
tracking img