【问题标题】:How to input a string in VC如何在VC中输入字符串
【发布时间】:2016-11-29 00:07:00
【问题描述】:

我想用VC输入一个字符串,但是getline函数似乎不起作用,当我运行我的程序时,“cin”部分被跳过了。这是我的代码

printf("Exercise 1\n");
    printf("Please enter the sentence you want\n");
    char str[256];
    std::cin.getline(str, 256);
    std::cout << str;

这是我的标题

#include <iostream>
#include <string>
#include <bitset>

我在使用 VS2015 社区,我的编译器有什么问题吗?

【问题讨论】:

  • char str[256]; -> std::string str; , std::cin.getline(str, 256); -> std::getline(std::cin, str); 和天空又是蓝色的。
  • 买一本不同的书。您应该将std::string 用于string 和文本。

标签: c++ visual-studio visual-c++


【解决方案1】:

你为什么不做呢?

char str[256];
std::cin >> str;

string mystring;
std::getline (std::cin,mystring);

【讨论】:

  • 嗯,我以前做过。但是我需要计算我的字符串中的单词和字符,并且需要跳过两个单词之间的空格。这就是我使用 getline 功能的原因。那我该怎么做呢?
  • 顺便说一句,std::cin &gt;&gt; mystring 只会读取一个单词。要获取输入文本行,必须使用std::getline
  • 我明白了。已编辑。感谢@ThomasMatthews 的评论
  • @D.Kenny :为此,请参阅stackoverflow.com/questions/236129/split-a-string-in-c 投票最多(1800+)的答案,然后再拆分字符串。
猜你喜欢
  • 2012-11-27
  • 2017-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 2014-04-25
  • 2011-04-03
  • 2020-04-10
相关资源
最近更新 更多