gpt4 book ai didi

c - 菜单无法正常工作

转载 作者:行者123 更新时间:2023-11-30 14:22:08 25 4
gpt4 key购买 nike

#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(仍然无法正常工作)。有人可以帮我解决这个错误吗,因为我尝试了我所知道的一切,但仍然是徒劳的

最佳答案

我认为你的问题是在 customers_menu() 中输出菜单,但不读取选择,而是调用 recordCount() ,它直接调用 addCustomer()

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

其他一些注意事项:

  1. gets() 不好,我建议您使用 scanf() (带有 %s)。
  2. 执行 printf() 然后清除屏幕有点毫无意义。
  3. 错误消息实际上应该发送到 stderr (fprintf(stderr,...)) 而不是 stdout (printf(...))
  4. 您的代码缺少尾随}。
  5. 添加了 cc,但未使用。

关于c - 菜单无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14053836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com