Here is my code:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

//()
char openedP='('; //open (
char closedP=')'; //close )
//end ()

//dash
char dash='-';
//end dash

class PhoneNumber
{
int countryCode;
int areaCode;
int number;
char type;
int year;
public:
PhoneNumber()
{
countryCode = 43;
areaCode = 800;
number = 8675309;
type = 'H';
year = 1981;
}
PhoneNumber(int ccode, int acode, int num,char line,int yr)
{
countryCode = ccode;
areaCode = acode;
number = num;
type = line;
year = yr;

}
PhoneNumber(int num,char line = 'B')
{
number = num;
areaCode = 800;
year = 1981;
countryCode = 43;
type = line;
}
PhoneNumber(int acode, int num, char line = 'C')
{
countryCode = 43;
year = 1981;
areaCode = acode;
number = num;
type = line;

}
void setCountry(int ccode)
{
ccode = countryCode;
}
void setArea(int acode)
{
acode = areaCode;
}
void setType(char line)
{
type = line;
}
void setYear(int yr)
{
year = yr;
}
int getCountry() const
{
return countryCode;
}
int getNumber() const
{
return number;
}
int getArea() const
{
return areaCode;
}
char getType() const
{
return type;
}
void printNumber(ostream& out) const //Prints Areacode & Phone Number
{
cout<<"The phone number is: \n";
cout<<openedP<<areaCode<<closedP<<number/10000<<dash<<number%10000<<endl;
cout<<endl;
}
void printPhoneNumberStats(ostream& out) const // prints all phone information
{
cout<<"Printing phone number stats!\n";
cout<<" Country code: "<<countryCode<<endl;
cout<<" Area code:    "<<areaCode<<endl;
cout<<" Phone number: "<<number<<endl;
cout<<" Type:         "<<type<<endl;
cout<<" Year:         "<<year<<endl;
cout<<endl;
}
};
int main()
{
    char q; //stop from quitting
    PhoneNumber firstNum;
    PhoneNumber secondNum(39, 415, 8675555, 'B', 2012);
    PhoneNumber thirdNum(1234567);
    PhoneNumber fourthNum(1234566, 'C');
    PhoneNumber fifthNum(925, 4392181);
    PhoneNumber sixthNum(925, 5512346, 'H');

    firstNum.printNumber(cout);
    firstNum.printPhoneNumberStats(cout);
    secondNum.printNumber(cout);
    secondNum.printPhoneNumberStats(cout);
    thirdNum.printNumber(cout);
    thirdNum.printPhoneNumberStats(cout);
    fourthNum.printNumber(cout);
    fourthNum.printPhoneNumberStats(cout);
    fifthNum.printNumber(cout);
    fifthNum.printPhoneNumberStats(cout);
    sixthNum.printNumber(cout);
    sixthNum.printPhoneNumberStats(cout);
cin>>q;
  return 0;
}

Here is what the output should look like:

The phone number is:
(800)867-5309
Printing phone number stats!
Country code: 43
Area code: 800
Phone number: 8675309
Type: H
Year: 1981

The phone number is:
(415)867-5555
Printing phone number stats!
Country code: 39
Area code: 415
Phone number: 8675555
Type: B
Year: 2012
There are duplicate digits in this phone number!!

I need help detecting double digits in the 7 digit phone number and printing it out.

Dani AI

Generated

Nice start, — and good tip from to consider a string-based check. Two quick fixes before adding detection: your setCountry / setArea implementations assign the wrong way around (they should set the member from the parameter), and both printNumber and printPhoneNumberStats should use the passed ostream& out rather than writing directly to cout. Also consider using <iomanip> (setw + setfill) so the last 4 digits always print with leading zeros.

If you prefer to avoid allocating a string for each check, here is a compact integer-based helper that scans the seven digits and returns true if any adjacent pair match. It reads digits LSB-to-MSB, which does not change adjacency:

// return true if any two adjacent digits in a 7-digit number are equal
bool hasAdjacentDuplicateDigits(int sevenDigit)
{
    if (sevenDigit < 0 || sevenDigit > 9999999) return false; // validate as needed
    int prev = -1;
    for (int i = 0; i < 7; ++i) {
        int d = sevenDigit % 10;
        if (d == prev) return true;
        prev = d;
        sevenDigit /= 10;
    }
    return false;
}

Integration is simple: call this helper after printing the phone info and emit the duplicate message through the same ostream& out. If you must preserve leading zeros as entered (e.g., "0123456"), use the string approach (std::to_string) and pad to 7 characters before checking. Final notes: fix the two setter assignments, switch your printing to use out, and consider validating the numeric range in constructors so malformed inputs are handled predictably.

Implement a function which iterates over the 7 digit phone number (one digit at a time) and checks if it's the same as the digit before. I suggest converting the phonenumber to string for easier manipulation.

Here's a short example (not the complete solution;)

bool hasDoubleDigits( int phoneNumber )
{
 //convert int to string
 std::string phoneNumberSring;
 // iterate over string
 for( unsigned i=0; i!=phoneNumberString.size()-1;++i )
 {
   //check if phoneNumberString.at(i) == phoneNumberString.at(i+1)
   //return true
 }
// no doubledigits found, return false
}
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.