How do I read input from standard input until a sp

2020-07-22 19:10发布

I need to read input from standard input until a space or a TAB is pressed.

Eventually, I need to hold the input in a std::string object.

标签: c++
8条回答
2楼-- · 2020-07-22 19:49

You can use getch() or getchar() to read each character separately, and then process the input manually so that when a space or a tab is pressed, you end the input.

查看更多
戒情不戒烟
3楼-- · 2020-07-22 19:51

This should do the job:

#include <stdio.h>

int main ()
{
  char c;
  do {
    c=getchar();
    /** Your code here **/
  } while ((c != ' ') && (c != '\t'));
  return 0;
}
查看更多
登录 后发表回答