[英]Banking Record I want to ask the user to input a 4 digit pin and no letters
I want my program to ask the user to input a 4 pin code (only numbers) and that it will accept no letters.我希望我的程序要求用户输入 4 针代码(仅数字)并且不接受任何字母。 It does accept no letters but each time I enter a pin the program doesn't care about the 4 numbers that I ask, you can input 1 number , so as, 10 numbers and it will still work.它不接受任何字母,但每次我输入一个 pin 时,程序都不关心我问的 4 个数字,您可以输入 1 个数字,因此,10 个数字仍然可以工作。 I want it to absolutely input a 4 pin code I tried many solution, such as, the if(pin>999 && pin<=9999) technique and it doesn't work... there is my code so far for that part.我希望它绝对输入一个 4 针代码,我尝试了许多解决方案,例如 if(pin>999 && pin<=9999) 技术,但它不起作用......到目前为止,我的代码是该部分。 Many thanks in advance for the help of those who can explain me my wrongs非常感谢那些可以向我解释我的错误的人的帮助
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
bool clientnumberlength(string pin) {
bool status = true;
if (pin.length() != 4) {
status = false;
}
return status;
}
int main() {
int clientnumber = 4;
cout << "Welcome to my bank incorporation" << endl;
cout << "Please enter your 4 pin customer number" << endl;
while (!(cin >> clientnumber)) {
string PIN;
cin >> PIN;
if (clientnumberlength(PIN) == 4) {
cout << "PIN accepted" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
else {
cout << "PIN refuser" << endl;
cout << "Please enter your 4 pin customer number" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
return 0;
}
You can't really put restrictions on what is inputted through std::cin
.您不能真正限制通过std::cin
输入的内容。 You can either hope the user will input numbers, or look into making a more structured app with events and everything, which would be more time consuming.您可以希望用户输入数字,或者考虑使用事件和所有内容制作更结构化的应用程序,这会更耗时。 You can also have the user input a string, remove any non-numerical characters, check if the length is 4 and eventually ask to input another one if the conditions are not met.您还可以让用户输入一个字符串,删除任何非数字字符,检查长度是否为 4,如果不满足条件,最终要求输入另一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.