【问题标题】:Basic Main Menu code not working基本主菜单代码不起作用
【发布时间】:2012-05-19 10:04:36
【问题描述】:

所以我要做的是编写一个带有主菜单的简单程序,用户可以在其中填写项目的详细信息、列出所有项目、删除选定的项目或退出程序。项目将保存在一个数组中。函数将使用结构。

我已经编写了一个功能代码,它编译得很好(我知道这很重要),但到目前为止只有第 4 个选项“退出”有效。其他 3 个选项似乎不起作用。

这是我的代码:

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

struct fullname
{
        char name[10];
        char surname[10];
};
struct meletes
{
        int identifier;
        struct fullname customer;
        char date[10];
        float price;
};
void initialize(struct meletes projects[10]);
struct meletes newp(struct meletes projects[10]);
struct meletes list(struct meletes projects[10]);
struct meletes deletep(struct meletes projects[10]);

int main(void)
{
int choice,k;
struct meletes projects[10];

void initialize(struct meletes projects[10]);

printf("Main Menu\n ========\n");
printf("Please choose a function from below:\n");
printf("1.New Project\n2.Delete\n3.List\n4.Exit\n");
scanf("%d", &choice);

    while ((choice != 1) && (choice != 2) && (choice != 3) && (choice != 4))
          {

           printf("You have chosen a wrong function, please use numbers 1-4:\n\n");
           printf("Main Menu\n ========\n");
           printf("Please choose a function from below:\n");
           printf("1.New Project\n2.Delete\n3.List\n4.Exit\n");
           scanf("%d", &choice);
           }

          while (choice != 4)
          {
          switch (choice) {

                 case 1:
                 {
                  struct meletes newp(struct meletes projects[10]);

                  }

                 case 2:
                 {
                  struct meletes deletep(struct meletes projects[10]);

                  }

                 case 3:
                 {
                  struct meletes list(struct meletes projects[10]);

                  }
                         }


            printf("Main Menu\n ========\n");
            printf("Please choose a function from below:\n");
            printf("1.New Project\n2.Delete\n3.List\n4.Exit\n");
            scanf("%d", &choice);             

          }        

          printf("Thank u.\n");

system("pause");
return 0;
}

 void initialize(struct meletes projects[10])
 {
int l;
 for(l=0; l<10; l++)
  {
          projects[l].identifier = 00000;
          projects[l].price = 0.00;
          strcpy(projects[l].customer.name,"----------");
          strcpy(projects[l].customer.surname,"----------");
          strcpy(projects[l].date, "0/0/0");
  }          
}          

struct meletes newp(struct meletes projects[10])
{
        int i;
        for(i=0; i<10; i++)
        {
                  if (projects[i].identifier == 00000)
                 {
                    scanf("Please enter the project's identifier %d\n",        &projects[i].identifier);
                    scanf("Name:%s\n", &projects[i].customer.name);
                    scanf("Surname:%s\n", &projects[i].customer.surname);
                    scanf("Give the date in dd/mm/yyyy! format :%c\n", &projects[i].date);
                    scanf("Price:&f\n", &projects[i].price);
                 }
             break;
        }                

}

struct meletes deletep(struct meletes projects[10])
{
       int j,id;
        for (j=0; j<10; j++)
        {
            if (projects[j].identifier != 00000)     //Emfanizei oles tis meletes pou den ine diegrammenes
               {
               printf("%d\n", projects[j].identifier);
               }
        }

        scanf("\nPlease insert the identifier of the project u want to delete:%d", &id);

        for(j=0; j<10; j++)
        {                       
          projects[j].identifier = 00000;
          projects[j].price = 0.00;
          strcpy(projects[j].customer.name,"----------");
          strcpy(projects[j].customer.surname,"----------");
          strcpy(projects[j].date, "0/0/0");
        }

}

