不接受密码

发布于 2024-09-12 11:14:48 字数 2753 浏览 3 评论 0原文

在第二课()下面的代码中,我使用了密码来进入该函数,但是当我输入该函数时,它不接受密码并显示错误的密码。通过不接受密码,我的意思是说我已经使用了gets 但它正在等待我输入密码。请不要告诉我不要使用 gets!

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
int mistakes=0,mistakes_length,len;
char temp[100];
void lesson1(void);
void lesson2(void);
void result(void);
void main(void)
{
    int choice;
    clrscr();
    printf("Enter a lesson number to practise:1-10 \n");
    scanf("%d",&choice);
    switch(choice)
    {
        case 1:
        lesson1();
        result();
        break;
        case 2:
        lesson2();
        result();
        break;
        default:
        printf("You did not entered a valid choice program quitting..\n");
        exit(0);
    }
    getch();
}
void lesson1(void)
{
    int i;
    char str1[100]="testing the typing tutor if it works";
    mistakes_length=5;
    clrscr();
    gotoxy(25,2);
    puts("Welcome to lesson 1");
    puts(str1);
    len=strlen(str1);
    for(i=0;i<len;i++)
    {
        temp[i]=getche();
        if(strncmpi(&str1[i],&temp[i],1))//does not match
        {
            mistakes++;
            sound(100);
            delay(1000);
            nosound();

        }
    }
    getch();
}
void result(void)
{
    printf("Your statsistics are as under:\nYou performed this lesson with %d mistakes\n",mistakes);
    if(mistakes>=mistakes_length)
        {
                printf("\n Your typing is very bad");//allow anything to be typed with any mistake in lesson 1

        }
        if(mistakes>3&&mistakes<5)
        {
            printf("Bad!,You need to practise this excercise more\n");
        }
        if(mistakes>=1&&mistakes<=3)
        {
            printf("Good!you can still do better\n");
        }
        if(mistakes==0)
        {
            printf("Excellent!You are qualified for the next lesson\n");
            printf("The next lessons password is \n\t\t\t:12345");
        }
}

void lesson2(void)
{
    char password[]="12345",str2[]="My name is khan and i am not a criminal";
    int i;
    mistakes=0,mistakes_length=0,
    printf("Enter password:\n");
    gets(temp);
    if(strcmp(temp,password))
    {
        gotoxy(20,25);
        printf("Wrong Password,Program Quitting.\n");
        getch();
        exit(1);
    }
    gotoxy(25,25);
    printf("Password Accpted!");
    getch();
    clrscr();
    gotoxy(25,2);
    printf("Welcome to lesson 2\n");
    printf("Type the text shown below:\n");
    puts(str2);
    len=strlen(str2);
    for(i=0;i<len;i++)
    {
        temp[i]=getche();
        if(strncmp(&str2[i],&temp[i],1));
        {
            mistakes++;
        }

    }
    getch();
}

我认为是scanf和扎堆造成的问题!

In the code below in lesson2() i have used a password to enter the function but when i enter the function it does not takes in the passord and says incorrect password.By not taking in the password,i mean to say that i have used gets but its waiting for me to input a password.please dont tell me not to use gets!

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
int mistakes=0,mistakes_length,len;
char temp[100];
void lesson1(void);
void lesson2(void);
void result(void);
void main(void)
{
    int choice;
    clrscr();
    printf("Enter a lesson number to practise:1-10 \n");
    scanf("%d",&choice);
    switch(choice)
    {
        case 1:
        lesson1();
        result();
        break;
        case 2:
        lesson2();
        result();
        break;
        default:
        printf("You did not entered a valid choice program quitting..\n");
        exit(0);
    }
    getch();
}
void lesson1(void)
{
    int i;
    char str1[100]="testing the typing tutor if it works";
    mistakes_length=5;
    clrscr();
    gotoxy(25,2);
    puts("Welcome to lesson 1");
    puts(str1);
    len=strlen(str1);
    for(i=0;i<len;i++)
    {
        temp[i]=getche();
        if(strncmpi(&str1[i],&temp[i],1))//does not match
        {
            mistakes++;
            sound(100);
            delay(1000);
            nosound();

        }
    }
    getch();
}
void result(void)
{
    printf("Your statsistics are as under:\nYou performed this lesson with %d mistakes\n",mistakes);
    if(mistakes>=mistakes_length)
        {
                printf("\n Your typing is very bad");//allow anything to be typed with any mistake in lesson 1

        }
        if(mistakes>3&&mistakes<5)
        {
            printf("Bad!,You need to practise this excercise more\n");
        }
        if(mistakes>=1&&mistakes<=3)
        {
            printf("Good!you can still do better\n");
        }
        if(mistakes==0)
        {
            printf("Excellent!You are qualified for the next lesson\n");
            printf("The next lessons password is \n\t\t\t:12345");
        }
}

