我找不到如何查看我们之前在用于输入信息的函数中输入的数据的解决方案。另外,我使用的是结构,而不是普通的声明和变量。
流程如下,我选择情况 1,它将调用 addbook 函数,我将在其中插入数据,然后我将选择情况 4,其中我刚刚插入的数据现在应该显示在屏幕上,但不幸的是它不会显示显示数据。所以我该怎么做?如果你们能帮助我,我将不胜感激
这是我的编码
struct book
{
char title[50];
char author[50];
int quantity;
int price;
};
struct book a;
void addbook();
//void searchbook();
//void deletebook();
void viewbook();
//void updatebook();
void returnfunc();
int main()
{
char choice;
do
{
printf("\n\n\t1.Add Book\n");
printf("\n\t2.Search Book\n");
printf("\n\t3.Delete Book\n");
printf("\n\t4.View Book\n");
printf("\n\t5.Update Book\n");
printf("\n\t6.Exit\n");
printf("\n\nPlease enter your choice :");
scanf("%d",&choice);
switch (choice)
{
case 1 :addbook();
break;
//case 2 :searchbook();
break;
//case 3 :deletebook();
break;
case 4 :viewbook();
break;
//case 5 :updatebook();
break;
case 6 :printf("THANK YOU !!");
break;
default :printf("Wrong Choice.Please enter Again");
break;
}
}
while(choice!=6);
return 0;
}
void addbook()
{
printf("\n============= PLEASE ADD NEW BOOK DETAILS ================");
int quantity;
FILE *fp;
printf("\n\nNumber of book to insert:");
scanf("%d", &quantity);
struct book a[quantity];
int i;
fp=fopen("Bibek.dat","ab+");
for(i=0;i<quantity;i++)
{
printf("Title:");
scanf("%s", &a[i].title);
printf("Author:");
scanf("%s", &a[i].author);
printf("Price:");
scanf("%d", &a[i].price);
printf("\n");
}
printf("\n\n---NEW BOOK DETAILS WAS SUCCESFULLY ADDED---\n\n");
}
//void searchbook()
//{
//printf("========SEARCH THE BOOK========");
//}
//void deletebook()
//{
//}
void viewbook(void)
{
FILE *fp;
int i=0,j;
system("cls");
printf("*********************************Book List*****************************");
printf(" TITLE AUTHOR QTY PRICE");
j=4;
fp=fopen("Bibek.dat","rb");
while(fread(&a,sizeof(a),1,fp)==1)
{
printf("%s",a.title);
printf("%s",a.author);
printf("%d",a.quantity);
printf("%d",a.price);
printf("\n\n");
j++;
i=i+a.quantity;
}
printf("Total Books =%d",i);
fclose(fp);
returnfunc();
}
void returnfunc(void)
{
{
printf(" Press ENTER to return to main menu");
}
a:
if(getch()==13) //allow only use of enter
main();
else
goto a;
}
//void updatebook()
//{
//}
最佳答案
您需要的更改相当简单和/或在注释中。请尝试一下。您可以修改我的尝试来修复您的代码。
#include <stdio.h>
#include <stdlib.h>
struct book {
char title[50];
char author[50];
int quantity;
int price;
};
struct book a;
void addbook();
//void searchbook();
//void deletebook();
void viewbook();
//void updatebook();
void returnfunc();
int main() {
int choice;
do {
printf("\n\n\t1.Add Book\n");
printf("\n\t2.Search Book\n");
printf("\n\t3.Delete Book\n");
printf("\n\t4.View Book\n");
printf("\n\t5.Update Book\n");
printf("\n\t6.Exit\n");
printf("\n\nPlease enter your choice :");
scanf("%d", &choice);
switch (choice) {
case 1 :
addbook();
break;
//case 2 :searchbook();
break;
//case 3 :deletebook();
break;
case 4 :
viewbook();
break;
//case 5 :updatebook();
break;
case 6 :
printf("THANK YOU !!");
break;
default :
printf("Wrong Choice.Please enter Again");
break;
}
}
while (choice != 6);
return 0;
}
void addbook() {
printf("\n============= PLEASE ADD NEW BOOK DETAILS ================");
int quantity;
FILE *fp;
printf("\n\nNumber of book to insert:");
scanf("%d", &quantity);
struct book a[quantity];
int i;
fp = fopen("Bibek.dat", "ab+");
for (i = 0; i < quantity; i++) {
printf("Title:");
scanf("%s", a[i].title);
printf("Author:");
scanf("%s", a[i].author);
printf("Price:");
scanf("%d", &a[i].price);
printf("\n");
}
fwrite(a, sizeof(a) , 1, fp);
fclose(fp);
printf("\n\n---NEW BOOK DETAILS WAS SUCCESFULLY ADDED---\n\n");
}
void viewbook(void) {
FILE *fp;
int i = 0, j;
//system("cls");
printf("*********************************Book List*****************************");
printf(" TITLE AUTHOR QTY PRICE\n");
j = 0;
fp = fopen("Bibek.dat", "rb");
while (fread(&a, sizeof(a), 1, fp) == 1) {
printf("%s ", a.title);
printf("%s ", a.author);
//printf("%d", a.quantity);
printf("%d ", a.price);
printf("\n\n");
j++;
i = i + a.quantity;
}
printf("Total Books =%d", j);
fclose(fp);
returnfunc();
}
void returnfunc(void) {
printf(" Press ENTER to return to main menu");
}
测试
/a.out
1.Add Book
2.Search Book
3.Delete Book
4.View Book
5.Update Book
6.Exit
Please enter your choice :1
============= PLEASE ADD NEW BOOK DETAILS ================
Number of book to insert:2
Title:foo
Author:carol
Price:2
Title:bar
Author:mallory
Price:3
---NEW BOOK DETAILS WAS SUCCESFULLY ADDED---
1.Add Book
2.Search Book
3.Delete Book
4.View Book
5.Update Book
6.Exit
Please enter your choice :4
*********************************Book List***************************** TITLE AUTHOR QTY PRICE
foo carol 2
bar mallory 3
Total Books =2 Press ENTER to return to main menu
1.Add Book
2.Search Book
3.Delete Book
4.View Book
5.Update Book
6.Exit
Please enter your choice :
关于c - 如何使用 struct 显示从 C 中另一个函数输入的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41278580/