Search code examples
c++arraysalgorithmc++11stdstring

Trouble with strings and arrays


My goal is to make a program that inputs a phone number and outputs it in a standard format. It skips over any non-number characters, will output if there are not enough digits, and will also skip over any digits after the first ten digits. My raptor worked without a hitch, but it's been difficult to translate it to C++.

I am using Microsoft Visual Studio. The problem is it is not running. If I put in anything more then one number in, I receive a fail error.

I am having some difficulty running this code. Any and all help and advice would be greatly appreciated.

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

void format(char outArray[], string inNumber)
{
    outArray[0] = '(';
    outArray[4] = ')';
    outArray[5] = ' ';
    outArray[9] = '-';

    outArray[1] = inNumber[0];
    outArray[2] = inNumber[1];
    outArray[3] = inNumber[2];
    outArray[6] = inNumber[3];
    outArray[7] = inNumber[4];
    outArray[8] = inNumber[5];
    outArray[10] = inNumber[6];
    outArray[11] = inNumber[7];
    outArray[12] = inNumber[8];
    outArray[13] = inNumber[9];
}

int main()
{
    string phone, inNumber;
    cout << "Please enter a phone number: ";
    cin >> phone;
    int index = 0;
    int num = 0;
    char outArray[14];

    for (index; phone[index] >= '0' && phone[index] <= '9'; index++)
    {
        inNumber[num] = phone[index];
        num++;
    }

    if (inNumber.size() > 10)
    {
        format(outArray, inNumber);

        cout << "The properly formatted number is: ";
        cout << outArray;
    }
    else {
        cout << "Input must contain at least 10 digits." << endl;
    }

    system("pause");
    return 0;
}

Solution

  • A few things to note:

    • Use std::string instead array of char array.

    • You do not need to check charters using a for loop unless you are not sure about the input(phone). However, if that's the case, use std::getline() to get the input and parse as follows using a range-based for loop.

    • You can use std::isdigit to check the character is a digit.

      My goal is to make a program that inputs a phone number and outputs it in a standard format. It skips over any non-number characters, will output if there are not enough digits, and will also skip over any digits after the first ten digits.

    • That means the number should have a minimum length of 10. Then the if statement should be if (inNumber.size() >= 10)

    • Need a pass by ref call in the function format(), since you want to change the content of outArray. Additionally, inNumber could be a const ref, since we do not change this string.

    Updated code: (See a sample code online)

    #include <iostream>
    #include <string>
    #include <cstddef>   // std::isdigit, std::size_t
    
    void format(std::string& outArray, const std::string& inNumber) /* noexcept */
    {
       for (std::size_t index = 0; index < 10; ++index)
       {
          if (index == 0) outArray += '(';
          else if (index == 3) outArray += ") ";
          else if (index == 6) outArray += '-';
          outArray += inNumber[index];
       }
    }
    
    int main()
    {
       std::string phone;
       std::cout << "Please enter a phone number: ";
       std::getline(std::cin, phone);
    
       std::string inNumber;
    
       for (char letter : phone)
          if (std::isdigit(static_cast<unsigned char>(letter))) // check the letter == digits
             inNumber += letter;
    
    
       if (inNumber.size() >= 10)
       {
          std::string outArray;
          format(outArray, inNumber);
          std::cout << "The properly formatted number is: ";
          std::cout << outArray;
       }
       else {
          std::cout << "Input must contain at least 10 digits." << std::endl;
       }
       return 0;
    }