Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1088579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:05:18+00:00 2026-05-16T23:05:18+00:00

I have a function that reads user input from std::cin, and I want to

  • 0

I have a function that reads user input from std::cin, and I want to write a unittest that inserts some strings into std::cin, such that later extraction from std::cin will read that string instead of pausing for keyboard input.

Ideally, I would change the function signature so that I can pass a custom istream as parameters, but I can’t do that here since I have a fixed interface that I cannot change.

cin.putback() is almost what I wanted, however it is inserting only one character at a time, and it is inserting them in reverse order (but I read somewhere that putting back char that wasn’t originally there can be dangerous, though the website doesn’t elaborate why). I’ve tried several methods to inject the string to cin’s internal buffer cin.rdbuf(), but none would work either. I’ve also considered using an external testing script or creating subprocess, however I’d like to first consider a test in pure C++.

So, is there any method to put strings into cin? Or do you know a better way to inject my “fake keyboard input”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:05:19+00:00Added an answer on May 16, 2026 at 11:05 pm

    If you really, really want to use std::cin, try this:

    int main() {
      using namespace std;
      streambuf *backup;
      istringstream oss("testdata");
      backup = cin.rdbuf();
      cin.rdbuf(oss.rdbuf());
      string str;
      cin >> str;
      cout << "read " << str;    
    }
    

    You can restore std::cin’s streambuf when you are done from backup.
    I don’t guarantee the portability of this ;P

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some functionality that I want to mock out being called from main
I have a form that accepts numeric input from the user. Upon form submission,
I have an example function below that reads in a date as a string
I have a function, that reads a registry key, get a path for a
I have this function on my controller (Im using CodeIgniter) that reads the database,
I have a function that calls out a read or write request on a
I have some jQuery code that intercepts links clicked on a page: $(document).ready(function() {
Inside my Controller i have function that runs after user clicks on item, which
I have a jQuery search that gets a search term from the user and
I have a php file that reads data from my mysqldb as a json

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.