0

//help me i dont know what wrong with list function

#include <stdio.h>
#define MAX 10

struct book{
	char title[81];
	char author[71];
	char category[31];

	};

	int main()
	{
		struct book library[MAX];
		int menu(void);
		struct book add(void);
		int list(struct book,int);
		int choice, count = 0;
		do
		{
			choice = menu();
			switch( choice )
			{
				case 0:
					puts("End of my act! :-)");
					break;
				case 1:
					if (count < MAX )
					{
						library[count] = add();
						count++;
					}
					else
						puts("Library is reached maximum value");
						break;
				case 2:
					list(library[count],count);
					break;
				default:
					puts("Wrong selection. Try again");
		  }
		  }while(choice != 0 );
		  return 0;
		  }//end of main

//this function show the main menu read the user selection and return the user selection to the caller

int menu(void)
{
	int choice;
	puts("\n<<Super duper menu>>");
	puts("\t0:Exit");
	puts("\t1:Add a book");
	puts("\t2:List all books");
	printf("Enter your selection");
	scanf("%d",&choice);
	return choice;
}

struct book add(void)
{
	struct book temp;
	fflush(stdin);
	puts("\n <<ADD MORE>>");
	printf("Title?");
	gets(temp.title);
	printf("Author?");
	gets(temp.author);
	printf("Category");
	return temp;
}

void list(struct book *sp,int size)
{
	int i;
	puts("\n");
	puts("<<BOOKS LIST>>");
	for(i = 0; i < size;++i,++sp)
		printf("%d by %s - - - %s\n", i + 1, sp ->title, sp ->author, sp ->category);
	return;
	}

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

6
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by konata_89
0

The first question to ask is where do function prototypes belong? Not in main(). After that you should compare the prototype for the function list with the definition of it down below. They don't match. After that it's some minor adjustments once you decide whether you're passing the pointer or not.


And please learn to use code tags -- http://www.daniweb.com/forums/announcement118-3.html

Edited by jonsca: n/a

0

>The first question to ask is where do function prototypes belong? Not in main().
Prototypes are not required to be at file scope. It's merely conventional because they usually reside in headers (which are usually placed at file scope) and used in multiple translation units. In the average case, prototypes at block scope would be duplicated in every block they're used, which would be tedious for all but the simplest of programs.

0

//help me i dont know what wrong with list function

There's absolutely nothing wrong with it. What's wrong is how it is called. Look at line 36, and ask yourself what is a type of library[count] .

0

Prototypes are not required to be at file scope.

Noted. I'd never seen it done that way, false assumption on my part. Thank you.

There's absolutely nothing wrong with it.

The prototype doesn't match the definition, unless I'm missing something again. I do agree about the call being wrong.

0

Adding to what's been said above, GCC spotted a thing in the list() function, it said;

warning: too many arguments for format

That means that the printf(...) call there needs attention. There is a mismatch between the format string and the number of arguments you actually pass in.

Then you might consider dropping usage of fflush(stdin) altogether (as it might produce quite unexpected results), and perhaps replace gets() with e.g. fgets() to be on the safer side.

See ...
Why gets() is bad.
Why fflush(stdin) is bad.

Votes + Comments
Many fine points that deserved better than an anonymous -1 for your effort
0

case 2:
list(library[count],count);
break;


I think the call to the function should be


case 2:
list(library,count); or list(&library,count);
break;

Edited by anup375: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.