简体   繁体   English

这个循环将如何结束

[英]How this loop will ever end

Opening Again Edit : How to end this one 再次打开编辑:如何结束这一个

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<string> s;
    string word;

    while(cin >> word)
        {
            s.push_back(word);
        }



        for(auto i =0; i < s.size(); i++)
        {
            for(auto &c : s[i])
            c = toupper(c);
        }

        int j=1;
        for(auto c : s)
        {
              cout << c << " ";
              if(j%8==0)
              {
                  cout << "\n";
              }
              j++;
        }  
}
  1. Other method can be used like putting word != "end" or something like that in while loop but it will create and extra word which i dont want. 可以使用其他方法,如在循环中放置单词!=“end”或类似的东西,但它将创建和我不想要的额外单词。

  2. I am not getting a thing why when i give space between two words like, Hello my name is james(in input) then why c++ treats it like different string and strors in different blocks of vector. 我没有得到一个为什么当我在两个单词之间给出空格时,例如,Hello我的名字是james(在输入中)然后为什么c ++将它视为不同的向量中的不同字符串和strors。 I am new at c++ programming as you can see, but a old C programmer, not very old 3 month older, college guy. 我是c ++编程的新手,你可以看到,但是一个老C程序员,不是很老的3个月大的大学生。


This is an example from book c++ primer 5th edition. 这是书籍c ++入门第5版的一个例子。 My question how this while loop will end I tried everything like enter, entering 0 there a many examples in this book like this. 我的问题是这个while循环将如何结束我尝试了所有类似输入,输入0这里有很多例子就像这样。

int main()
{
      vector<unsigned> scores(11, 0);          
      unsigned grade;
      while (cin >> grade)
      { 
            if (grade <= 100) // handle only valid grades
            ++scores[grade/10];
      }

      for(auto c : scores)
      {
           cout << c << endl;
      }
}

The expression cin >> grade yields false (when evaluated as boolean) if the input operation failed. 如果输入操作失败,则表达式cin >> grade产生false(当被评估为boolean时)。 This happens if you reach the end of the input (EOF) or if the itput could not be parsed as the according type. 如果到达输入的末尾(EOF)或者无法将输出解析为相应类型,则会发生这种情况。 Since whitespace is skipped, hitting enter doesn't exit the loop. 由于跳过了空格,因此按Enter键不会退出循环。 Since a zero is a valid unsigned number, that doesn't trigger loop exit either. 由于零是有效的无符号数,因此也不会触发循环退出。 Entering a letter would do the job, or the (OS-specific) key combo to signal EOF. 输入一个字母可以完成工作,或者(特定于操作系统的)键组合来表示EOF。

The expression (cin >> end) has a return value: the object cin . 表达式(cin >> end)有一个返回值:对象cin This is what allows you to do stuff like cin >> x >> y . 这是允许你做cin >> x >> y类的东西。 The reason the loop ends is that the boolean (truth) value of the object cin is equal to the status of the input (probably your terminal) that it is reading from. 循环结束的原因是对象cin的布尔(真值)值等于它正在读取的输入(可能是你的终端)的状态。 If it reaches the end of input, cin will be evaluated as false and the loop ends. 如果它到达输入的末尾,则cin将被评估为false并且循环结束。

As the commenters have said, you can use Ctrl-D or Ctrl-Z in a terminal to cause cin to read the "End of File" marker. 正如评论者所说,您可以在终端中使用Ctrl-D或Ctrl-Z来使cin读取“文件结束”标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM