I am writing a program that outputs a digit based off of the letter entered and am stuck on the two most important parts. My professor wants me to output a hypen(-) after the third digit and LIMIT the amount of letters I can enter to 7. Now I know the getline function is what i've learned so far and the if statement for the hypen will be required I am just confused on where the placement would be useful and hints would be very useful.
I will be using getline(cin, letter) letter is my variable in this situation.
Below is the if statement I think would be suitable I just don't know where they would go and it's getting to me.
if (i==3)
cout << "-";
else
cout << "error" << endl;
Below is my work done so far and i've only just started for a couple of months I hope im on the right track.
while (letter != '#')
{
cout << "The letter you entered is: "
<< letter << endl;
cout << "The corresponding telephone "
<< "digit is: ";
if (letter >= 'A' && letter <= 'Z' || letter >= 'a' && letter <= 'z')
switch(letter)
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
cout << 2 << endl;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
cout << 3 << endl;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
cout << 4 << endl;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
cout << 5 << endl;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
cout << 6 << endl;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
cout << 7 << endl;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
cout << 8 << endl;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
cout << 9 << endl;
break;
}
else
cout << "Invalid output. " << endl;
cout << " Continue by pressing another letter." << endl;
cout << "To stop the program remember to input #." << endl;
Did I need to use a switch in this instance, this would be more efficiently handled with a series of if like the initial check before the switch statement, what you have is equivalent to about 52 if statements which would be in 18 if's checking for range if I didn't normalize my input.
I am assuming I want to stop entering letters with the '#' key. I would have normalized input with a function called tolower(char inChar) or toupper(char inChar) out of the cctype library of c++. Reference http://www.cplusplus.com/reference/clibrary/cctype/
The last piece you were asking about is two variables. One to count the digits in a section of the phone number, and another to keep track of the section. The first two sections are 3 digits long and the last section is 4. They would be defined and initialized outside the while loop and would be incremented inside the while loop. I would track when someone has entered a completed number. This is why my thoughts have two variables. this might be where I use a switch statement.