我的问题是我被要求编写一个菜单驱动程序,要求用户输入一个4位数代码2.加密代码并验证设定的4位数代码。 3.退出程序。除了用户选择退出时,程序应该在用户完成所选选项后循环回到开始菜单。即,在加密和验证根据设定的4位数代码输入的代码之前,用户将输入其代码并返回菜单。
下面我的代码没有循环回菜单也没有正确运行当我选择选项1它要求我输入4位数代码两次。我已经尝试过无疲劳来解决这个问题,但无济于事。你能给我的任何帮助都会很棒。
#include <stdio.h>
#include <stdlib.h>
#define CODE 4
//prototypes
int enter_code (int* code_arr);
int encrypt_code (int* pass_code, int* user_code);
int main(void)
{
//declare variables
int password[CODE] = {0};
int passOK[CODE] = {4,5,2,3};
int option;
int exit1;
int code;
//do while loop
do
{
//print the menu on screen
printf("\t \t \t \t Pearse Security \n \n");
printf("\t \t \t1 - Enter the access code\n");
printf("\t \t \t2 - Enter the encrypt code and verify\n");
printf("\t \t \t3 - Exit the program \n");
//scan for user input
scanf("%d",& option);
switch(option)
{
case 1:
{
code =enter_code(password);
break;
}
case 2:
{
if (encrypt_code(passOK, password))
printf("You unlocked the vault\n");
break;
}
case 3:
{
// prompt user to a key to exit
printf("\n You choose to exit the program.\n Press a key to exit\n ");
getchar();
exit(0);
break;
}
default:
{
printf("You must enter a number between 1-5\n");
}
} // end switch()
if (!enter_code(password))
{
printf ("Bad password entry\n");
}
else
{
if (encrypt_code(passOK, password))
{
printf("You unlocked the vault\n");
}
else
printf("You don't know the passcode\n");
}
return 0;
}//end do
while(exit1!=4 & exit1 <5);
}//end main()
//enter code function()
int enter_code (int* code_arr)
{
//declare variables for enter_code()
int i;
//prompt user to enter the 4 digit code
printf("Enter your 4 digit code\n");
for(i=0;i<CODE;i++)
{
if (scanf("%d", &code_arr[i]) != 1)
{
return 0;
}//end if()
}//end for()
return 1;
}//end enter_code()
//encrypt code and verify function
int encrypt_code (int* pass_code, int* user_code)
{
//variables for encrypt_code()
int i;
for(i=0;i<CODE;i++)
{
if (pass_code[i] != user_code[i])
{
return 0;
}//end if()
}//end for()
return 1;
}//end encrypt_code()
The output of this program is
Enter 4 digits
4
5
2
3
Enter 4 digits
4
5
2
3
您已解锁保险库 程序结束了 为什么要求我输入两次代码,为什么不循环回菜单。
答案 0 :(得分:0)
我收到了关于此行的两个编译器警告
while(exit1!=4 & exit1 <5);
首先它应该是
while(exit1!=4 && exit1 <5);
它也是未定义的行为,因为从未为exit1
分配了值。
我建议您用
替换该行while(1);
因为case 3:
包含exit()
。
接下来,您说enter_code()
被调用了两次。原因是它被调用了两次:一次在case 1:
内,再次在switch()
代码块之外。
以下是修改后的main()
,其中删除了switch()
块下方的剩余内容,并向case 2:
添加了失败消息
int main(void)
{
int password[CODE] = {0};
int passOK[CODE] = {4,5,2,3};
int option;
int code;
do
{
printf("\t \t \t \t Pearse Security \n \n");
printf("\t \t \t1 - Enter the access code\n");
printf("\t \t \t2 - Enter the encrypt code and verify\n");
printf("\t \t \t3 - Exit the program \n");
scanf("%d",& option);
switch(option)
{
case 1: {
code =enter_code(password);
break;
}
case 2: {
if (encrypt_code(passOK, password))
printf("You unlocked the vault\n");
else
printf("You don't know the passcode\n");
break;
}
case 3:{
// prompt user to a key to exit
printf("\n You choose to exit the program.\n Press a key to exit\n ");
getchar();
exit(0);
break;
}
default: {
printf("You must enter a number between 1-5\n");
}
}
} while(1);
return 0;
}