BorkedSystem32
Silver Coder
Hey there, it's been a long time since I've asked a question on this site!
I've started building my library management project, however, I have ran into a problem involving scanning string input. Data for new books is saved to a structure: book name and author(both strings), year published and page count(both integers).
I've been trying to use
Here's the snippet of code I wish to focus on:
I'd appreciate it if you guys didn't give me full snippets to use but rather just gave me hints as to where to go - I like a good challenge and I haven't been programming for a while, so I'd like a fun problem to solve whilst getting back in.
Already, I'm thinking it may have something to do with the way I have used the data structure, but I haven't had the chance yet to confirm that.
Thanks! 🙂
I've started building my library management project, however, I have ran into a problem involving scanning string input. Data for new books is saved to a structure: book name and author(both strings), year published and page count(both integers).
I've been trying to use
fgets
like I once done on a previous project, but I can't seem to get it to work properly: it accepts an input, but instead of proceeding onto the next step, it will instead ask for the same input(this is for the book name member) rather than moving onto the next input step. I can't figure out what may be causing this as I've used fgets
before and never had this problem - I'm stuck in a never ending loop and even if I go over the buffer, it still continues to accept new input(and it's even double or triple printing the same text!)Here's the snippet of code I wish to focus on:
C:
//...
struct Book {
char *Book_Name;
char *Book_Author;
int Page_Count;
int Year_Published;
};
struct Book Add_Book(char *Buffer) {
struct Book Added_Book = {"", "", 0, 0000};
printf("\nAdd A Book\nPlease insert the name of the book: ");
char *String = fgets(Buffer, 24, stdin);
Added_Book.Book_Name = String;
printf("%s", Added_Book.Book_Name);
// Continue to ask more questions about new book here.
return Added_Book;
}
int main(void) {
//...
char *Buffer = (char *) malloc(24);
//...
}
I'd appreciate it if you guys didn't give me full snippets to use but rather just gave me hints as to where to go - I like a good challenge and I haven't been programming for a while, so I'd like a fun problem to solve whilst getting back in.
Already, I'm thinking it may have something to do with the way I have used the data structure, but I haven't had the chance yet to confirm that.
Thanks! 🙂