using cin in coliru

It is possible to use cin with the ideone online compiler, since it offers a separate test area for cin.

However, is it possible to use cin in coliru? If so, how?

Thanks.
If the input consists of many lines, redirect stdin to a file containing the input.

Another option is to use a 'here document' http://www.tldp.org/LDP/abs/html/here-docs.html

For a single line input, we can also use <<< to pick up std input from the command line

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

int main()
{
    int a, b ;
    std::string str ;
    
    std::cin >> a >> b ;
    std::getline( std::cin >> std::ws, str ) ;
    
    std::cout << a << ' ' << b << ' ' << std::quoted(str) << '\n' ; 
}

echo 123 > input_file
echo 45678 >> input_file 
echo 'this is the string' >> input_file

echo && echo =========================== && echo
echo && g++ -std=c++14 -O3 -Wall -Wextra -pedantic-errors main.cpp && ./a.out < input_file

echo && echo =========================== && echo
clang++ -std=c++14 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors main.cpp -lsupc++ && ./a.out <<< '123 45678 this is the string'

echo && echo =========================== && echo
clang++ -std=c++14 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors main.cpp -lsupc++ && ./a.out <<here!here!here!
123
45678
this is the string
here!here!here!
echo && echo && echo

===========================


123 45678 "this is the string"

===========================

123 45678 "this is the string"

===========================

123 45678 "this is the string"

http://coliru.stacked-crooked.com/a/01ee203437394166
Last edited on
Thanks for the reply.

Incidentally, coliru Help has provided an example of how to accept standard input (cin):

http://coliru.stacked-crooked.com/a/0f5920b3edc34787

It had missed my notice.
Registered users can post here. Sign in or register to post.