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.
8 Respostas
+ 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 ;
+ 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 ;
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
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!!
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;
}
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 :)
0
âšïžTEJAS MK-82âšïž thanks, I learned again, I should finish C++ course here in Sololearn đ