0
How do i force a person to enter a specific number of numbers?
I entered some code so that someone enters 4 numbers and the sum of these numbers determine what is written then, how to change it so that he will only be able to enter 2 digits maximum and then be able to enter the next number which is also 2 digits and not let him add as many as he wants?
7 Réponses
+ 2
You could simply put an if statement that looks at the letter count and loops back if it is greater than what you want.
+ 2
#include <iostream>
int main()
{
std::string number;
std::cin >> number;
while(number.length() > 2 ) // check number for 2-digits
{
std::cin >> number; // read new number
}
int n = std::stoi(number); // converts string to int
std::cout << n; // print the input
return 0;
// other way
// int number;
// std::cin >> number;
// while(number > 100 || number < 10)
// check for number range
// {
// std::cin >> number;
// read in next number because first wasn't valid
// }
// std::cout << number; // print input
// return 0;
}
+ 1
I don't think that there's a way to catch the letter count before reading the numbers. A possible way is to read in the numbers as String check the letters count and if it's not like you want it to be, force the user to enter a valid number again.
0
can you please show me how to do that
0
please i really best understand by seeing an example so can anyone please just show me an example how instead of explaining first, that would be really helpful
0
is your code on code playground?
0
i use c4droid