struct meletes list(struct meletes projects[10])
{
       int k;
        for(k=0; k<10; k++)
        {
                 if (projects[k].identifier != 00000); 
                    {
                    printf("         Project %d:", k);
                    printf("\nIdentifier:%d\n", projects[k].identifier);
                    printf("Name:%s\n", projects[k].customer.name);
                    printf("Surname:%s\n",projects[k].customer.surname);
                    printf("Date:%s\n", projects[k].date);
                    printf("Price:%d\n", projects[k].price);
                    }
        }

 }        `

任何想法都会非常感激

【问题讨论】:

    标签: c function menu structure main


    【解决方案1】:

    main 中的以下几行声明了函数,但不要调用 thyem

    void initialize(struct meletes projects[10]);
    struct meletes newp(struct meletes projects[10]);
    struct meletes deletep(struct meletes projects[10]);
    struct meletes list(struct meletes projects[10]);
    

    你需要调用函数,像这样:

    initialize(projects);
    newp(projects);
    deletep(projects);
    list(projects);
    

    最后三个似乎也返回了meletes 结构,您需要声明一个meletes 类型的新变量并酌情使用它:

    struct meletes proj = newp(projects);
    // use proj as appropriate
    

    再次查看函数newpdeleteplist 返回任何内容,如果您使用上述建议的分配,这将导致问题。您要么需要返回一个项目(并且分配会很好),要么将其返回类型更改为void(并且不要执行分配)。

    【讨论】:

    • 好的,我已经更正了您的第一点,现在可以正确调用函数,但不能正常工作:S 正在处理其他函数
    • 确保scanfprintf 中的格式说明符正确(例如,%dlist() 中有浮点数price
    • 好的,更正了一个 2。到目前为止唯一的工作功能是“列表”功能。初始化函数和 newproject 函数只是给我废话。当我选择“1”进入一个新项目时,它只会给我一堆随机数,我无法输入任何内容。知道是什么原因造成的吗?
    • 对于初始化程序:您将 11 个字符复制到 10 个字符的空间中(10 个破折号和一个终止 '\0'),这将覆盖不属于这些缓冲区的其他内存。 newp()的:使用printf+scanfscanf并没有按照你的想法做(阅读字符串参数here的效果)
    • 如果这是家庭作业,您可能需要添加“家庭作业”标签。
    【解决方案2】:

    这是一个工作示例。请仔细看一下,让我们知道你学到了什么。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct 
    {
            char name[10];
            char surname[10];
    }fullname;
    
    typedef struct 
    {
            int identifier;
            fullname customer;
            char date[10+1]; /* need a NULL */
            float price;
    }meletes;
    
    void initialize(meletes *projects);
    meletes *newp(meletes *projects);
    void list(meletes *projects);
    void deletep(meletes *projects);
    
    int main(int argc, char *argv[])
    {
    
        int choice,k;
        meletes projects[10];
        meletes *retMeletes;
    
        initialize(projects);
    
        printf("Main Menu\n ========\n");
        printf("Please choose a function from below:\n");
        printf("1.New Project\n2.Delete\n3.List\n4.Exit\n");
        scanf("%d", &choice);
    
        while ((choice != 1) && (choice != 2) && (choice != 3) && (choice != 4))
        {
    
               printf("You have chosen a wrong function, please use numbers 1-4:\n\n");
               printf("Main Menu\n ========\n");
               printf("Please choose a function from below:\n");
               printf("1.New Project\n2.Delete\n3.List\n4.Exit\n");
               scanf("%d", &choice);
        }
    
        while (choice != 4)
        {
            switch (choice)
            {
                case 1:
                    retMeletes = newp( projects);
                break;
    
                case 2:
                    deletep(projects);
                break;
    
                case 3:
                    list(projects);
                break;
            }
    
            printf("Main Menu\n ========\n");
            printf("Please choose a function from below:\n");
            printf("1.New Project\n2.Delete\n3.List\n4.Exit\n");
            scanf("%d", &choice);             
        }
    
        printf("Thank u.\n");
    
        system("pause");
        return 0;
    }
    
    void initialize(meletes *projects)
    {
        int l;
        for(l=0; l<10; l++)
        {
            projects[l].identifier = 00000;
            projects[l].price = 0.00;
            strcpy(projects[l].customer.name,"----------");
            strcpy(projects[l].customer.surname,"----------");
            strcpy(projects[l].date, "00/00/0000");
        }          
    }          
    
    meletes *newp(meletes *projects)
    {
        int i;
    
        for(i=0; i<10; i++)
        {
            if (projects[i].identifier == 0)
            {
                printf("Please enter the project's identifier: ");
                scanf("%d", &projects[i].identifier);
                printf("Name: ");
                scanf("%s", &projects[i].customer.name);
                printf("Surname: ");
                scanf("%s", &projects[i].customer.surname);
                printf("Give the date in dd/mm/yyyy! format: ");
                memset( &projects[i].date, 0x00, sizeof( projects[i].date ));
                scanf("%s", &projects[i].date );
                printf("Price: ");
                scanf("%f", &projects[i].price);
                break;
            }
        }                
        return( &projects[i] );
    }
    
    void deletep(meletes *projects)
    {
        int j,id;
    
        for (j=0; j<10; j++)
        {
            if (projects[j].identifier != 0)     //Emfanizei oles tis meletes pou den ine diegrammenes
            {
                printf("%d\n", projects[j].identifier);
            }
        }
    
        printf("\nPlease insert the identifier of the project u want to delete: ");
        scanf("%d", &id);
    
        projects[id-1].identifier = 0;
        projects[id-1].price = 0.00;
        strcpy(projects[id-1].customer.name,"----------");
        strcpy(projects[id-1].customer.surname,"----------");
        strcpy(projects[id-1].date, "0/0/0");
    
        return;
    }
    
    void list(meletes *projects)
    {
        int k;
    
        for(k=0; k<10; k++)
        {
            if (projects[k].identifier != 00000); 
            {
                printf("         Project %d:", k);
                printf("\nIdentifier:%d\n", projects[k].identifier);
                printf("Name:%s\n", projects[k].customer.name);
                printf("Surname:%s\n",projects[k].customer.surname);
                printf("Date:%s\n", projects[k].date);
                printf("Price:%f\n", projects[k].price);
            }
        }
        return;
    }
    

    【讨论】:

    • 非常感谢你。非常感谢您的帮助。所以首先我注意到的是typedef。它基本上将一个变量定义为等于一个变量类型,从而节省了我每次编写 struct 的努力,对吧?
    • 不客气。是的,这基本上是正确的。请参阅en.wikipedia.org/wiki/Typedef 如果您觉得我的示例有帮助,请随时接受我的回答。如果您还有其他问题,请提出。
    • 好吧!我不能按向上箭头按钮。我真正不明白的一件事是 main() 函数中的 2 个变量?我没有看到这两个在任何地方被调用?他们的目的是什么?
    • meletes *retMeletes;用于保存 newp() 函数的返回值。该代码不使用 newp() 的返回值,但它可以。如果您不需要知道添加的项目的详细信息,您也可以让 newp() 不返回任何内容,然后删除 retMeletes。我相信 main 中的其他变量存在于您的原始来源中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多