Skipping expected characters like scanf () with cin

How to achieve effect scanf("%d # %d",&a,&b);using cinin C ++?

+3
source share
4 answers

You can create your own stream manipulator. This is pretty easy.

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

// skips the number of characters equal to the length of given text
// does not check whether the skipped characters are the same as it
struct skip
{
    const char * text;
    skip(const char * text) : text(text) {}
};

std::istream & operator >> (std::istream & stream, const skip & x)
{
    ios_base::fmtflags f = stream.flags();
    stream >> noskipws;

    char c;
    const char * text = x.text;
    while (stream && *text++)
        stream >> c;

    stream.flags(f);
    return stream;
}

int main()
{
    int a, b;
    cin >> a >> skip(" # ") >> b;
    cout << a << ", " << b << endl;
    return 0;
}
+2
source

You can skip #by extracting it to the symbol:

std::istringstream iss("10 # 20");

int main()
{
   int a, b; char hash;
   iss >> a >> hash >> b;

   assert(a == 10 && b == 20);
}
+2
source

istream , , . , , , , , .

, , - getline() , , , . , , , , ('#') , , , . - , false -, , , .

:

...

getline(cin,myStr);

while(!formatCheck(myStr))
{
    cout<<"Not valid format for input";
    getline(cin,myStr);
}

...

bool formatCheck(string str)
{
    string firstPart=str.subString(0,firstSpaceLocation);
    string middle=str[firstSpaceLocation+1];
    string lastPart=str.subString(firstSpaceLocation+3,end);

    if(first part not a valid number || middle!="#" || last part not a valid number)
    {
        return false;
    }

    return true;
}
+1

Here is another way. You can classify it #as a space character through a facet std::ctype<char>soaked in locale:

#include <iostream>
#include <sstream>
#include <vector>

namespace detail
{
    enum options { add, remove };

    class ctype : public std::ctype<char>
    {
    private:
        static mask* get_table(const std::string& ws, options opt)
        {
            static std::vector<mask> table(classic_table(),
                                           classic_table() + table_size);
            for (char c : ws)
            {
                if (opt == add)
                    table[c] |= space;
                else if (opt == remove)
                    table[c] &= ~space;
            }
            return &table[0];
        }
    public:
        ctype(const std::string& ws, options opt)
            : std::ctype<char>(get_table(ws, opt)) { }
    };
}

class adjustws_impl
{
public:
    adjustws_impl(const std::string& ws, detail::options opt) :
        m_ws(ws),
        m_opt(opt)
    { }

    friend std::istream& operator>>(std::istream& is,
                                    const adjustws_impl& manip)
    {
        is.imbue(std::locale(is.getloc(),
                             new detail::ctype(manip.m_ws, manip.m_opt)));
        return is;
    }
private:
    std::string m_ws;
    detail::options m_opt;
};

adjustws_impl setws(const std::string& ws)
{
    return adjustws_impl(ws, detail::add);
}

adjustws_impl unsetws(const std::string& ws)
{
    return adjustws_impl(ws, detail::remove);
}

int main()
{
    std::istringstream iss("10 # 20");
    int a, b;

    iss >> setws("#");
    iss >> a >> b;

    iss >> unsetws("#");
    std::cout << a << ' ' << b; // 10 20
} 
+1
source

All Articles