【问题标题】:break statement in c++ by pressing enterc++中的break语句,按回车键
【发布时间】:2016-06-13 19:45:32
【问题描述】:
#include <iostream>
using namespace std;
int main()
{
    int n,t=0,k=0;
    cin>>n;
    char data[n][100];
    int num[n];
    for(int i=0;i<n;i++)
{
    while(1)
    {
        cin>>data[i][t];
        cout<<data[i][t]<<endl;
        if(data[i][t]=='\n') break;
        k++;
        if(k%2==1) t++;
    }
    cout<<i;
    num[i]=(t-2)/2;
    k=0;
t=0;
 }

    for(int i=0;i<n;i++)
    {
        while(1)
        {
            cout<<data[i][t];
            if(t==num[i]) break;
            t++;
        }
        t=0;
    }
}

这是我用 c++ 编写的代码,它给出了用户给出的每个单词的前半部分的偶数字符,但是在按下 enter 后给出输入时,循环应该中断,但循环没有中断

while(1)
{
    cin>>data[i][t];
    cout<<data[i][t]<<endl;
    if(data[i][t]=='\n') break;
    k++;
    if(k%2==1) t++;
}

【问题讨论】:

  • data[i][t] 中的实际内容是什么?
  • 您假设cin 默认情况下会在从流中读取的数据中包含换行符。这是不正确的。
  • 这是我用 C++ 编写的代码 -- cin&gt;&gt;n; char data[n][100]; -- 这不是有效的 C++。数组必须具有编译时大小。
  • 与其自己搜索行尾,may I suggest std::getline获取整行然后解析行with a std::stringstream?这种方法将这个问题减少到几乎与Read file line by line 重复。

标签: c++ break enter


【解决方案1】:

默认情况下,使用“输入”运算符&gt;&gt; 的格式化输入跳过空白,换行符是空白字符。因此,&gt;&gt; 运算符只是等待输入一些非空白输入。

要告诉输入不要跳过空格,您必须使用std::noskipws 操纵器:

cin>>noskipws>>data[i][t];

【讨论】:

    【解决方案2】:

    有一些方法可以在 C++ 中实现 OP 试图做的事情。我会开始避免使用标准中没有的可变长度数组,而是使用std::strings 和std::vectors。

    一种选择是使用std::getline 从输入中读取整行,然后处理生成的字符串以仅保留偶数字符的前半部分:

    #include <iostream>
    #include <string>
    #include <vector>
    
    int main() {
        using std::cin;
        using std::cout;
        using std::string;
    
        cout << "How many lines?\n";
        int n;
        cin >> n;
    
    
        std::vector<string> half_words;
        string line;
        while ( n > 0  &&  std::getline(cin, line) ) {
            if ( line.empty() )     // skip empty lines and trailing newlines
                continue;
            string word;
            auto length = line.length() / 2;
            for ( string::size_type i = 1; i < length; i += 2 ) {
                word += line[i];
            }
            half_words.push_back(word);
            --n;
        }
    
        cout << "\nShrinked words:\n\n";
        for ( const auto &s : half_words ) {
            cout << s << '\n';
        }
    
        return 0;
    }
    

    另一个是,正如 Joachim Pileborg 在他的回答中所做的那样,使用 std::noskipws manipolator 禁用格式化输入函数跳过前导空格,然后一次读取一个字符:

    // ...
    // disables skipping of whitespace and then consume the trailing newline
    char odd, even;
    cin >> std::noskipws >> odd;
    
    std::vector<string> half_words;
    while ( n > 0 ) {
        string word;
        // read every character in a row till a newline, but store in a string
        // only the even ones
        while (    cin >> odd  &&  odd != '\n'
                && cin >> even && even != '\n' ) {
            word += even;
        }
        // add the shrinked line to the vector of strings
        auto half = word.length() / 2;
        half_words.emplace_back(word.begin(), word.begin() + half);
        --n;
    }
    //  ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-06
      • 2014-02-21
      • 2013-02-16
      • 2018-09-17
      • 2012-09-24
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      相关资源
      最近更新 更多