C++;用户定义的字符串需要包含空格(但程序不允许…?) 我正在为我的C++课做作业,而且自从我用它以来已经有一段时间了。我想知道是否有办法在字符串中允许空格(而不是将其置零并结束字符串) 我目前的代码是: int chapter10() { string strinput; char charstr[1000]; int numwords=1; cout << "Enter a phrase "; cin >> strinput; cout << strinput; const int size = strinput.size() + 1; strcpy_s(charstr, strinput.c_str()); cout << strinput << endl; for (int i = 0; i != size; i++) { if (*(charstr + i) == ' ') numwords++; } cout << "There are " << numwords << " words in that string." << endl; return 0; } int第10章(){ 弦纹; char-charstr[1000]; int numwords=1; cout>strinput; cout chapterNumber; 开关(chapterNumber){ 案例1:测试材料();断裂; 案例9:第9章();中断; 案例10:第10章();中断; 默认值:cout
如果您希望在一行中获取最终用户输入的所有字符,请使用:而不是C++;用户定义的字符串需要包含空格(但程序不允许…?) 我正在为我的C++课做作业,而且自从我用它以来已经有一段时间了。我想知道是否有办法在字符串中允许空格(而不是将其置零并结束字符串) 我目前的代码是: int chapter10() { string strinput; char charstr[1000]; int numwords=1; cout << "Enter a phrase "; cin >> strinput; cout << strinput; const int size = strinput.size() + 1; strcpy_s(charstr, strinput.c_str()); cout << strinput << endl; for (int i = 0; i != size; i++) { if (*(charstr + i) == ' ') numwords++; } cout << "There are " << numwords << " words in that string." << endl; return 0; } int第10章(){ 弦纹; char-charstr[1000]; int numwords=1; cout>strinput; cout chapterNumber; 开关(chapterNumber){ 案例1:测试材料();断裂; 案例9:第9章();中断; 案例10:第10章();中断; 默认值:cout,c++,string,C++,String,如果您希望在一行中获取最终用户输入的所有字符,请使用:而不是cin>>strinput编写以下命令: getline(cin, strinput); 实际情况是 STD::GETLIN(STD:CIN,StruPin)< /C>没有区别,因为您的代码无论如何使用了代码> STD< /Cord>命名空间。如果您想知道什么是代码> STD::/Cord>前缀,它是标准C++库的命名空间。< /P> < P> > GETLILL()函数 它复制到一个字符串中,直到到达换行符或找到分隔符为止——因此它
cin>>strinput
编写以下命令:
getline(cin, strinput);
<>实际情况是<代码> STD::GETLIN(STD:CIN,StruPin)< /C>没有区别,因为您的代码无论如何使用了代码> STD< /Cord>命名空间。如果您想知道什么是代码> STD::/Cord>前缀,它是标准C++库的命名空间。< /P> < P> > GETLILL()函数它复制到一个字符串中,直到到达换行符或找到分隔符为止——因此它将接受所有空格,直到到达换行符为止 或者您也可以使用
cin.getline()
如图所示-使用:
cin >> noskipws >> strinput;
使用std::getline()函数。示例:
#include <iostream>
#include <vector>
#include <sstream>
void WordCounter(
const std::vector<std::string> & lines) {
for (int i = 0; i < lines.size(); ++i) {
std::istringstream iss(lines[i]);
std::string word;
int count = 0;
while (iss >> word) {
++count;
}
std::cout << "Line #" << i << " contains " << count << " words." <<
std::endl;
}
}
int main() {
std::string line;
std::vector<std::string> lines;
while (std::getline(std::cin, line)) {
lines.push_back(line);
}
WordCounter(lines);
return 0;
}
#包括
#包括
#包括
无效字计数器(
常量标准::向量和线){
对于(int i=0;i>word){
++计数;
}
如果您想读取整行输入,std::cout使用std::getline()
。使用它可以使它完全跳过我的cin,谢谢,尽管它所做的只是禁用跳过前导空格。好的,终于让它停止给我编译器错误(不确定我做了什么)但现在它只是跳过了允许你输入内容的部分,我输入10,它立即完成,不允许我输入字符串。这张图片:是因为尝试在注释中输入代码不太有效well@JohnOlivas您可能在执行用户输入的其他函数之后调用您的函数忽略输入缓冲区中剩余的'\n'
。在getline
之前添加此行:cin.ignore(数值限制::max(),'\n')
,并在程序顶部添加#include
。好的,我也使用了这个,出现了同样的问题,我输入我的case语句以进入我的函数,当我在章节号(如何到达)后按enter键时,它会跳过整个cin行,我在键入“10”后不会按任何键,它只会跳过它。但是如果我键入“10 hello world”它抓取字符串hello world,并做它应该做的事情…只是,不在正确的时间。字符串c;std::getline(cin,c);coutdasblink为我修复了它,因为我使用了userinput的主函数来访问该程序所在的函数,它仍然使用我输入的内容作为用户输入。
#include <iostream>
#include <vector>
#include <sstream>
void WordCounter(
const std::vector<std::string> & lines) {
for (int i = 0; i < lines.size(); ++i) {
std::istringstream iss(lines[i]);
std::string word;
int count = 0;
while (iss >> word) {
++count;
}
std::cout << "Line #" << i << " contains " << count << " words." <<
std::endl;
}
}
int main() {
std::string line;
std::vector<std::string> lines;
while (std::getline(std::cin, line)) {
lines.push_back(line);
}
WordCounter(lines);
return 0;
}