Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use std::cin or getline with criterion ? #546

Open
P4r4dis opened this issue Jun 27, 2024 · 1 comment
Open

How to use std::cin or getline with criterion ? #546

P4r4dis opened this issue Jun 27, 2024 · 1 comment

Comments

@P4r4dis
Copy link

P4r4dis commented Jun 27, 2024

Hello,
im trying to use std::cin or getline in my code but i have some issues like symply when std::cin or getline() is called, the program runs in a void.
there must be things I haven't understood or that I'm not doing correctly, can you help me please?
Ex:

Test(test, cin)
{
    std::string command;
    
    while (command != "exit")
    {
        std::cin >> command; // I write "exit" (without ""). ISSUE -> ctrl + C for leave
        std::cout << "result: " << command << std::endl; // line never call
    }
}

Thank you

@P4r4dis
Copy link
Author

P4r4dis commented Jun 28, 2024

Im just seeing there is an example in sample/redirect.cc:

char rot13_char(char c)
{
    return std::isalpha(c) ? (c - 'a' + 13) % 26 + 'a' : c;
}

void rot13_io(void)
{
    std::string s;

    std::getline(std::cin, s);
    for (size_t i = 0; i < s.length(); ++i)
        s[i] = rot13_char(s[i]);
    std::cout << s << std::flush;
}

Test(redirect, rot13, .init = cr_redirect_stdout) {
    auto &f_cin = criterion::get_redirected_cin();

    f_cin << "the quick brown fox jumps over the lazy dog";
    f_cin.close();

    rot13_io();

    cr_assert_stdout_eq_str("gur dhvpx oebja sbk whzcf bire gur ynml qbt");
}

I understand the approach.
But is there any way to use std::cin for an "user" can enter a string of their choice and not simulate an user with a predefined string ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant