Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ cin accept only numbers

#include <iostream>
#include <limits>

using namespace std;

main () {
    int x, y;
    // <-- NO READ HERE!

    do {
        cout << "Enter a number: ";
        if (cin >> x) break;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '
');
    }
    while (true);

    // same as above, for y ...     
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: reference variablesr in c++ 
Cpp :: how to calculate the sum of primary diagonal matrix and secondary diagonal matrix using c++ 
Cpp :: beecrowd problem 1001 solution 
Cpp :: amusia 
Cpp :: bullet physics directx 11 
Cpp :: online compiler cpp 
Cpp :: passing a 2d array cpp 
Cpp :: c++ auto loop 
Cpp :: c++ multi-dimensional arrays 
Cpp :: c++ vector remove element by value 
Cpp :: short hand if else in c++ 
Cpp :: declare a variable in cpp 
Cpp :: c++ new operator 
Cpp :: how to make an enum in c++ 
Cpp :: computer vision libraries c++ 
Cpp :: opengl text rendering with 3d rendering 
C :: remix icon cdn 
C :: random number between 2 in C 
C :: wireless app debug android 
C :: disable lua errors 
C :: how to auto run something on cmd 
C :: remove from string c 
C :: how to add two numbers in c programming 
C :: how to ban websites on mac 
C :: c define array size 
C :: how to open a website in c 
C :: install tweaks ubuntu 
C :: factorial of a number in c 
C :: memcpy c 
C :: search in gz file 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =