0
#include <stdio.h>
#include <stdlib.h> //for the clear screen function
#include <string.h>

struct customer
{
    int custID;
    char custName[50];
    char custAddress[100];
};

typedef struct customer c;

void load_menu(void);
void customers_menu(void);
void createNew(void); //initialize your file
void add_Customer(c c1[30]); //add a new record to the file
FILE *fp;



int main(void)
{

load_menu();
return 0;
}

void load_menu(void)
{
int choice;

do
{
    printf("Customer Orders Main Menu. \n\n");
            printf("Please enter your choice: \n");
    printf("1. Customer's Menu \n");
    printf("2. Orders Menu\n");
    printf("3. Product Stock Menu\n");
    printf("4. Exit\n");
    printf("\n");
    if (scanf("%d",&choice)==1)
    {

        switch(choice)
        {
            case 1: system ("cls");
                    customers_menu();
                    printf("\n");
                    break;
            case 2: system ("cls");
                    orders_menu();
                    printf("\n");
                    break;
            case 3: system ("cls");
                    stock_menu();
                    printf("\n");
                    break;
            case 4: printf("Quitting program!\n");
                    break;
            default: printf("Invalid choice! Please try again\n");
                    printf("\n");
                break;
        }
    }

    else
    {
        fflush(stdin);
        printf("Characters are invalid, please enter a number: \n ");
        choice=0;
    }

}while((choice !=4));
}




void createNew(void)
{
    FILE *fp;
    fp=fopen("Customer.dat", "w");
    if (fp==NULL)
        printf("File creation failed! \n");
    else
    {
        printf("File created! \n");
        fclose(fp);
    }

}

void add_Customer (c c1[30])
{
    int i, n , cc=0;
    FILE *fp;
    fp=fopen("Customer.dat", "a");
    system("cls");

    if(fp==NULL)
   {
       printf("File Creation Failed!");
   }
   system("cls");

   printf("Enter the number of Customers: ");
   scanf("%d", &n);

   for(i=0;i<n;i++)
   {
       printf("Customer's ID (numbers only)  : ");
       scanf("%d", &c1[i].custID);

       printf("Customer's  Name              : ");
       gets(c1[i].custName);

        printf("Customer's Address            : ");
        gets(c1[i].custAddress);



        fwrite(&c1[i], sizeof(c), 1, fp);
    }cc++;

    fclose(fp);
}

void recordCount(c c1[30], int *count)
{
    add_Customer(c1);
    count=0;
    count++;
}

void customers_menu(void)
{
int choice;
    c c1[30];
    int i;


do
{
    printf("\n");
    printf("Customers Menu \n\n");
    printf("Please enter your choice: \n");
    printf("1. Add Customer \n");
    printf("2.\n");
    printf("3.\n");
    printf("4. Go back to Main Menu \n");
    recordCount (c1, &i);

    if (scanf("%d",&choice)==1)
    {

        switch(choice)
        {
            case 1: add_Customer(c1);
                    createNew();

                    printf("\n");
                    break;
            case 2:
                    printf("\n");
                    break;
            case 3:
                    printf("\n");
                    break;
            case 4: printf("Going back to Main Menu\n");
                    system ("cls");
                    break;
            default: printf("Invalid choice! Please try again\n");
                    printf("\n");
                break;
        }
    }

    else
    {
        fflush(stdin);
        printf("Characters are invalid, please enter a number: \n ");
        choice=0;
    }

}while((choice !=4));

我有一个问题,因为当我进入客户菜单时,它开始立即执行案例 1(仍然无法正常工作)。有人可以帮我解决这个错误吗,因为我尝试了我所知道的一切,但它仍然是徒劳的

4

2 回答 2

1

这个问题来自if (scanf("%d",&choice)==1)因为scanf不会返回选择。如果您输入有效的答案(如数字),那么它会返回 1 并使用 1 切换案例。我认为这就是问题所在。如果输入 char 而不是 integer,scanf将返回 0。

于 2012-12-27T11:27:46.450 回答
1

我认为您的问题是在customers_menu()您输出菜单时,但不要阅读选择,而是recordCount()直接调用addCustomer().

addCustomer()我们返回之后customers_menu(),然后调用scanf()早已消失的菜单。

其他一些注意事项:

  1. gets() is not good, I suggest you use scanf() (with %s) instead.
  2. Doing a printf() then clearing the screen is a bit pointless.
  3. Error messages should really go to stderr (fprintf(stderr,...)) rather than stdout (printf(...))
  4. You code is a missing trailing }.
  5. cc is added to, but not used.
于 2012-12-27T11:58:56.733 回答