Hello guys. I'm new to this site. I just want to know why can't I end the loop in my code. It's basically a telephone conversion code in which it prompts the user to enter words and it converts it to a phone number.
#include <iostream>
using namespace std;
int main()
{
int counter;
char phoneNumber;
char prompt = '\0';
if(phoneNumber >= 'A' && phoneNumber <= 'Z'
|| phoneNumber >= 'a' && phoneNumber <= 'z')
switch(phoneNumber)
{
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;
}
}
cout << endl;
cout << "Would you like to enter another number (Y/N)? ";
cin >> prompt;
}
cout <<endl<<endl;
system("pause");
return 0;
}
Enter a telephone number expressed in letters [ 7 letters maximum ]: rahmatr
your number is : 724-6287
Would you like to enter another number (Y/N)? y
Enter a telephone number expressed in letters [ 7 letters maximum ]: rahmatw
your number is : 724-6289
Would you like to enter another number (Y/N)? n
Thanks for the help guys. I have a question. Is it possible that the user can input more than 8 letters? The book says that if the program inputs "CALL HOME", it would produce the output for "CALL HOM" then ask the user if he/she wants to enter a number again. And lastly, is it possible to omit the system("cls") line? We haven't talked about it in class yet. Again thanks for the help guys.
yes you can omit system("cls") , but it cleans the console and attracts user.
however , choise is with you.
you can ask user that " your number contains how many digits ??? ".
then set that to the condition of loop.
like this.
1 2 3 4 5 6 7
int digits;
std::cout<<"your number contains how many digits ???";
std::cin>>digits;
// for(counter = 0; counter < 7; counter++){ // last loop
for(counter = 0; counter < digits; counter++){// new loop
To make telephone numbers easier to remember, some companies use letters
to show their telephone number. For example, using letters, the telephone
number 438-5626 can be shown as GET LOAN. In some cases, to make a
telephone number meaningful, companies might use more than seven letters.
For example, 225-5466 can be displayed as CALL HOME, which uses eight
letters. Write a program that prompts the user to enter a telephone number
expressed in letters and outputs the corresponding telephone number in digits. If
the user enters more than seven letters, then process only the first seven letters.
Also output the – (hyphen) after the third digit. Allow the user to use both
uppercase and lowercase letters as wellas spaces between words. Moreover, your
program should process as many telephone numbers as the user wants.
That's the problem given in the book. Is it possible that the user will just input a number more than 8 without asking them? And also, I would like to not use the system("cls") since we have not studied it yet but I don't know how to replace it with the right code.
So you would simply ignore the excess digits. In other words
1 2
for(int letter = 0; letter < 7; ++letter)
//7 should be a constant variable such as "max letters" or "max digits"
As far as the assignment does it explicitly say to append the digits to a string?
If not I would just do something like cout << (tolower(phoneNumber[digit]) - 'a') / 3 % 9; untested but in theory it should work.
*edit formula is off. working on a new one. Too exhausted to come up with a cleaver solution but you could do something like this (though not very pretty)
It's half the lines as before but I feel as if it could be majorly improved if I wasn't as tired :P I'm sure someone else can find it unless you need more help tomorrow.
The assignment is basically do a program out of the problem. However, this is under chapter 5 which is control structures (repetition). It's a beginning C++ class, so some terms are quite unfamiliar to me.
wow! im a beginner. real talk i dont understand all of these. hahaha. But i can make a program that can convert attendance, quiz, project, exam in final grade.