简体   繁体   English

我需要一个要求用户输入图钉的功能,经过3次错误尝试后,他们编程终止

[英]I need a function that ask the user to enter a pin and after 3 wrong attempts, they program terminates

I have to write an ATM program for a class, and i cant figure out how to make a function that will ask the user for a pin and if the pin is entered incorrectly three times the program will display an exit message then terminate.... this is what i have some far. 我必须为一个类编写一个ATM程序,但我想不出如何创建一个功能来询问用户输入的密码,并且如果密码输入错误三遍,该程序将显示退出消息,然后终止...这就是我要说的。 I think my issue is i don't know the correct syntax to handle my issue. 我认为我的问题是我不知道处理我的问题的正确语法。

I know i will need a for loop but not sure how exactly to construct it. 我知道我将需要一个for循环,但不确定如何构造它。

void validate_acc(){
     int user_acc_try;

     printf("Please enter your account number: ");
     scanf("%d", &user_acc_try);

     if(user_acc_try != account_number){
                     printf("You entered the wrong account number");
                     }
     else{
          printf("");
          }
}

void validate_pin(){
     int user_pin_try;

     printf("Please enter your pin number: ");
     scanf("%d", &user_pin_try);

     if(user_pin_try != pin){
                     printf("You entered the wrong pin number.");
                     }
     else{
          printf("");
          }
}

void validate(){
     validate_acc();
     validate_pin();
}

Secondly, since i can only post every 90 minutes might as well ask another question, I do not know how to make a function go back to the beginning of my program like for example say after an deposit, what is the logic i would need to use to have a function go back to the beginning of my main function. 其次,由于我只能每90分钟发布一次,所以不妨再问一个问题,我不知道如何使函数返回到程序的开头,例如在存入资金后说,我需要做的逻辑是什么?使用具有功能可以回到我主要功能的开头。 I know of goto labels , that didnt seem to work when i put it in front of my main function like so... 我知道goto labels ,当我像这样将它放在我的主要功能前面时,它似乎不起作用。

MAIN:
int main()

i would put goto main; 我会把goto main; in another function and i would get a.... Main is not defined error. 在另一个功能,我会得到一个...。主要是没有定义错误。 I have read a few different questions on here about labels but cant find anything that helps, if someone could guide me in the right direction, you would be giving me a great deal of relief. 我在这里阅读了有关标签的几个不同问题,但找不到任何有帮助的东西,如果有人可以向正确的方向指导我,您将为我带来很大的缓解。

thank you in advance. 先感谢您。

It's a good idea to write out a flow chart for things like this if you can't figure out how to do it in code. 如果您不知道如何在代码中执行此操作,则为此类事件写出流程图是个好主意。

Please do not use labels/goto in C. It's a nasty habit and it's not needed. 请不要在C语言中使用标签/转到。这是一个令人讨厌的习惯,因此不需要。

You know how to use if statements to make a decision; 您知道如何使用if语句做出决定; think about how you would use a while loop to try to make the same decision over and over again until something changes. 想一想您将如何使用while循环反复尝试做出相同的决定,直到发生变化。 For instance, in pseudo-code (because I don't want to do your work for you) 例如,用伪代码(因为我不想为你做你的工作)

user_has_not_entered_correct_pin = true user_has_not_entered_correct_pin = true
retries_left = 3 retries_left = 3
while retries_left > 0 and user_has_not_entered_correct_pin: 而retries_left> 0和user_has_not_entered_correct_pin时:
get pin 得到别针
if pin_is_not_correct(pin) retries = retries - 1 如果pin_is_not_correct(pin)重试=重试-1
else user_has_not_entered_correct_pin = false 否则user_has_not_entered_correct_pin = false
end while 一会儿结束

I am limited on time right now, so I will just post a quick help. 我现在的时间有限,所以我将发布一个快速帮助。 I would suggest start researching loops in C. Since this is for a class, the book you are using should have information in it about for loops and while loops, but if not, a simple Google search can help a lot. 我建议开始研究C语言中的循环。由于这是针对一类的,因此您正在使用的书中应该包含有关for循环和while循环的信息,但如果没有,那么简单的Google搜索会有所帮助。

With a quick search on Google, this site seemed like a decent site for basic information on loops: Loops in C 通过Google的快速搜索,该站点看起来像一个不错的站点,提供了有关循环的基本信息: C中的循环

It has links and examples of using a for loop, a while loop, a do...while loop and nested loops which should help you solve your problem. 它具有使用for循环,while循环,do ... while循环和嵌套循环的链接和示例,这些应该可以帮助您解决问题。

Edited to add: In your post you mentioned that you think the problem is that you don't know the syntax that you need. 编辑添加:在您的帖子中,您提到您认为问题是您不知道所需的语法。 It is for that reason that I pointed you to a location that can help you with the syntax that you need to solve your problem rather than show you directly how to solve the problem. 出于这个原因,我为您指出了一个可以帮助您解决问题的语法的位置,而不是直接向您展示如何解决问题。 I hope that this helps you not only with this question, but going forward in your class as well. 我希望这不仅对您有帮助,而且对您在课堂上的发展也有帮助。

Keep a count variable like I have did below and check the number of attempts: I don't see a need for goto here. 像下面所做的那样保留一个count变量,并检查尝试次数:我认为这里不需要goto The same logic can be used for checking pin also. 同样的逻辑也可以用于检查引脚。

int i=0;
while(1)
{
  if(i>2)
  {
    printf("Maximum attempts reached\n");
    break;
  }
  printf("Enter the acc_num\n");
  scanf("%d", &user_acc_try);
  if(acc_num == saved_acc_num)
  {
    // Do your stuff
  }
  i++;
}

Return value from validate_pin() int validate_pin(){... return 0; .... return 1;} 从validate_pin()返回的值int validate_pin(){... return 0; .... return 1;} int validate_pin(){... return 0; .... return 1;} and test it in the main() or your validate(). int validate_pin(){... return 0; .... return 1;}并在main()或您的validate()中对其进行测试。

int i=0;
int result=0;
while ( (result==0)&&(i<3) ){
  result=validate_pin();
  i++;
}

Dont use goto , learn to use loops. 不要使用goto学习使用循环。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在编写一个程序,要求用户以24小时格式输入时间并显示计划航班的最接近出发时间 - I am writing a program which ask user to enter time in 24 hour format and diplay closest departute time of schedule flight 嗨,我需要让用户输入 3 个值,直到他输入 EOF(Ctrl+z)。EOF 部分不起作用,用户每次输入 6 个值,我需要 3 个 - Hi,I need to ask the user to enter 3 values until he enters EOF(Ctrl+z).The EOF part isn't working and the user enters 6 values each time and I need 3 C程序在if / else之后终止,或者在我使用fputs / fgets时重复 - C Program terminates after if/else or repeats if I use fputs/fgets 我想让程序询问用户他们想使用哪种付款方式(在线/卡)。 但是我的代码会显示错误的 output - i want the program to ask user which payment method they wanna use (ONLINE/CARD) . But my code will show the wrong output 要求用户重复该程序 - ask user to repeat the program 如果我使用 scanf 程序终止 - Program terminates if I use scanf 程序帮助! 用户点击进入程序后,程序结束 - Program help! After user hits enter the program ends 有没有办法制作一个错误计数器,在特定数量的用户输入后终止程序? - Is there a way to make an error counter that terminates the program after a specific number of user inputs? 在c语言中,如何创建一个不需要用户按Enter键的scanf函数? - In c language, how can i make a scanf function that doesnt need to require the user to press Enter? 用户定义的信号1终止我的POSIX程序! - User defined signal 1 terminates my POSIX program!
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM