当条件不满足时,必须返回输入

时间:2017-01-29 13:06:29

标签: c++

我希望能够输入1到100之间的数字,然后将相应的值打印到我的txt文件中。如果数字小于1或大于100,我应该回去输入并再试一次,直到我最终满足条件。这就是我到目前为止所做的。

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int c, e = 100;
    ofstream outputFile;
    outputFile.open("num_gen.txt");
    while (0)
    {
        cout << "Enter starting value from 1 to 100: " << endl;
        cin >> c;
        if (c <= 0)
            cout << "Value is too small" << endl;
        else if (c >= 100)
            cout << "Value is too big" << endl;
        else
        {
            //while (c > 0 && c < 100)
            //{
            for (c; c <= e; c++)
            {
                outputFile << "<" << c << c << c << c << endl;
            }
            //}
        }
    }
        cout << "Press enter to exit...";
        cin.get();
        cin.get();
        return 0;
}

1 个答案:

答案 0 :(得分:0)

尝试

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  int c, e = 100;
  ofstream outputFile;
  outputFile.open("num_gen.txt");
  while (true)
    {
      cout << "Enter starting value from 1 to 100: " << endl;
      cin >> c;
      if (c <= 0)
        cout << "Value is too small" << endl;
      else if (c >= 100)
        cout << "Value is too big" << endl;
      else
        {
          while (c > 0 && c < 100)
            {
              outputFile << "<" << c << c << c << c << endl;
              c++;
            }
          break;
        }
    }

  cout << "Press enter to exit..." << endl;
  cin.get();
  cin.get();
  return 0;
}