English 中文(简体)
基础 不工作的男性法典
原标题:Basic Main Menu code not working

So what I am trying to do is write a simple program with a main menu where the user will be able to fill in the details of a project,list all projects, delete a chosen project or exit the program. Projects will be saved in an array. The functions will use structures.

我已撰写了一部功能性法典,其中汇编了罚款(一般交易单知道),但迄今为止,只有第四种选择“Exit”工程。 其他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
 ========
");
printf("Please choose a function from below:
");
printf("1.New Project
2.Delete
3.List
4.Exit
");
scanf("%d", &choice);

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

           printf("You have chosen a wrong function, please use numbers 1-4:

");
           printf("Main Menu
 ========
");
           printf("Please choose a function from below:
");
           printf("1.New Project
2.Delete
3.List
4.Exit
");
           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
 ========
");
            printf("Please choose a function from below:
");
            printf("1.New Project
2.Delete
3.List
4.Exit
");
            scanf("%d", &choice);             

          }        

          printf("Thank u.
");

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
",        &projects[i].identifier);
                    scanf("Name:%s
", &projects[i].customer.name);
                    scanf("Surname:%s
", &projects[i].customer.surname);
                    scanf("Give the date in dd/mm/yyyy! format :%c
", &projects[i].date);
                    scanf("Price:&f
", &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
", projects[j].identifier);
               }
        }

        scanf("
Please 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("
Identifier:%d
", projects[k].identifier);
                    printf("Name:%s
", projects[k].customer.name);
                    printf("Surname:%s
",projects[k].customer.surname);
                    printf("Date:%s
", projects[k].date);
                    printf("Price:%d
", projects[k].price);
                    }
        }

 }        `

任何想法都将得到真正的赞赏。

最佳回答

这里是一个工作例子。 请看这一问题,并让我们知道你所学到的东西。

#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
 ========
");
    printf("Please choose a function from below:
");
    printf("1.New Project
2.Delete
3.List
4.Exit
");
    scanf("%d", &choice);

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

           printf("You have chosen a wrong function, please use numbers 1-4:

");
           printf("Main Menu
 ========
");
           printf("Please choose a function from below:
");
           printf("1.New Project
2.Delete
3.List
4.Exit
");
           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
 ========
");
        printf("Please choose a function from below:
");
        printf("1.New Project
2.Delete
3.List
4.Exit
");
        scanf("%d", &choice);             
    }

    printf("Thank u.
");

    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
", projects[j].identifier);
        }
    }

    printf("
Please 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("
Identifier:%d
", projects[k].identifier);
            printf("Name:%s
", projects[k].customer.name);
            printf("Surname:%s
",projects[k].customer.surname);
            printf("Date:%s
", projects[k].date);
            printf("Price:%f
", projects[k].price);
        }
    }
    return;
}
问题回答

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]);

页: 1 职能如下:

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

后三条似乎退回了<代码>meletes。 您还必须宣布新的类型<代码>熔炉/代码>,并酌情加以使用:

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

第二看<代码>newp,deleteplistdo not 返还任何物品,如果与上述建议转让发生关系,将产生问题。 您要么需要退还项目(而且转让将罚款),要么将其返回类型改为<代码>,撤销。 (没有转让)。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签