Adding Area code in program

1.6k Views Asked by At

Basically, I got this program for one assignment. It asks for the any phone numbers that is 7 digits long such as 930-1892. then It'll say your digits as output. Now, I decided that I would prefer to add the area code to the 7 digits, totaling up to 10 digits long. I want it to output in format like (505)123-4567.

I am surprisingly struggling with it, i can't get the program to output the 10 digits including the area code instead of just 7 digits. But the program as now should compile and run and output the 7 digits phone number you added into.

#include<iostream>
#include<string>
using namespace std;

/* Global Variables */
int prefix[3];
int number[4];
string phoneNumber;

// main routine
int main()
{
  // local variables
  int input_done = 0; // flag to control input loop
  int ascii_0 = 48;
  int i = 0;

  while (input_done == 0)
  {
    // Ask the user for input
    cout << "Please enter your 7 digit phone number (for example: 123-4567)" << endl;
    getline(cin, phoneNumber);

    // split up the input and check it for validity
    if (phoneNumber.length()==8)
    {
      input_done = 1;  // assume number is correct until otherwise

      for (i = 0; i <= 7; i++)
      {
        if (i == 3)
        {
          if (phoneNumber[3] != '-')
            input_done = 0;
        }
        else
        {
          if ( (phoneNumber[i] < '0') || (phoneNumber[i] > '9') )
            input_done = 0;
        }
      }

      // assign values to individual array elements if checked out okay
      if (input_done == 1)
      {
        for (i = 0; i <= 2; i++)
        {
          prefix[i] = phoneNumber[i] - ascii_0;
          number[i] = phoneNumber[i+4] - ascii_0;
        }
        number[3] = phoneNumber[7] - ascii_0;
     }
    }

    if (input_done != 1)
      cout << "There is a problem with what you entered, please try again.\n";

  }

  // report
  cout << "I have your phone number as: ";

  for (i = 0; i<3; i++)
    cout << prefix[i];
  cout << "-";
  for (i = 0; i<4; i++)
    cout << number[i];
  cout << endl;

  return 0;
}
1

There are 1 best solutions below

0
On BEST ANSWER

First there are a few minor issues with the approach you have taken. First you use a lot of magic numbers, this makes your code difficult to maintain and evolve as you have to worry about updating more code increasing the chance of introducing bugs. The second is there isn't really necessary to convert the phone number into individual numbers, this just makes the values more difficult to manage in the long run. You're also expecting the hyphen to be in a specific location, again using magic numbers which will make it more difficult to add support for area codes. The following approach eliminates the use of magic numbers as well as using indexes and arrays and takes advantages of iterators.

#include <iostream>
#include <string>
using namespace std;

// main routine
int main()
{
    // local variables
    bool isValidNumber = false;
    string phoneNumber;

    while (isValidNumber == false)
    {
        // Ask the user for input
        cout << "Please enter your 10 digit phone number (i.e. 900-976-8008)" << endl;
        std::getline(cin, phoneNumber);

        isValidNumber = true;

        //  Validate and clean up the phone number
        for (std::string::iterator it = phoneNumber.begin(); it != phoneNumber.end();)
        {
            //  Check for characters we want to ignore
            if (*it == '(' || *it == ')' || *it == '-' || *it == ' ')
            {
                it = phoneNumber.erase(it);
            }
            //  Check for numbers since we really want to keep them
            else if (*it >= '0' || *it <= '9')
            {
                ++it;
            }
            //  Check characters that are considered invalid
            else
            {
                isValidNumber = false;
            }
        }

        //  Make sure the number of required digits are present. Add an additional
        //  check for numbers without area codes if you like.
        if (phoneNumber.size() != 10)
        {
            isValidNumber = false;
        }

        if (isValidNumber == false)
        {
            cout << "There is a problem with what you entered, please try again.\n";
        }
    }


    //  Split the number and print it
    std::string areacode;
    std::string prefix;
    std::string number;

    areacode = phoneNumber.substr(0, 3);
    prefix = phoneNumber.substr(3, 3);
    number = phoneNumber.substr(7, 4);

    std::cout
        << "I have your phone number as: ("
        << areacode
        << ") "
        << prefix
        << '-'
        << number
        << '\n';

    return 0;
}

I also recommend you do not place using namespace std statements at namespace scope. It's just bad form and can lead to name conflicts.