Hello zeck239,
Let us start with:
Hello, I'm working on this problem for my C++ class. The question is to write a program that does the following:
1. Prompts a user to enter Y or y to begin the conversion, or any other input to quit. Covered.
2. Prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone
number in digits Covered. Partly
3. Processes only the first seven letters if the user enters more than seven letters. Not covered.
Also you will need t account for a space or "-".
4. Outputs the – (hyphen) after the third digit. Covered.
5. It allows the user to use both uppercase and lowercase letters as well as spaces between words. Covered.
6. process as many telephone numbers as the user wants while allowing them to quit after each conversion. Not covered.
|
For point 2, like I said, you are trying to enter a string into a single character. The variable type and how you input the data needs to be changed. That is why point is only partly correct.
Point 3 is not dealt with at all. You need to check if the entered number contains a space or a "-". This will let you know if the final string should be 7 or 8 characters long. This needs to be done before you enter the for loop. I used the "string" function
.find_first_of(" -")
for this. Next I used the
.resize(8)
to eliminate anything beyond 7 or 8 characters.
For point 6 yo need to ask the user if she/he would like to continue to either keep the value of "run" or change it to end the while loop. As is the while loop is an endless loop.
Based on the instructions, if I understand them correctly, I have done this:
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
|
#include <cctype> // <--- std::tolower() and std::toupper() and others. http://www.cplusplus.com/reference/cctype/
#include <iostream>
#include <string>
using namespace std;
int main()
{
char run{ 'Y' };
//std::string phoneNumber;
// <--- Change comments for testing. Comment all or delete when finished.
//std::string phoneNumber{ "abcdefg" };
std::string phoneNumber{ "abc defg" };
//std::string phoneNumber{ "abc-defg" };
//std::string phoneNumber{ "jklmnop" };
//std::string phoneNumber{ "abc defghij" };
//cout << "\n If you wish to translate a phone number press 'Y' or 'y',\n If not press any other key to exit this program. ";
//cin >> run;
while (std::toupper(run) == 'Y')
{
//cout << "Please enter the phone number in letters.: ";
//cin >> phoneNumber;
cout << "\n Please enter the phone number in letters.: " << phoneNumber << endl; // <--- Added for testing. Comment or remove when done.
size_t pos = phoneNumber.find_first_of(" -");
if (pos != std::string::npos)
phoneNumber.resize(8);
else
phoneNumber.resize(7);
std::cout << "\n The number is: "; // <--- Added.
for (size_t i = 0; i < phoneNumber.size(); i++)
|
Some of the code that is commented out is done for testing. This way you do not have to enter something each time you test the program. It will not hurt to uncomment the prompt and input lines 24 and 25 as it will over wright the initialized variable. Line 27 would then need a comment.
Lines 29 - 34 can be changes to just an if statement if you are not familiar with the ".find" functions.
In
Dutch's example for the switch using the "std::toupper()" function is just another way of dealing with lower case letters. Your original switch or his example will work.
I did not find anything wrong with your switch. It works when you have the right input to work with. What I used is
switch (std::toupper(phoneNumber[i]))
Andy