How do i force a person to enter a specific number of numbers? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

22nd Nov 2016, 1:50 PM
Husam Alsheikh
Husam Alsheikh - avatar
7 Answers
+ 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.
22nd Nov 2016, 1:52 PM
Matthew Horn
Matthew Horn - avatar
+ 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; }
22nd Nov 2016, 2:33 PM
Andreas K
Andreas K - avatar
+ 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.
22nd Nov 2016, 1:58 PM
Andreas K
Andreas K - avatar
0
can you please show me how to do that
22nd Nov 2016, 1:52 PM
Husam Alsheikh
Husam Alsheikh - avatar
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
22nd Nov 2016, 2:01 PM
Husam Alsheikh
Husam Alsheikh - avatar
0
is your code on code playground?
22nd Nov 2016, 2:10 PM
Matthew Horn
Matthew Horn - avatar
0
i use c4droid
22nd Nov 2016, 2:12 PM
Husam Alsheikh
Husam Alsheikh - avatar