c++parsingistringstream

C++ Trouble parsing telephone number using istringstream and istringstream.ignore


I'll say first that sending my code a hardcoded, perfectly formatted string works fine. But when letting user input string, the code parses areaCode but fails during exchange parsing. Here is my .h

// PhoneNumber.h
#ifndef PHONENUMBER_H
#define PHONENUMBER_H

#include <string>

class PhoneNumber {
   private:
      short areaCode;
      short exchange;
      short line;
   public:
      PhoneNumber(std::string number);
      void setPhoneNumber(std::string number);
      std::string getPhoneNumber() const;
      void printPhoneNumber() const;
};
#endif

Here is my .cpp implementation

// PhoneNumber.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cctype>
#include <stdexcept>
#include "PhoneNumber.h"

PhoneNumber::PhoneNumber(std::string number) {
   setPhoneNumber(number);
}

void PhoneNumber::setPhoneNumber(std::string number) {
   int length = number.length();

   std::istringstream iss(number);
   int count = 0;
   while (!isdigit(number[count])) {
      count += 1;
      iss.ignore(1);
   }

   iss >> std::setw(3) >> areaCode;
   count += 3;

   while (!isdigit(number[count])) {
      count += 1;
      iss.ignore(1);
   }

   iss >> std::setw(3) >> exchange;
   count += 3;

   while (!isdigit(number[count])) {
      count += 1;
      iss.ignore(1);
   }

   if (length - count < 4) {
      throw std::invalid_argument("Something wrong with your phone number input");
   }
   else {
      iss >> std::setw(4) >> line;
   }

}

void PhoneNumber::printPhoneNumber() const {
   std::cout << "(" << areaCode << ") " << exchange << "-" << line;
}

And now my short test code.

// PhoneNumber testing
#include <iostream>
#include <string>
#include "PhoneNumber.h"

int main() {
   std::string p1;

   std::cout << "Enter phone number in format of (800) 555-1212: ";
   std::cin >> p1;

   PhoneNumber phone1(p1);
   phone1.printPhoneNumber();
   std::cout << std::endl;
}

I have tried to write my setPhoneNumber code so that it is user error tolerant. So first question is how do I make this work with user input? Secondary (need not be answered) why does it work with hardcoded telephone number string and not user input?


Solution

  • std::cin >> p1;
    

    will only read to the first space or carriage return. So, if the user inputs (800) 555-0123, you'll only read "(800)".

    You need

    std::getline(std::cin, p1);
    

    to read the input.

    The reason it works with a hardcoded string is that the string assignment operator is not affected by this. When you code p1 = "(800) 555-0123";, p1 gets set to "(800) 555-0123"