c++ - 陷入循环

标签 c++ loops

我已经posted this question了,但是一旦我实现了给出的建议,所有的回答都不正确。这是我需要发生的事情。

Would you like to process all the records in the file? (y/n) w
Error - Please enter either y or n.
Would you like to process all the records in the file? (y/n) n
Enter number of records to process: two
XXXXXXXXXX Error-non numeric or negative value, try again
Enter number of records to process: -10
XXXXXXXXXX Error-non numeric or negative value, try again
Enter number of records to process: 0
XXXXXXXXXX Error-non numeric or negative value, try again
Enter number of records to process: 10
Maximum requested record count of 10 reached

这就是我所拥有的。我不知道我在做什么错。
#include <iostream>
#include <fstream>

using namespace std;

int main(){

  char a = 0;            //User chooses Y or N
  int ProcessAmount = 0; //Amount of times to process if not all

  cout << "Would you like to process all the records in the file? "
       << "(y/n) ";
  cin >> a;

  if (a == 'y')
  {
    cout << "Processed all records successfuly" << endl;
  }

  do
  {

    if (a == 'n')
    {
      cout << "Enter number of records to process: ";
      cin >> ProcessAmount;

      if (ProcessAmount <= 0 or cin.fail())
      {
        cout << "" << endl;
        cout << "XXXXXXXXX Error-non numeric or negative value";
        cout << "" << endl;
        cin >> ProcessAmount;
      }
      else if (ProcessAmount >= 0 or (!(cin.fail())))
        ;
      {
        cout << "Maximum requested record count of " << ProcessAmount;
        cout << " reached" << endl;
        break;
      }
    }
    else
      (cin.fail());
    {
      cin.clear();
      cin.ignore(40, '\n');
      cout << "Please try again" << endl;
      cout << "Would you like to process all the records in the file? "
           << "(y/n) ";
      cin >> a;
    }

  } while (a == 'n');

}

最佳答案

首先,or是我在C++中不知道的东西,但是在我的gcc编译器中,它可以正常工作,所以谢谢你,尽管如此,我仍然在回答中将其替换为||

除此之外,do - while周期中的事件顺序还存在一些问题,请尝试下面的代码。

Live sample here

do {
  if (a == 'y') {
    cout << "Processed all records successfuly" << endl;
    break;
  }
  if (a == 'n') {
    cout << "Enter number of records to process: ";
    cin >> ProcessAmount;

    if (ProcessAmount <= 0 || cin.fail()) {
      cout << "XXXXXXXXX Error-non numeric or negative value";
      cout << "" << endl;
    }
    else {
      cout << "Maximum requested record count of " << ProcessAmount;
      cout << " reached" << endl;
      break;
    }
    cin.clear();
    cin.ignore(40, '\n');
    continue;
  }

  cout << "Please try again" << endl;
  cout << "Would you like to process all the records in the file? "
       << "(y/n) ";
  cin >> a;

} while (a != 'y');

关于c++ - 陷入循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60583341/

相关文章:

c++ - 取消引用映射内存时出现段错误

python - 从文件中读取行,然后仅将那些不包含特定短语的行写入文件

C++ 使用 scoped_ptr 作为成员变量

c++ - stringstream 中的自定义字符串输入

java - do while 循环错误

c++ - 将 vector 上的 std::find_if 转换为循环的最佳方法是什么?

linux - 使用 Bash 循环遍历 txt 文件中的唯一行

html - 使用水平滚动的 div 布局(无表格)

c++ - 在 C++ 中从另一个进程解锁线程

c++ - G++无法内联多态方法吗?