-
help me
this asks for info about a book
but it says that it cant find the header file
Code:
struct bookInfo {
char title [40];
char author [25];
float price;
int pages;
};
#include <bookinfo.h>
#include <stdio.h>
main()
{
int ctr;
struct bookInfo books[3];
for (ctr = 0; ctr < 3; ctr++)
{
printf("What is the name of the book #%d?\n\n", (ctr+1));
gets (books[ctr] .title);
puts("Who is the author?");
gets(books[ctr].author)
puts("How much did the book cost?");
scnaf(" $%f", &books[ctr].price);
puts("How many pages in the book?");
scanf(" %d", &books[ctr].pages);
getchar();
}
printf("\n\nHere is the collection of books: \n\n");
for (ctr = 0; ctr < 3; ctr++)
{
printf("#%d: %s by %s",
(ctr+1), books[ctr].title, books[ctr].author);
printf("\n\nIt is %d pages and costs $.2f",
books[ctr.pages, books[ctr].price);
printf("\n\n");
}
return (0);
}
-
Which header file?
You already declared your struct, so bookinfo.h seems unnecessary.
If your book is that old that it doesn't declare main properly, and thinks gets() is a good idea, you need to throw it away.
Also, check your spelling of scanf.
Also, "help me" is about the dumbest title you can choose.
How To Ask Questions The Smart Way
-
plus after what Salem said about that, if you where to put it into a file called bookinfo.h it is referenced like this,
Code:
#include "bookinfo.h"
quotes are for local files, < > brackets are for system ones.
and formating goes like this, if you actually did it like that in your main.
Code:
#include <stdio.h>
#include "bookinfo.h"
struct bookInfo {
char title [40];
char author [25];
float price;
int pages;
};
int main()
{
// stuff
return 0;
}
-
books[i.pages, books[i].price]
You meant to access books[i] right? But look where the bracket traveled. Keep an eye out for these types of small syntax errors. They plague everyone.
Edit: a user by the name escapeSequence had posted code with this error but deleted his post in apparent shame. :( Sorry that he removed the context.
-
There were several syntax errors.
Code:
#include "bookinfo.h"
#include <stdio.h>
int main(void) {
struct bookInfo books[3];
for (int i = 0; i < 3; i++)
{
printf("What is the name of the book #%d?\n\n", (i+1));
gets (books[i] .title);
puts("Who is the author?");
gets(books[i].author);
puts("How much did the book cost?");
scanf(" $%f", &books[i].price);
puts("How many pages in the book?");
scanf(" %d", &books[i].pages);
getchar();
}
printf("\n\nHere is the collection of books: \n\n");
for (int i = 0; i < 3; i++)
{
printf("#%d: %s by %s",
(i+1), books[i].title, books[i].author);
printf("\n\nIt is %d pages and costs $.2f",
books[i].pages, books[i].price);
printf("\n\n");
}
return (0);
}
book.h
Code:
struct bookInfo {
char title [40];
char author [25];
float price;
int pages;
};
-
Code:
gets(books[ctr] .title);
man gets | grep ^BUGS --after-context=8 -
BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.
For more information, see CWE-242 (aka "Use of Inherently Dangerous
Function") at CWE -
CWE-242: Use of Inherently Dangerous Function (2.11)