0

Number input only, no text

//------------------------- int Input =0; cin >> Input; cout << "Your input is: " << Input; //------------------------- My question is, how can I make the Input variable accept number only, and not text? Because when I input text, it says error, I want to make a code without error if possible.

22nd Oct 2020, 11:28 AM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar
8 Answers
+ 2
Jonathan Jundarino if you will type any String it will give you error becoz you defined type of Variable int type . If u want to take input String then u can write like this char str[20]; cin>>str ;
22nd Oct 2020, 11:57 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Jonathan Jundarino if you will type any String it will give you error becoz you defined type of Variable int type . If u want to take input String then u can write like this char str[20]; cin>>str ;
22nd Oct 2020, 11:57 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
You can put cin in if statement. It will evaluate true if the input is satisfied and correct, else false. int z; if(cin >> z) //Code for input number else //Code for not input number
22nd Oct 2020, 11:45 AM
你知道規則,我也是
你知道規則,我也是 - avatar
0
Vadivelan, thanks, I use"do while", just to make the input user input repeat if wrong, but when I type text, or string, it produce error, thaks man!!
22nd Oct 2020, 11:45 AM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar
0
What errors you facing its very simple declear one variable which type will be int float ,double any type which u want thn use cin to take Input #include <iostream> using namespace std; int main() { int num; cout<<"enter number"; cin>>num; cout<<endl<<"Entered number is "<<num; return 0; }
22nd Oct 2020, 11:45 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
CarrieForle and ♨️TEJAS MK-82♨️ , I have a int Input; cin >> Input; If I type "CarrieForle love TEJAS MK-82" and not a number, it will error, haha anyway, it's already solve thanks for answering :)
22nd Oct 2020, 11:53 AM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar
0
♨️TEJAS MK-82♨️ thanks, I learned again, I should finish C++ course here in Sololearn 😄
22nd Oct 2020, 12:05 PM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar