C++ 用户定义的字符串需要包含空格(但程序不允许)
C++ User defined string needs to contain spaces (but not allowed by program..?)
我正在为我的 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;
}
例如,我遇到的问题是,如果我输入"Hello World"并按回车键,它会弹出下一行(紧跟在 cin 之后)并说"Hello",空格使其切断了短语的其余部分。
如何解决此问题?我不想使用str::的东西,因为我几乎不知道它们是什么,而且真的从来没有使用过它们,这对老师来说看起来有点可疑:P
更新:如果您建议使用 getline(cin, strinput);效果不太好。从我所看到的情况来看,我只能输入 10 来访问我的函数,但是在我按 Enter 后,它认为我按下了其他内容,这使得它完全跳过 cin 来获取字符串值。但是,这有些奇怪,如果我输入"10 hello world",它会正确执行所有操作。好吧,除了它需要与数字在同一行才能到达该功能。
已解决:如果您事先没有使用用户输入,则使用 getline(cin,strinput)可以正常工作。如果是,你将需要在getline()之前使用cin.ignore 。正如我的最佳答案在评论中所述。
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
using namespace std;
//~~~Initialize all functions
int chapter10();
//~~~Initializing complete
int main() {
srand(time(0)); //makes rng thingy work instead of choose same numbers cause it doesn't do it on its own. lol
cout << "Enter the chapter number you need to look at: ";
int chapterNumber;
cin >> chapterNumber;
switch (chapterNumber) {
case 1: testingStuff(); break;
case 9: chapter9(); break;
case 10: chapter10(); break;
default: cout << "You chose an invalid chapter number, reload the program."; break;
}
system("pause");//So console doesn't close instantly cause that's not annoying at all...
}
int chapter10() {
string strinput;
char charstr[10000];
int numwords=1;
cout << "Enter a phrase." << endl;
cin.ignore(numeric_limits<streamsize>::max(), 'n');
getline(cin, strinput);
const int size = strinput.size() + 1;
strcpy_s(charstr, strinput.c_str());
for (int i = 0; i != size; i++) {
if (*(charstr + i) == ' ' & *(charstr + (i+1)) != ' ' )//the & is fail safe so multiple space no ++numwords
numwords++;
}
cout << "There are " << numwords << " words in that string." << endl;
return 0;
}
我编写代码的方式是使用开关/外壳来访问我的功能。这需要用户输入,这反过来又导致我的程序"认为"我仍在键入第 10 章函数中所需的第二个输入。
添加代码行:cin.ignore(numeric_limits<streamsize>::max(), 'n');
允许我取消输入,并开始一个新的输入。
如果要获取最终用户在一行中输入的所有字符,请使用getline
: 而不是cin >> strinput
写这个:
getline(cin, strinput);
它实际上是std::getline(std::cin, strinput)
的事实没有区别,因为您的代码无论如何都使用std
命名空间。如果您想知道前缀是什么std::
它是标准C++库的命名空间。
你可以使用 getline() 函数
它复制到字符串中,直到到达换行符或找到分隔符 - 因此它将接受所有空格,直到到达换行符http://www.cplusplus.com/reference/string/string/getline/
或者您也可以使用如下所示的cin.getline()
-
std::cin 输入带空格?
使用:
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;
}
- 用于拆分空格字符串的程序不起作用
- 如何在C++的凯撒密码程序中包含空格?
- 为什么EclipseCDT代码格式化程序有时会在模板参数中引入空格
- 解锈化代码格式化程序删除C++单行中的空格
- 崇高文本 3 - 在终端中编译并运行 C++ 程序(路径包含空格)
- 当使用e时没有空格时,奇怪的程序故障
- 在我的输入中没有空格的情况下,程序工作正常?C++
- 字符串中的空格是否会改变程序解释它的方式
- 使用包含给定空格的路径启动程序
- 我有n个空格,在每个空格中,我可以放置一个数字0到m.编写一个程序来输出所有可能的结果.需要帮助:)
- 为什么我不能在数组程序中使用空格?
- 如何从C++程序中删除多个空格,并放一个空格代替它
- 程序错误:用"%20"替换空格
- 当路径中有空格时,从与Qt应用程序相同的目录启动可执行文件
- 如何让我的程序分开而不是将输入之间的空格计算为整数
- C++ 用户定义的字符串需要包含空格(但程序不允许)
- Eclipse在运行调试器时破坏了带有空格字符的程序参数
- 如何在程序 c++ 结束时留出空格
- 当滥用词包含空格时,程序崩溃
- 如何在C++中随机生成的应用程序中在数字之间留出空格