C++ 用户定义的字符串需要包含空格(但程序不允许)

C++ User defined string needs to contain spaces (but not allowed by program..?)

本文关键字:空格 程序 不允许 包含 定义 用户 字符串 C++      更新时间:2023-10-16

我正在为我的 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;
}