D
D
Demigodd2017-10-04 10:44:19
C++ / C#
Demigodd, 2017-10-04 10:44:19

How to check the output of cin?

#include <iostream>
#include <string>
using namespace std;

int main() {
  int a, b;

  cout << "write a \n";
  cin >> a;
  cout << "write b \n";
  cin >> b;

  if (a > b) {
    cout << a << " a bolshe \n";
  }
  else if (a == b) {
    cout << a << " ravno " << b << "\n";
  }
  else if (a < b) {
    cout << b << " b bolshe \n";
  }
  else {
    cout << " ERROR \n";
  }

  system("pause");
  return 0;
}

There is such a primitive code, when compiling, if we print y, then
write a
y
write b
-858993460 ravno -858993460

How to check the output of cin and if it is not a digit then else ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2017-10-04
@Demigodd

Read into a line or 1 character and parse each character entered.
You are now directly reading int.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question