Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C Struggling to get string and then accept other types of input

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 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! 🙂
 
I see absolutely no issues with your code, no infinite loop or whatever.
I completed the program like this
C:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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\n\n");
    printf("Name  : ");
    char* String = fgets(Buffer, 24, stdin);
    Added_Book.Book_Name = String;

    printf("Author: ");
    String = fgets(Buffer, 24, stdin);
    Added_Book.Book_Author = String;

    printf("Pages : ");
    String = fgets(Buffer, 24, stdin);
    Added_Book.Page_Count = atoi(String);

    printf("Year  : ");
    String = fgets(Buffer, 24, stdin);
    Added_Book.Year_Published = atoi(String);

    return Added_Book;
}

int main(void)
{
    char* Buffer = (char*)malloc(24);
    struct Book b = Add_Book(Buffer);
    printf("\n\nBOOK ADDED !\n\n");

    printf("Name   : %s\n", b.Book_Name);
    printf("Author : %s\n", b.Book_Author);
    printf("Pages  : %d\n", b.Page_Count);
    printf("Year   : %d\n", b.Year_Published);

    printf("END PROGRAM");
}

and it runs just fine for me, see screenshot. So, no clue why it's not working for you.

Small niggle: I don't like the malloc'ed Buffer and the repetition of the number 24. Why not just declare char Buffer[25]; inside Add_Book, and use sizeof(Buffer)-1 as the length with fgets ?

PS - Note that fgets gives you the string including the terminating LF. I did not bother to remove it.
 

Attachments

  • a.jpg
    a.jpg
    47.9 KB · Views: 5

New Threads

Buy us a coffee!

Back
Top Bottom