Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我希望我的代码重复3次,但循环没有结束。有人知道我如何解决这个问题吗?_C++_Repeat - Fatal编程技术网

C++ 我希望我的代码重复3次,但循环没有结束。有人知道我如何解决这个问题吗?

C++ 我希望我的代码重复3次,但循环没有结束。有人知道我如何解决这个问题吗?,c++,repeat,C++,Repeat,代码如下: #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

代码如下:

    #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;
    }
#包括
#包括
#包括
#包括
#定义重复(n)
#定义endrepeat
int main()
{
int登录;
字符用户名[4];
字符密码[4];
printf(“\t\t请输入您的用户名:”);
scanf(“%s”,用户名);
printf(“\t\t请输入您的用户密码:”);
scanf(“%s”,密码);
如果(strcmp(用户名,“Jane”)==0和&strcmp(密码,“1234”)==0 ||
strcmp(用户名,“Arin”)==0和strcmp(密码,“3201”)==0 | | strcmp
(用户名,“Jake”)==0&&strcmp(密码,“4312”)==0 | | strcmp
(用户名,“Jazz”)==0&&strcmp(密码,“4456”)==0)
{
printf(“授权访问”);
系统(“CLS”);
getch();
}
其他的
{
printf(“输入的用户名或密码无效。\n”);
printf(“\t\t请输入您的用户名:”);
scanf(“%s”,用户名);
printf(“\t\t请输入您的用户密码:”);
scanf(“%s”,密码);
}
int重复;
重复=0;
做{
printf(“输入的用户名或密码无效。\n”);
printf(“\t\t请输入您的用户名:”);
scanf(“%s”,用户名);
printf(“\t\t请输入您的密码:”);
scanf(“%s”,密码);
重复+1;

}而(repeat增加
repeat
变量,如下所示:

repeat = repeat + 1;
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))
{
repeat = repeat + 1

if
语句中使用括号分隔逻辑并避免得到错误结果,如下所示:

repeat = repeat + 1;
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))
{
repeat = repeat + 1
摆脱
else
语句,只需在
if
语句中使用
do-while
循环即可。或者更好的是,您可以创建一个单独的函数来接收和验证输入。这将使所有内容更具可读性

比如:

bool requestInput()
{
   printf("\t\tPlease enter your Username: ");
   scanf("%s", Username);
   printf("\t\tPlease enter your 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))
   {
       return true;
   }

   printf("INVALID USERNAME OR PASSWORD ENTERED.\n");
   return false;
}


int main()
{
   bool accessGranted;
   int repeat = 0;

    do {
        accessGranted = requestInput();
        repeat++;
    } while(repeat<3 && !accessGranted);

    if(accessGranted)
    {
       printf("ACCESS GRANTED");
       // ...
    }
}
bool requestInput()
{
printf(“\t\t请输入您的用户名:”);
scanf(“%s”,用户名);
printf(“\t\t请输入您的密码:”);
scanf(“%s”,密码);
如果((strcmp(用户名,“Jane”)==0&&strcmp(密码,“1234”)==0)|
(strcmp(用户名,“Arin”)==0和strcmp(密码,“3201”)==0)
(strcmp(用户名,“Jake”)==0和strcmp(密码,“4312”)==0)
(strcmp(用户名,“Jazz”)==0和strcmp(密码,“4456”)==0))
{
返回true;
}
printf(“输入的用户名或密码无效。\n”);
返回false;
}
int main()
{
授予布尔访问权;
int repeat=0;
做{
accessgrated=requestInput();
重复++;

}while(repeat将1添加到
repeat
变量后,必须保存结果,例如:

repeat = repeat + 1;
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))
{
repeat = repeat + 1
较短的备选方案是:

repeat += 1


一般情况下,在这种情况下,我将使用for循环。< /p>学习计数器的最佳机会。观察计数器的值如何变化。代码中的C++是什么?行>代码>重复+ 1;< /代码>不改变变量值。我认为您打算编写<代码>重复+=1;< /代码>。由于您只增加了1,所以可以使用增量运算符
++repeat;
。另外,为什么不为
循环使用
?您可以轻松地为(size\u t repeat{0};repeat<3;++repeat){…}执行
如果我想让它在输入正确的用户名和密码后退出循环,我是否要创建另一个If语句?@jessi_jebbie如果变量
repeat
达到值3或
accessgrated
变为
true
do while
循环将自行终止。无需额外的
If
语句。