About operator >>

Hello everyone! I'm just a beginner at C++ and I want to ask about this problems:

In C we can use scanf("%d / %d / %d", &mm, &dd, &yyyy) to get multiple variable, how can I do this with operator >> in C++?

I tried to use cin >> mm >> "/" >> dd >> "/" >> yyyy but it didn't work

Any help will be appreciated.

Regards
You can't. At least not with any variable type I am familiar with.

the >> operator will only handle what the programmer who designed the type you are using it with programmed the operator to do.

With default types (number based variables and chars) y = x >> n; moves/shifts the bits in the x variable n space to the right and stores the results in y.

With the cin statement, the cin is actually a standardized variable of the ifstream type. It is only programmed to move what is in the "stream" (in this case, what the person types) into an appropriate variable. Only the first >> in your statement does this. The computer doesn't know how to handle the rest. For one thing, string literals (strings that are explicitly written in the code such as "/") are not variables. You cannot move data into them.


Last edited on
1
2
3
char s1, s2 ;
int mm, dd, yyyy ;
if( std::cin >> mm >> s1 >> dd >> s2 >> yyyy && s1 == '/' && s2 == '/' )


If you must have scanf like syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

struct eat_char
{
    eat_char( char c ) : ch(c) {}
    const char ch ;
};

std::istream& operator>> ( std::istream& stm, eat_char ec )
{
    char c ;

    if( stm >> c )
    {
        if( c == ec.ch ) return stm ;

        stm.putback(c) ;
        stm.clear(std::ios::failbit) ;
    }

    return stm ;
}

int main()
{
    int a, b, c ;
    while( std::cin >> a >> '/' >> b >> ':' >> c )
        std::cout << a << ' ' << b << ' ' << c << '\n' ;
}

http://coliru.stacked-crooked.com/a/0c836b55430a680b
Last edited on
Interesting. So how does it know to cast a, '/', b, etc... as an eat_char without an explicit cast?
Operator overloading. (Edit: Which is not casting) The >> operator is overloaded with an std::istream on the left-hand side, and handles a variety of possible data types on the right hand side, from characters, to string literals, to std::strings, to ints to doubles, etc etc, out of the box.

If you look here
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
You can see all the overloads function declarations. The compiler knows which one matches.
Last edited on
> how does it know to cast a, '/', b, etc... as an eat_char without an explicit cast?

The constructor of eat_char is a converting constructor.
1
2
3
4
5
struct eat_char
{
    eat_char( char c ) : ch(c) {} // converting constructor
    const char ch ;
};

See: http://en.cppreference.com/w/cpp/language/converting_constructor
Thanks you, guys!

I want to ask another question:

What are the differences between ios::setstate and ios::clear, the description says that these 2 function both use for setting stage flag?

If I set cin's error state flag like this cin.setstate(std::ios::failbit), does that mean I can't use it anymore to input data?
> What are the differences between ios::setstate and ios::clear

std::basic_ios::setstate(flag) sets the states in flag in addition to the already set state flags.

std::basic_ios::clear(flag) replaces the stream state with the states in flag

std::cout.setstate( std::ios_base::failbit )
is equivalent to
std::cout.clear( std::cout.rdstate() | std::ios_base::failbit )


> If I set cin's error state flag like this cin.setstate(std::ios::failbit), does that mean I can't use it anymore to input data?

You can, after you clear the failed state with std::cin.clear()

Topic archived. No new replies allowed.