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

时间:2014-12-29 23:02:23

标签: c++ for-loop

  1. 编写一个由while循环组成的程序(每次循环)读入两个整数然后打印它们。终止'|'时退出程序进入了。
  2. 你怎么写这个?

    当我检查int是否等于|时,他们从不这样做,因为如果输入|,它们的值为零。如果输入|,我的程序将永远重复。我不知道如何通过使用int来访问非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 << "\n\n";
    22-    }
    23-
    24-    return 0;
    25- }
    

4 个答案:

答案 0 :(得分:1)

你有两个问题。首先,你不是在给泵灌注&#34;第二,你试图将管道转换为int,这样就会出问题。而不是在开头将val1和val2作为int插入,而是将它们键入为字符串。由于您只是打印数值而不进行任何数学计算,因此无关紧要。从那里开始,您需要在进入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 << "\n\n";           
       //recharge val1 and val2 before looping next time.
       cin >> val1;
       cin >> val2;
   }

    return 0;
 }

答案 1 :(得分:0)

不要直接从val1阅读val2stdin,而是阅读一行文字。如果该行不以|开头,请使用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 << "\n\n";
   }

   return 0;
}

答案 2 :(得分: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;
}

虽然如果您需要跳过白色字符,例如&#39; \ n&#39;,这不会起作用。您应该尝试查看此相关问题以解决此问题:How to properly use cin.peek()

答案 3 :(得分:0)

请遵循代码中的注释

// 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;
    }