C++作业(循环终止时)

C++ homework (while loop termination)

本文关键字:终止 循环 作业 C++      更新时间:2023-10-16
  1. 编写一个由 while-loop 组成的程序,该循环(每次围绕循环)读取两个整数,然后打印它们。输入终止的"|"时退出程序。

你怎么写这个?

当我检查任何一个 int 是否等于 | 他们永远不会这样做,因为如果输入 |,它们的值为零。如果输入|,我的程序当前将永远重复。我不知道如何使用 ints 访问非 int |的正确值,也不知道如何阻止它永远重复。

1 - #include <iostream>
2 - 
3 - using namespace std;
4 - 
5 - int main()
6 - {
7 -     int val1=0, val2=0;
8 -     while(val1 != '|' && val2 != '|')
9 -     {
10-        cout << "Enter 2 integers, terminate with "|"" << endl;
11-        cin >> val1;
12-        if (val1 == '|')
13-        {
14-            return 0;
15-        }
16-        cin >> val2;
17-        if (val2 == '|')
18-        {
19-            return 0;
20-        }
21-        cout << val1 << " " << val2 << "nn";
22-    }
23-
24-    return 0;
25- }

你有两个问题。首先,您不是在"启动泵",其次您正在尝试将管道转换为int,因此这将是一个问题。不要在开头将 val1 和 val2 转换为整数,而是将它们键入为字符串。由于您只是打印值而不进行任何数学运算,因此这无关紧要。从那里,您需要先阅读一次输入,然后再放入 while 语句。你这样做,所以如果你立即得到一个管道,程序将停止。

int main()
{
    string val1=0, val2=0;
    //grab the values first, before checking at the head of the loop
    cin >> val1;
    cin >> val2;
    while(val1 != '|' && val2 != '|')
    {
       //You may want to check for empties in your while as well
       cout << "Enter 2 integers, terminate with "|"" << endl;
       cout << val1 << " " << val2 << "nn";           
       //recharge val1 and val2 before looping next time.
       cin >> val1;
       cin >> val2;
   }
    return 0;
 }

不是直接从stdin读取val1val2,而是阅读一行文本。如果该行不以 | 开头,则使用 sscanfistringstream 从该行中提取数字。如果该行以 | 开头,则断开while循环。

#include <stdio.h>
#include <iostream>
using namespace std;
const int LINE_SIZE = 200; // Make it large enough.
int main()
{
   char line[LINE_SIZE] = {0};
   int val1=0, val2=0;
   // Break out of the loop after reading a line and the first character
   // of the line is '|'.
   while( true )
   {
      cout << "Enter 2 integers, teminate with "|"" << endl;
      // Read the entered data as a line of text.
      cin.getline(line, LINE_SIZE);
      if ( !cin )
      {
         // Deal with error condition.
         break;
      }
      // If the first character of the line is '|', break out of the loop.
      if ( line[0] == '|' )
      {
         break;
      }
      // Read the numbers from the line of text.
      int n = sscanf(line, "%d %d", &val1, &val2);
      if ( n != 2 )
      {
         // Deal with error condition.
         continue;
      }
      cout << val1 << " " << val2 << "nn";
   }
   return 0;
}
您可以使用

istream.peek()来判断下一个字符是什么,而无需消耗它:

int main()
{
    int val1 = 0;
    int val2 = 0;
    while (std::cin.peek() != '|')
    {
       std::cin >> val1;
       std::cin >> val2;
       std::cout << val1 << " " << val2 << std::endl;
   }
   return 0;
}

检查 EOF 也是一种很好的做法:

while (std::cin.peek() != EOF && std::cin.peek() != '|')

而且您不必使用两个变量,将它们放在作用域内:

int main()
{
    while (std::cin.peek() != '|')
    {
       int val = 0;
       std::cin >> val;
       std::cout << val << " ";
       std::cin >> val;
       std::cout << val2 << std::endl;
   }
   return 0;
}

尽管如果您需要跳过白色字符(如""),这将不起作用。您应该尝试查看此相关问题来解决此问题:如何正确使用 cin.peek()

请关注代码中的注释

// while (cin >> variable), read whitespace-separated values in type condition.
// We use character '|' to terminate the input — anything that isn’t an int type can be used.
// for string type, Ctrl+Z terminates an input stream under Windows and Ctrl+D does that under Unix.
    int main ()
    {
        int val1;
        int val2;
        cout << "Enter 2 integers: n";
        while (cin >> val1 >> val2 )
             cout << "You entered: n" << val1 <<'n' << val2 << 'n';
        return 0;
    }