>当前值){ int cnt=1;//存储我们正在处理的当前值的计数 而(std::cin>>val){//读取剩余的数字 if(val==currVal)//如果值相同 ++cnt;//将1添加到cnt else{//否则,打印上一个值的计数 std::cout,c++,input,user-input,eof,cin,C++,Input,User Input,Eof,Cin" /> >当前值){ int cnt=1;//存储我们正在处理的当前值的计数 而(std::cin>>val){//读取剩余的数字 if(val==currVal)//如果值相同 ++cnt;//将1添加到cnt else{//否则,打印上一个值的计数 std::cout,c++,input,user-input,eof,cin,C++,Input,User Input,Eof,Cin" />

被C+中的控制流执行所迷惑+;初级示例 我正在通过C++引物(第五ED)。在第1.4.4节中,有以下示例: #include <iostream> int main() { // currVal is the number we're counting; we'll read new values into val int currVal = 0, val = 0; // read first number and ensure that we have data to process if (std::cin >> currVal) { int cnt = 1; // store the count for the current value we're processing while (std::cin >> val) { // read the remaining numbers if (val == currVal) // if the values are the same ++cnt; // add 1 to cnt else { // otherwise, print the count for the previous value std::cout << currVal << " occurs " << cnt << " times" << std::endl; currVal = val; // remember the new value cnt = 1; // reset the counter } } // while loop ends here // remember to print the count for the last value in the file std::cout << currVal << " occurs " << cnt << " times" << std::endl; } // outermost if statement ends here return 0; } #包括 int main() { //currVal是我们正在计算的数字;我们将把新值读入val int currVal=0,val=0; //读取第一个数字并确保我们有数据要处理 如果(标准::cin>>当前值){ int cnt=1;//存储我们正在处理的当前值的计数 而(std::cin>>val){//读取剩余的数字 if(val==currVal)//如果值相同 ++cnt;//将1添加到cnt else{//否则,打印上一个值的计数 std::cout

被C+中的控制流执行所迷惑+;初级示例 我正在通过C++引物(第五ED)。在第1.4.4节中,有以下示例: #include <iostream> int main() { // currVal is the number we're counting; we'll read new values into val int currVal = 0, val = 0; // read first number and ensure that we have data to process if (std::cin >> currVal) { int cnt = 1; // store the count for the current value we're processing while (std::cin >> val) { // read the remaining numbers if (val == currVal) // if the values are the same ++cnt; // add 1 to cnt else { // otherwise, print the count for the previous value std::cout << currVal << " occurs " << cnt << " times" << std::endl; currVal = val; // remember the new value cnt = 1; // reset the counter } } // while loop ends here // remember to print the count for the last value in the file std::cout << currVal << " occurs " << cnt << " times" << std::endl; } // outermost if statement ends here return 0; } #包括 int main() { //currVal是我们正在计算的数字;我们将把新值读入val int currVal=0,val=0; //读取第一个数字并确保我们有数据要处理 如果(标准::cin>>当前值){ int cnt=1;//存储我们正在处理的当前值的计数 而(std::cin>>val){//读取剩余的数字 if(val==currVal)//如果值相同 ++cnt;//将1添加到cnt else{//否则,打印上一个值的计数 std::cout,c++,input,user-input,eof,cin,C++,Input,User Input,Eof,Cin,这是因为这一部分: while (std::cin >> val) 为了终止读取输入流,您必须使用一个EOF终止它,该EOF由Ctrl-D提供 想想看:cin默认情况下跳过空白,每次输入一个数字时都用空白(空格、制表符或换行符)分隔 程序将如何终止输入?答案是当它读取EOF字符时-如前所述,该字符由Ctrl-D提供。您必须按Ctrl+D,否则程序不知道您的stdin流何时完成。否则,它将在时坐在那里(std::cin>>val永远不终止。请参阅jrd1答案。或者,您可以在最后一个数

这是因为这一部分:

while (std::cin >> val)
为了终止读取输入流,您必须使用一个EOF终止它,该EOF由Ctrl-D提供

想想看:
cin
默认情况下跳过空白,每次输入一个数字时都用空白(空格、制表符或换行符)分隔


程序将如何终止输入?答案是当它读取EOF字符时-如前所述,该字符由Ctrl-D提供。

您必须按Ctrl+D,否则程序不知道您的stdin流何时完成。否则,它将在
时坐在那里(std::cin>>val
永远不终止。

请参阅jrd1答案。或者,您可以在最后一个数字后加上一个“a”左右。