Hello GamerAid,
Just a quick note: your code:
1 2 3
|
cout << "Enter a string to be converted: " << flush;
cin >> phoneLetters;
|
The "flush" although better than "endl" is not needed because the following "cin" will "flush" the output buffer before it takes your input. Not 100% sure, but I believe that the "input " and "output" buffers use the same memory.
In a sense you are making more work for your-self than you need to when you can use what you have.
I have included the whole program so you can see the comments I have put in.
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include <cctype>
#include <iostream>
//#include <iomanip> // <--- Not needed or used in this program. Will not hurt if you leave it.
#include <limits> // <--- Added for the end of "main", but may not be needed.
#include <string>
using namespace std;
int main()
{
int digit;
//char letter[8]{}; // <--- Not needed.
//string phoneNum; // <--- Not needed or used.
// <--- Change comments of any two lines to test different letters.
//string phoneLetters{"abc defg"};
string phoneLetters{ "hijklmn" };
//string phoneLetters{ "opq rstu" };
//string phoneLetters{ "vwx-yzab" };
//std::string phoneLetters; // <--- Original definition. Use when finished testing.
cout << "Program that converts letters into a phone number, " <<
"\nbased on the T9 numeric keypad.\n\n";
cout << "Enter a string to be converted: " << phoneLetters << "\n\n"; // <--- Changed for testing. Remone the ("\n\n")s when finished.
//std::getline(std::cin, phoneLetters); // <--- Commented for testing.
for (size_t i = 0; i < phoneLetters.size(); i++)
{
phoneLetters[i] = std::toupper(phoneLetters[i]);
if (isspace(phoneLetters[i]) || phoneLetters[i] == '-')
{
std::cout << '-';
continue;
}
digit = ((phoneLetters[i] - 'A') / 3) + 2;
if ((phoneLetters[i] == 'S' || phoneLetters[i] == 'V' || phoneLetters[i] == 'Y' || phoneLetters[i] == 'Z'))
{
digit--;
}
if (i == 3)
cout << "-";
cout << digit;
}
// Use or delete your choice.
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point for testing.
}
|
In line 30 the ".size()" function returns a "size_t", an alias for "unsigned int". It helps, and avoids the warning of a type difference, when the types match. Using this is better than the "7" you started with because that size of the string could be 7 or 8 or maybe more. This will check the whole string no matter what the size is.
Line 32 changes the string before you start processing. Should that element be an upper case letter or something other than a lower case letter nothing is changed.
In line 43 these are the only 4 letters that become a problem based on the T9 key pad.
The rest of the program works as is.
Andy