【发布时间】:2021-03-13 06:59:18
【问题描述】:
代码如下:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <iostream>
#define repeat(n)
#define endrepeat
int main()
{
int login;
char Username [4];
char Password [4];
printf("\t\tPlease enter your user name: ");
scanf("%s", Username);
printf("\t\tPlease enter your user password: ");
scanf("%s", Password);
if (strcmp (Username,"Jane")==0 && strcmp(Password,"1234")==0 ||
strcmp(Username,"Arin")==0 && strcmp (Password,"3201")==0 || strcmp
(Username,"Jake")==0 && strcmp (Password,"4312")==0 || strcmp
(Username,"Jazz")==0 && strcmp (Password,"4456")==0)
{
printf("ACCESS GRANTED");
system("CLS");
getch();
}
else
{
printf("INVALID USERNAME OR PASSWORD ENTERED.\n");
printf("\t\tPlease enter your user name: ");
scanf("%s", Username);
printf("\t\tPlease enter your user password: ");
scanf("%s", Password);
}
int repeat;
repeat = 0;
do {
printf("INVALID USERNAME OR PASSWORD ENTERED.\n");
printf("\t\tPlease enter your Username: ");
scanf("%s", Username);
printf("\t\tPlease enter your password: ");
scanf("%s", Password);
repeat + 1 ;
} while(repeat<3);
endrepeat;
return 0;
}
我希望代码在退出前重复 3 次。当我尝试运行代码时,它会不断重复。我是编码新手,很抱歉,如果它很容易解决。 :) 我尝试过使用不同的重复方法,但我仍然遇到同样的问题。
【问题讨论】:
-
学习调试器的最佳机会。观察计数器的值如何变化。您的代码中的 C++ 在哪里?
-
repeat + 1;行不会更改变量值。我想你的意思是写repeat += 1;。由于您只增加 1,因此您可以使用增量运算符++repeat;。另外,你为什么不使用for循环?您可以轻松完成for (size_t repeat {0}; repeat < 3; ++repeat) { ... }