【问题标题】:Cin in a while loop condition in C++C ++中的while循环条件中的Cin
【发布时间】:2021-06-27 23:38:54
【问题描述】:

以下代码假设获取句子的第一个单词(如果有的话)并逐个字母打印(打印部分有效)。我遇到的问题是,每当我在提示中输入一个句子时,它会抓住句子中的每个单词而不是第一个单词(cin 假设停在第一个空格,输入等......所以我认为这里错误的部分是while循环)。我该如何解决这个问题?我觉得有些地方我不明白。

#include <iostream>
using namespace std;

int main(void){
    int i = 0;
    int length= 25;
    string word[length];
 
    cout << "Type another word: (Press Ctrl + D to quit):";
    while( i < length && cin >> word[i]){
    i++;
    cout << "Type another word: (Press Ctrl + D to quit):";
    }

    for(int j = 0; j < i; j++){
     cout<< endl << word[j]<< endl;
     for(int k = 0; k < word[j].length(); k++)
     cout << word[j].at(k) << endl;
    }
}

正如我们所见,这会抓取整个句子,因此它会打印另一个不需要的提示。

输出示例:

Type another word: (Press Ctrl + D to quit):testing this
Type another word: (Press Ctrl + D to quit):Type another word: (Press Ctrl + D to quit):december
Type another word: (Press Ctrl + D to quit):
testing
t
e
s
t
i
n
g

this
t
h
i
s

december
d
e
c
e
m
b
e
r

我将附上一个“理想”的输出:

Type another word: (Press Ctrl + D to quit):testing this
Type another word: (Press Ctrl + D to quit):december
Type another word: (Press Ctrl + D to quit):
testing
t
e
s
t
i
n
g

december
d
e
c
e
m
b
e
r

【问题讨论】:

  • 它一次读取一个单词,但仍会读取您输入的所有单词。下次循环读取第二个单词
  • string word[length]; 中,length 不是编译时常量,使word 成为可变长度数组,即not standard C++。将length 设为const,或改用std::vector

标签: c++ string while-loop


【解决方案1】:

当您编写 cin 时,假设会在第一个空格处停止,输入等...所以我认为这里错误的部分是 while 循环,您几乎将其钉牢。 cin &gt;&gt; words[i] 将一个字读入数组。循环体递增i,然后再四处读取。逐词地。不是下一句。下一句话。操作员&gt;&gt; 听不懂句子。只是文字。

改为使用std::getline 读取整行,然后从该行获取第一个单词。最简单的方法可能是将行转换为std::istringstream 并使用运算符&gt;&gt; 来提取单个单词。

例如:

#include <iostream>
#include <string> // was missing.
#include <sstream> // defines std::istringstream
using namespace std;

int main(){ // no need for void. Compiler's smart enough to figure 
            // that out from empty braces.
    int i = 0;
    const int length= 25; // needs to be constant to be used to size 
                          // an array
    string word[length];

    cout << "Type another word: (Press Ctrl + D to quit):";
    std::string line; // storage for the line
    while( i < length && std::getline(cin, line)){ //read whole line
        std::istringstream strm(line); // make a stream out of the line
        if (strm >> word[i]) // read first word from line
        {
            i++;
        }
        else  // tell user they made a mistake or do something fancy. 
        {
            std::cout << "No word on that line.\n";
        }
        cout << "Type another word: (Press Ctrl + D to quit):";
    }

    for(int j = 0; j < i; j++){
        cout<< endl << word[j]<< endl;
        for(int k = 0; k < word[j].length(); k++)
            cout << word[j].at(k) << endl;
    }
}

Documentation for std::getline

Documentation for std::istringstream

【讨论】:

    猜你喜欢
    • 2014-04-27
    • 1970-01-01
    • 2017-06-21
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    相关资源
    最近更新 更多