void lesson2(void)
{
    char password[]="12345",str2[]="My name is khan and i am not a criminal";
    int i;
    mistakes=0,mistakes_length=0,
    printf("Enter password:\n");
    gets(temp);
    if(strcmp(temp,password))
    {
        gotoxy(20,25);
        printf("Wrong Password,Program Quitting.\n");
        getch();
        exit(1);
    }
    gotoxy(25,25);
    printf("Password Accpted!");
    getch();
    clrscr();
    gotoxy(25,2);
    printf("Welcome to lesson 2\n");
    printf("Type the text shown below:\n");
    puts(str2);
    len=strlen(str2);
    for(i=0;i<len;i++)
    {
        temp[i]=getche();
        if(strncmp(&str2[i],&temp[i],1));
        {
            mistakes++;
        }

    }
    getch();
}

I think that it is the scanf and gets together making a problem!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

日久见人心 2024-09-19 11:14:48

您的问题是,在调用 gets() 之前,您已调用 scanf("%d",&choice); (在 main() 中)。问题是控制台输入是面向行的。这意味着,尽管您只是等待输入数字,但用户必须输入以换行结尾的完整行。 %d 格式说明符仅消耗数字字符,将行的其余部分保留在缓冲区中以供下一个控制台输入调用使用;在本例中是 gets(),它在缓冲区中看到换行符并返回一个空字符串,而不等待进一步的输入。

解决方案:

scanf("%d",&choice);
while(getchar() != '\n' ) { /*no nothing*/}

Your problem is that prior to calling gets(), you have called scanf("%d",&choice); (in main()). The problem with this is that console input is line oriented. This means that although you are only waiting for a number to be entered the user has to enter a complete line ending in newline. The %d format specifier only consumes the digit characters leaving the rest of the line in the buffer to be used by the next console input call; which in this case is gets() which sees the newline in the buffer and returns an empty string without waiting for further input.

A solution:

scanf("%d",&choice);
while(getchar() != '\n' ) { /*no nothing*/}
烟柳画桥 2024-09-19 11:14:48

我认为您看到的问题实际上来自 main 方法。

您进行的 scanf() 调用仅查找“%d”而不是“%d\n”。

因此,您输入数字 2 以便尝试测试 2,并且必须在该值到达程序之前按 Enter 键。由于 scanf 不尝试匹配它,因此换行符仍然作为输入流的一部分,因此直接传递给下一个函数中的 gets() 。

因此,如果您更改 main 函数内 scanf 中的字符串,您应该会看到代码开始工作。

现在,另一方面(我知道您要求我们不要这样做,但是......)您确实不应该使用 gets()。要切换到 fgets,只需将 gets(temp) 替换为 fgets(temp,99,stdin) 即可。 99 是小于您创建的临时缓冲区大小的任何数字,在本例中临时缓冲区的大小为 100。

I think the problem you are seeing is in fact coming from the main method.

The scanf() call that you make is only looking for "%d" rather than "%d\n".

Therefore, you enter the number 2 in order to try test 2, and must press enter before that value gets to the program. Since scanf is not trying to match it, that newline remains as part of the input stream and thus is passed directly to gets() within the next function.

Therefore, if you change the string within scanf inside the main function, you should see the code start working.

Now on another note (and I know that you asked us not to but...) you really shouldn't use gets(). In order to switch to fgets, just replace gets(temp) with fgets(temp,99,stdin). That 99 is any number that is less than the size of the temp buffer you have made, which in this case has size 100.

放飞的风筝 2024-09-19 11:14:48

尝试像这样打印 temp:gets(temp); 之后的 printf("[%s]\n", temp); 以查看 中保存的内容温度。它可能会将之前给定的来自 lesson1 函数的输入作为输入?

如果是这种情况,类似这样的事情可能会解决问题:

char other_tmp[5];
gets(other_tmp);
gets(temp);

尽管在这种情况下,您应该更好地纠正 lesson1 函数。

Try printing temp like this: printf("[%s]\n", temp); after the gets(temp); to see what is saved in temp. It may takes as input a previous given input from lesson1 function?

If this is the case, something like this maybe will fix the problem:

char other_tmp[5];
gets(other_tmp);
gets(temp);

although in such a case you should better correct the lesson1 function instead.

妞丶爷亲个 2024-09-19 11:14:48

它应该可以工作。
编辑您的代码:

int r = gets(temp);
printf("returned: %d, Entered: %s\n", r, temp);

并在此处发布结果

it supposed to work.
edit your code:

int r = gets(temp);
printf("returned: %d, Entered: %s\n", r, temp);

and post here the result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文