finding a string within a struct

I'm not sure what would be the best method of finding a string that is locate within a struct. My struct has:

1
2
3
4
5
6
  struct apartmentInfo 						// apartment information
{                      
        string phoneNum;                    // phone number  
        float rent;                         // ental price
        bool rentalStatus;                  // apartment rented = 1; vacant apartment = 0
};

The format of phoneNum is: 123-123-1234. This is the string that I need to find and verify after the user inputs the phone number. What do you guys recommend using? Thank you.
I am not sure if I understand your question right. From what I understand, I think you want to compare user's input with structure object and find out if the user matches with apartmentInfo object. If so, you would like to return other member variables pertaining to that object.

Some things to remember, struct and classes are user-defined data types. We use struct when we only care about member variables and classes when we want some functions to work on member variables. Also, you need to instantiate objects from these data types to access these values.

Hope this helps.
> This is the string that I need to find and verify after the user inputs the phone number.
> What do you guys recommend using?

The regular expressions library (which is part of the standard C++ library.)
http://en.cppreference.com/w/cpp/regex

1
2
3
4
5
6
7
8
9
#include <string>
#include <regex>

bool is_valid_phone_number( std::string str )
{
    // http://www.stroustrup.com/C++11FAQ.html#raw-strings
    static const std::regex pattern( R"(\d\d\d\-\d\d\d\-\d\d\d\d)" ) ; // \d - decimal digit [0-9]
    return std::regex_match( str, pattern ) ;
}

http://coliru.stacked-crooked.com/a/150c556118de5358
http://rextester.com/FRMX4071
Topic archived. No new replies allowed.