hello;

I want to make the user write one character, not more than one and save it to char.

so how can I make the input limited to one char?

If I work with number only, how can I lock the letters input, making the user to input only numbers?

____________________________
Faizalw, C# and C++ prog learner
======================

If I work with number only, how can I lock the letters input, making the user to input only numbers?

You can do something like this

int number;
if(cin>>number){
//code
}
else{
//code
}

how can I make the input limited to one char?

Declare a character variable to hold the input.
Use >>, or its equivalent in C, to accept the input.
Flush the input buffer to remove anything else the user entered.

If I work with number only, how can I lock the letters input, making the user to input only numbers?

Short answer, you can't make the user input only numbers, or only char, or anything at all really. All you can do is validate that the input is the form, or can be tranformed into the form, you requested.

One approach to data validation is only accept input as a string and then transform the string into whatever other data type you want once you have validated it. The other scheme is to use an input stream and it's methods to do the validation. Neither scheme is straightforward, but it is a doable project once you have all your parameters in place.

This article has been dead for over six months. Start a new discussion instead.