简体   繁体   中英

How to Exit program loop on pressing Enter/Return Key in C++?

The Challenge is Given like this:

You are provided with an unknown number of test cases. Each test case consists of a natural number, which is followed by a white space, relational operator (==, !=, >= or <=), white space, and another natural number. All test cases are separated with a new line character. You can assume that no number has more than 1000 digits.

So, I'm trying to solve the above problem using C++.The problem is, this program should run for any number of cases that will be checked by Online Judge but my code is works for a limited number of inputs only, as the code will going to be checked by online judge and number of inputs is not specified. So I stuck how to solve this problem.I also tried using do..while() loop and while() loop but it doesn't work -_-

My code is given below:


#include <iostream>

using namespace std;


bool isSmaller(int n1, int n2, string oper)
{
    // Calculate lengths of both string
    if (oper == "==")
      /* code */
    if (n1 == n2)
    return true;

    if (oper == "<=")
      /* code */
    if (n1 <= n2)
    return true;

    if (oper == ">=")
      /* code */
    if (n1 >= n2)
    return true;

    if (oper == "!=")
      /* code */
    if (n1 != n2)
    return true;


    return false;
};

int main() {
  /* code */
  int n1, n2;
  string oper;

  for (int i = 0; i < 1; i++) {
    cin >>n1>>oper>>n2;
  }

  for (int j = 0; j < 1; j++) {
    if(isSmaller(n1, n2, oper)){
      std::cout <<1<<'\n';
    }
    else{
      std::cout <<0<< '\n';
    }
  }

  return 0;
}

Ideal OUTPUT:

A binary sequence should appear on the output. i th element of the sequence should be equal to 1 or 0, depending on whether the corresponding relation is true or false. All elements of the sequence should be separated with a new line character.

Example
Input:
100 == 200
200 <= 100
200 >= 100

Output:
0
0
1

finally I wrote the code based on the hint given by @bruno but still online judge returns an error I don't know wheres the problem but the code seems correct.

The Code is Below:

#include <iostream>
#include <iomanip>
#include <sstream>
#include <list>

using namespace std;


bool computeResult(string line)
{
    // compare two values from given operator
    istringstream stream(line);
    int n1, n2;
    string oper;
    stream >> n1 >> oper >> n2;
    stream >> std::cout.rdbuf();
    if (oper == "==")
      return (n1 == n2);

      if (oper == "!=")
        return (n1 != n2);

          if (oper == ">=")
            return (n1 >= n2);

              if (oper == "<=")
                return (n1 <= n2);

     return false;
};

int main() {
  /* code */
  list<bool> result;
  std::string line;
  std::istringstream stream(line);


  cout << "Enter Numbers \n";

  std::getline(std::cin, line);

  do {
    result.push_back(computeResult(line));
  } while(std::getline(std::cin, line) && !line.empty());

  for (auto b : result)
    std::cout << b << std::endl;

  return 0;
}

Here scanf() will help you to take unknown number of inputs. You can include or to use scanf() and printf() in your C++ program.

while(scanf("%d %s %d",&n1,oper,&n2) == 3)
{
    //Your code goes here.
}

Explanation This works because scanf() returns the total number of inputs scanned successfully, or EOF if input failure occurs before the first receiving argument was assigned. And in this case it will return 3 as for a successful scanning of n1, oper and n2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM