帮助我完成我的项目。 [英] Help me with my project.
问题描述
你好,我在学校的项目工作。我们的项目是关于ATM的。
我在输入用户密码时首先输入星号,就像在ATM中一样。
但是因为它是一个字母,我无法将它与数字比较如
if(pin> = 0)...它将无效。你能帮助我吗?
我尝试过的事情:
char pin [4];
string pass =afgt;
int p;
int i = 1;
cout<< ENTER PIN\t :: \t;
if((pin> = 0)&&(pin< = 9)){
for(i = 1; i <= 4; i ++){
pin [i] = getch();
cout<< *;
}
}
这个......
Hello, I am working in our project in school. Our project is about ATM.
I first put asterisk while inputing the user's PIN, just like in ATM.
but since it is a char I can't compare it to numbers like
if (pin >= 0) ... it won't work. Can you help me?
What I have tried:
char pin[4];
string pass = "afgt";
int p;
int i=1;
cout << "ENTER PIN\t::\t";
if ((pin >= 0) && (pin <= 9)){
for (i=1;i<=4;i++){
pin[i]=getch();
cout << "*";
}
}
this one....
推荐答案
你应该学会使用调试器。这将帮助您通过查找自己的错误来学习。
实际上,您将用户的输入保存在IF片段中的变量中。
所以,如果该变量尚未保存,那么如果((pin> = 0)&&(pin< = 9))
声明要匹配吗?
其次,如果用户输入错误(如字母)会发生什么?事实上,该程序将在没有给出任何理由的情况下结束。
我建议您考虑想要实现的目标,将其写入纸张然后尝试翻译代码。这样可以让您更轻松地订购您的想法。
you should learn to use the debugger. That will help you learn by finding your own errors.
As it is, you are saving the user's input in the variable within the IF snippet.
So, if the variable has not been saved yet, how is theif ((pin >= 0) && (pin <= 9))
statement going to match?
Secondly, what happens if the user inputs something wrong (e.g. a letter)? As it is, the program would end without giving any reason.
I recommend you to think what you want to achieve, put it in paper and then try to translate it into code. That should make it a bit easier for you to order your ideas.
if ((pin >= 0) && (pin <= 9)){
您已将pin声明为4个字符的数组,因此您无法将其与数字进行比较。你应该依次检查每个角色:
You have declared pin as an array of 4 characters so you can not compare it to a number. You should check each character in turn like:
pin[i] = getch();
if (pin[i] < ‘0’ || pin[i] > ‘9’)
{
// error message here
}
事实上,
f((pin> = 0)&&(pin< ; = 9))
f ((pin >= 0) && (pin <= 9))
是完全错误的(除了无用之外)。
它看起来你可以使用 C ++
字符串
秒。如果您还被允许使用现代 C ++
功能,请尝试:
is plain wrong (other than useless).
It looks you're allowed to use C++
string
s. If you are also allowed to use modern C++
features then try:
#include <iostream>
#include <array>
using namespace std;
int main()
{
using Code = array<char, 4>;
const Code pwd{'a','f','g','t'};
Code pin;
cout << "please enter the pin\n";
for (auto & c : pin)
cin >> c;
if ( pwd == pin)
cout << "that's OK\n";
else
cout << "sorry, no luck\n";
}
这篇关于帮助我完成我的项目。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!