简体   繁体   English

我的C程序中运行时错误的原因&?

[英]reason for runtime error in my c program &?

        #include<stdio.h>
        main()
        {char *names[4];
        int i,a;
        printf("ënter the guests names\n");
               for(i=0;i<=3;i++)
               {
                 scanf("%s",names[i]);
               }
      char *yourname;
      printf("\nenter your name ");
      scanf("%c",yourname);
                    for(i=0;i<=3;i++)
                       {a=strcmp(names[i],yourname);
                         if(a==0)
                         printf("\nwelcome");
                         break;
                       }
     if(a!=0)
     printf("\naccess denied");
     return 0;
     }

this is a program to check your entry in a show. 这是一个检查节目中条目的程序。 first we give permitted names & then it asks your name ,it compares your name with the names in the guest list. 首先,我们提供允许的姓名,然后询问您的姓名,它将您的姓名与来宾列表中的姓名进行比较。

im getting runtime error, plz tell me the correction.i want to use pointers to string so plz suggest correction in the existing program 即时通讯收到运行时错误,请告诉我更正。我想使用指向字符串的指针,因此请在现有程序中建议更正

when i run this program in devc++ after entering first name it gives program.exe stopped working. 当我输入名字后在devc ++中运行该程序时,它给出了program.exe停止工作的功能。

The code will be like this: 代码将如下所示:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    int main()
    {
        char *names[4];
        int i,a;
        printf("ënter the guests names\n");
        for(i=0;i<=3;i++) {
                names[i] = (char*) malloc(100 * sizeof(char));
            scanf("%s", names[i]);
        }
        char yourname[100];
        printf("\nenter your name ");
        scanf("%s",yourname);
        for(i=0;i<=3;i++) {
            a = strcmp(names[i], yourname);
            if (a == 0) break;
        }
        if (a==0)
            printf("\nwelcome");
        else printf("\naccess denied");

        for(i=0;i<=3;i++)
            free(names[i]);

        return 0;
    }

Your code have to be formated so we can give you a better answer. 您的代码必须经过格式化,以便我们为您提供更好的答案。

Now, use gets to take your input, verify the guest match with your name inside the for loop, stop the loop when a match is found. 现在,使用gets进行输入,在for循环内验证guest虚拟机是否与您的名字匹配,找到匹配项后停止循环。

#include<stdio.h>
#include<string.h>
int main()
{
    char names[4][20];
    int i = 0;
    int a = 0;
    printf("Enter guests names: \n");

    for(i=0; i<3; i++)
    {
        gets(names[i]);
    }
    char yourname[20];
    printf("\n Enter your name ");
    gets(yourname);

    printf("\n Verify access right:");
    for(i=0; i<3; i++)
    {    
        a=strcmp(names[i], yourname);
        if(a==0)
        {
            printf("\n welcome");
            break;
        }
        else
        {
            printf("\n access denied");
        }
    }  

    return 0;
}

Although this looks like a homework assignment. 虽然这看起来像是一项家庭作业。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM