samedi 23 juin 2018

Pass a char to an int in using std::cin in C++

I am having a problem with an exercise that asks me to receive two integers and print them. But, the program ends when the user enters with the entry '|'. However, I am testing this and the program enters in an infinite loop. What is the problem?

#include <iostream>
using namespace std;

int main ()
{
  int i1 = 0, i2 = 0;

  cin >> i1;
  cin >> i2;
  while (i1 != int('|') && i2 != int('|'))
  {
     cout << i1 << endl;
     cout << i2 << endl;

     cin >> i1 >> i2; 
  } 

  return 0;
}

Aucun commentaire:

Enregistrer un commentaire