Hello Programmers!

I am working on a C++ phone number class. The class has overloaded stream manipulators, cin and cout for output. It has three private members: areaCode, exchange, and line, strings that represent the numbers that are used in a telephone call. It is supposed to default to (000) 000-0000 with the default constructor. Then, the user is able to manipulate the object by inputting in a telephone number of the specified format: (xxx) xxx-xxxx. My overloaded operator 'cin' is supposed to check for various errors on the user's fault, and set the failbit if the inputted number does not consist of 14 characters (see for yourself why), or if the area code and exchange begin with a 0 or a 1, or the middle digit of an area code is not a 0 or a 1. If the failbit is supposed to be set, the cin is NOT supposed to react, but if the failbit does not need to be set, the cin is supposed to react.

Then my cout operator is supposed to print the telephone number out.
However, when I run this main.cpp program:

#include <iostream>
#include "PhoneNumber.h"
using namespace std;

int main()
{
    PhoneNumber phone; //create object phone
    cout << "<Enter> a phone number in the form (123) 456-7890:"<<endl;

    //cin >> phone invokes operators my implicitly
    //issuing the non-member function call opreator >> (cin,phone)
    cin >> phone;

    //cout >> phone invokes operators my implicitly
    //issuing the non-member function call opreator cout (cout,phone)
    cout << phone << endl;
    return 0;
}

it does not print it out. I have tracked the problem down (using the xCode debbugger), and I have found that the problem is in the overloaded Cin operator. It occurs after I check for the failbit to be set, and if the failbit does not need to be set, I am supposed to assign the new values to my private members. The code is below:

//  Definition for class PhoneNumber's functions
#include "PhoneNumber.h"
#include <iomanip>
#include <array>
using namespace std;


PhoneNumber::PhoneNumber()
{
    //default construtor
    areaCode="000";
    exchange="000";
    line="0000";
}
//overloaded stream insertion operator
//a memeber function if we would like to invoke it with
//cout << somePhoneNumber
ostream &operator << (ostream &output, const PhoneNumber &number)
{
    output << "(" << number.areaCode << ")" << number.exchange << "-" << number.line;
    return output; //enables cout << a << b << c << etc.
}

//overloaded stream extraction operator; cannot be
//a member function if we would like to invoke it with
//cin >> somePhoneNumber
istream &operator >> (istream &input,PhoneNumber &number)
{

    //check for various errors in inputing phone number
    //input phone number into a 100-space array
    string strInput;
    getline(cin,strInput);

    const unsigned SIZE=strInput.length();
    char inputTest[SIZE+1]; //for '\0'

    //put the string into an array
    for (int i=0; i < SIZE; i++)
    {
        inputTest[i]=strInput[i];
    }
    inputTest[SIZE+1]='\0';

    //start testing to set failbit
    bool setFailbit=false;

    if (SIZE+1 != 15)  //14 characters + NULL char.
        setFailbit=true;

    else if (inputTest[0] != '(') //area code parenthesies
        setFailbit=true;

    else if (inputTest[4] != ')') //area code parenthesies
        setFailbit=true;

    else if (inputTest[1] == '0' || inputTest[1] == '1') //test the area code
        setFailbit=true;

    else if (inputTest[2] != '0' && inputTest[2] != '1') //continue testing the area code
        setFailbit=true;

    else if (inputTest[9] != '-')
        setFailbit=true;


    if (setFailbit) //set failbit due to an error and do not set the new data in here
        input.clear(ios::failbit);

    else
    {
        cin.ignore(); //skip (
        input >> setw(3) >> number.areaCode; //input area code
        input.ignore(2); //skip ) and space
        input >> setw(3) >> number.exchange; //input exchange
        input.ignore(); //skip dash (-)
        input >> setw(4) >> number.line;     //input line
    }

    return input; //enables cin >> a >> b >> c

}

If you notice in the last "else" structure, the cin.ignore() line, the problem is in this. My debugger stops tracking my problem after I skip over this line. Why is this happening? Is there an issue with my cin.ignore()?

I trashed my approach completely, and I solved the problem!

I have the same problem at the moment! Please, help! How did you solve it?

Hi IrinaAnohina,
knowing that this question is answered about a year ago. I think you should start a new thread to get help.
State what the problems are, what you have done so far and where you are having issues, also don't forget to show your expected output.

Cheeers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.