I need to create a C++ program that the user type letter and the program convert the letter into a telephone number including the hyphen.
Can anyone show me how to cast uppercase and lowercase alphabet letters to covert in number ( like the Get-loan will convert in some number )
It has to be short without case A, Case B... I think I gotta use static_cast to do that but i don't know how. Please help!!!
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
char startProgram;
while(true) // This while(run = true) is incorrect, it will always be true, change to while(run) or while(true)
{
cout <<"\nEnter Y or y to convert phone number from letters to numbers \n(enter any other letter to quit)"<<endl;
cin >> startProgram;
cin.ignore(10000,'\n');
if(startProgram=='Y'|| startProgram=='y')
{
cout << "\nEnter the letters you would like your phone number to represent: "<<endl;
getline(cin, letters);
cout << "\nThe corresponding number is: ";
// No need for these operations:
//length = letters;
//maxLength = length.length();
// This doesnt make sense:
//if(maxLength > 8)
//{
//maxLength = 7;
//}
for(unsignedint i=0; i <= 7 ; i++) // You could just set it to <= 7 or < 8
// There is a function from the string class that gives you the size letters.length()
{
// Put this if BEFORE the switch statement, if its AFTER,
// it will first do the operations on the switch and then check
if(i==3)
cout<<'-'; // If you are only outputting a single character use single quotes
switch(letters[i])
{
case'A':
case'a':
case'B':
case'b':
case'C':
case'c':
cout << 2;
break;
case'D':
case'd':
case'E':
case'e':
case'F':
case'f':
cout << 3;
break;
case'G':
case'g':
case'H':
case'h':
case'I':
case'i':
cout << 4;
break;
case'J':
case'j':
case'K':
case'k':
case'L':
case'l':
cout << 5;
break;
case'M':
case'm':
case'N':
case'n':
case'O':
case'o':
cout << 6;
break;
case'P':
case'p':
case'Q':
case'q':
case'R':
case'r':
case'S':
case's':
cout << 7;
break;
case'T':
case't':
case'U':
case'u':
case'V':
case'v':
cout << 8;
break;
case'W':
case'w':
case'X':
case'x':
case'Y':
case'y':
case'Z':
case'z':
cout << 9;
break;
// I need to remove all these cases and make one short program: '
//case ' ':
//cout << " ";
default:
// Just do a break here
//cout <<"Now quitting..."<<endl;
break;
}
}
}
elsebreak;
}
cout << "Now quitting..." << endl;
//system("pause");
return 0;
}