how create a read function?

i'm creating the read().
my big objective on function:
1 - no arguments for wait the user press enter(cin.get());
2 - accept several parameters for read to several variables.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void read()
    {
        //nothing
        cin.get();
    }

    template<typename first_t, typename... rest_t>
    void read(const first_t &first, const rest_t... rest) //yes i added the addressed operator
    {
        std::cin>>first;
        read(rest...); //function is recursive
                        //when the parameter pack becomes empty
                        //read(); will be called with no parameters
                        //which is why we declare it above
    }

how i used:
1
2
3
4
string name;
    read(name);
    write(name);
    read();

but i get several errors on "std::cin>>first;":
"error: no matching function for call to 'std::basic_istream<char>::read(const std::__cxx11::basic_string<char>&)'|"
so what i did wrong?
i can't understand that error message, but i have seen the big error:
the parameters are 'const', so we can't change their values... i haven't seen that error.
but now it's fixed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void read()
    {
        //just wait enter key:
        cin.get();
    }

    template<typename first_t, typename... rest_t>
    void read(first_t &first, rest_t... rest)
    {
        std::cin >> first;
        read(rest...); //function is recursive
                        //when the parameter pack becomes empty
                        //read(); will be called with no parameters
                        //which is why we declare it above
    }

thank you so much for all

PS: the write() function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void write()
    {
        //nothing
    }

    template<typename first_t, typename... rest_t>
    void write(const first_t &first, const rest_t... rest)
    {
        std::cout << first;

        write(rest...); //function is recursive
                        //when the parameter pack becomes empty
                        //write(); will be called with no parameters
                        //which is why we declare it above
                        //these will never be used without parameters, but must be here
    }

i can use the 'const' because the variables aren't changed.
i used the address operator, maybe i win memory space, but someone can correct me
Last edited on
and heres the read for strings with several words:
1
2
3
4
5
6
7
8
9
void read(string &parameter)
    {
        std::getline (std::cin,parameter);
    }

    void read(char *parameter)
    {
        cin.getline (parameter, 256);
    }
Fold expressions (C++17): https://en.cppreference.com/w/cpp/language/fold

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <iomanip>

template < typename... T > std::istream& read( T&... args )
{ return ( std::cin >> ... >> args ) ; }

template < typename... T > std::ostream& write( T&&... args )
{ return ( std::cout << ... << args ) << '\n' ; }

int main()
{
    read() && write() ; // read nothing, write a new line

    int a ;
    double b ;
    std::string c ;
    if( read( a, b, c ) ) write( a, ", ", b, '\t', std::quoted(c) ) ;
}

http://coliru.stacked-crooked.com/a/0b75c36c5571adfe
i can't use C++17, or i must change several code... i'm using C++14.
i even tried these functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void read()
    {
        cin.get();
    }

    template<typename first_t>
    void read(first_t arg)
    {
        cin>>arg;
    }

    template <typename first_t, typename... T >
    void read(first_t starg, T... args )
    {
        read(args...);
    }

but i must press enter\return twices :(
i'm sorry, the problem is on read()(no arguments)
i found the problem:
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
void read()
{
    //wait if the eneter key is pressed:
    cin.get();
}

template<typename first_t, typename... rest_t>
void read(first_t &first, rest_t... rest)
{
    std::cin >> first;
    read(rest...); //function is recursive
                    //when the parameter pack becomes empty
                    //read(); will be called with no parameters
                    //which is why we declare it above
}

void read(string &parameter)
{
    std::getline (std::cin,parameter);
}

void read(char *parameter)
{
    cin.getline (parameter, 256);
}

even with these changes, i found the problem:
- the cpp file is created;
- the cpp calls the header file:
#include "Untitled1.h"
but the closed file to cpp.. that wasn't changed... after sometime i notice that... so i changed to real header file:
#include "C:\Users\Cambalinho\Documents\CB\testfolder\Untitled1.h"
now works fine.
sometimes we have these simple problem.. but to notice isn't easy.
thank you so much for all
Last edited on
Topic archived. No new replies allowed.