Jump to content

Help a C newbie

Go to solution Solved by reniat,
20 minutes ago, Manos said:

just tried that and still nothing i think the problem is not in the malloc as when i run the program and type "add" it doesnt print anything the printfs' i think should have worked if the function was called properly 

perhaps it could be that you're not calling the function. you are declaring a function of the same name instead with no body :P

 

Book *AddBook(Book *previous);

 

instead of

Book* pSomeBook = AddBook(pSomePreviousBook);

Hello have created this function in C  Book* AddBook(Book *previous) {......} and although i dont have errors in the code i cannot call it in the main function.

Is it even possible to call such a function in main and if even so how?

 Printf("Thanks Very Much\n");

Link to comment
https://linustechtips.com/topic/781820-help-a-c-newbie/
Share on other sites

Link to post
Share on other sites

can you show us the full code? 

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
https://linustechtips.com/topic/781820-help-a-c-newbie/#findComment-9856934
Share on other sites

Link to post
Share on other sites

Just now, reniat said:

can you show us the full code? 


#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <stdbool.h>


enum genre { fiction = 1, scientific = 2, politics = 3 };

typedef struct Book {// dimiourgo to struct gioa to ka8e book
    char author[20];
    char title[15];
    enum genres genre;
    int id;
    struct Book *next;
}Book;


/*void PrintBook() {
//printf("");
} */

void PrintList() {  //menu,
    printf("List Print \n");
}
      
        Book* AddBook(Book *previous) {
        printf("Adding a Book \n");
        printf("for the genre of the book type 1 = fiction 2 = scientific 3=politcs \n");
        printf("Enter the name of author, title the genre of the book and the id\n");
        char input[60];
        fgets(input, 59, stdin);// to input einai -1 apo to max dioti prepei na perilif8ei kai to end of the line
        int ok = 0;
         Book *newBook = malloc(sizeof(Book));

        int res = sscanf(input, "%s %s %d %d", newBook->author, newBook->title, newBook->genre, &newBook->id);
        if (res == 4) { //to apotelesma prepei na einFai 4, 1 gia kathe plhroforia toy vivliou
            ok = 1; //tote h methodos epistrefei 1 gia na elenxoume oti exoyn eisaxthei oles oi plirofories
            printf("Added the book with name of author :%s, the title: %s , the genre %d and the id %d\n");
        }
        else {
            printf("Sorry Wrong input\n\n");
            printf("type a command from the menu\n");
        }

        if (previous != NULL) {
            previous->next = newBook;
        }

        return newBook;
    }


void UpdateBook() {
    printf("Update Book\n");
}
void LoadList() {
    printf("Load List\n");
}
int Findbook(Book *book) {
    printf("Find book\n");

    int input;
    scanf("%d", &input);// to input einai -1 apo to max dioti prepei na perilif8ei kai to end of the line
    int ok = 0;
    if (input >= 0 || input <= 999) {
        ok = 1;
        printf("fuck\n");
        //struct Book *head = NULL;

    }
    else {
        printf("Sorry Wrong input\n\n");
        printf("type a command from the menu\n");;

    }
    return ok;
}
void SaveList() {
    printf("Save List\n");
}
void DeleteBook() {
    printf("Delete Book\n");
}
void ReviewBook() {
    printf("review your bookie bitch");
}

void menu() {

}
int list()
{
    Book *start = NULL;
    Book *newest = NULL;
    if (start == NULL) {
        start = AddBook(NULL);
        newest = start;
    }
    else {
        newest = AddBook(newest);
    }
}
int main()
{
    printf("For listing all the books type <list>\n");
    printf("For Printing a specific book type <print>\n");
    printf("For adding a book type <add>\n");
    printf("For updating an existing book type <update>\n");
    printf("For saving a book type <find>\n");
    printf("For loading a book type <load>\n");
    printf("Saving a book type <save>\n");
    printf("For Deleting a book type <delete>\n");
    printf("For writing a review type  <review>\n");
    printf("For quiting the program type <quit>\n");

    char command[16];
    char input[16];
    while (fgets(input, 15, stdin))
    {
        sscanf(input, "%s", command);
        if (strncmp(command, "quit", 4) == 0) //sygrisei char gia elegxo
        {
            printf("\n\nBreaking...\n");
            break;
        }
        else if (strncmp(command, "list", 4) == 0)
        {
            PrintList();

        }
        else if (strncmp(command, "review", 6) == 0)
        {
            ReviewBook();

        }
        else if (strncmp(command, "add", 3) == 0)
        {
            Book *AddBook(Book *previous);

        }
        /*else if (strncmp(command, "print", 5) == 0)
        {
        PrintBook();

        }*/
        else if (strncmp(command, "load", 4) == 0)
        {
            LoadList();

        }
        else if (strncmp(command, "save", 4) == 0)
        {
            SaveList();

        }
        /*else if (strncmp(command, "find", 4) == 0)
        {
        numOfbooks += Findbook(&allBooks[numOfbooks]);
        } */
        else if (strncmp(command, "delete", 6) == 0)
        {
            DeleteBook();

        }
    }
    list();
    menu();

    return 0;
}
 

Link to comment
https://linustechtips.com/topic/781820-help-a-c-newbie/#findComment-9856937
Share on other sites

Link to post
Share on other sites

17 minutes ago, Manos said:

<snip>

 

You just need to cast the void* that malloc returns to a Book*. Also, just looking at your code I see a int list() function with no return statement :P 

 

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
https://linustechtips.com/topic/781820-help-a-c-newbie/#findComment-9856975
Share on other sites

Link to post
Share on other sites

11 minutes ago, reniat said:

You just need to cast the void* that malloc returns to a Book*. Also, just looking at your code I see an int list() function with no return statement :P 

 

umm yup you are right bout int list()i was experimenting with something before

idont see how i can do that. can you provide an example because this is my first assignment in C

 

Link to comment
https://linustechtips.com/topic/781820-help-a-c-newbie/#findComment-9857036
Share on other sites

Link to post
Share on other sites

1 minute ago, Manos said:

umm yup you are right bout int list()i was experimenting with something before

idont see how i can do that. can you provide an example because this is my first assignment in C

 

Book* newbook = (Book*)malloc(sizeof(Book));

 

malloc returns a void* so that you can cast it to literally any kind of pointer. malloc is just allocating memory, it doesn't care about the type.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
https://linustechtips.com/topic/781820-help-a-c-newbie/#findComment-9857052
Share on other sites

Link to post
Share on other sites

20 minutes ago, Manos said:

just tried that and still nothing i think the problem is not in the malloc as when i run the program and type "add" it doesnt print anything the printfs' i think should have worked if the function was called properly 

perhaps it could be that you're not calling the function. you are declaring a function of the same name instead with no body :P

 

Book *AddBook(Book *previous);

 

instead of

Book* pSomeBook = AddBook(pSomePreviousBook);

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
https://linustechtips.com/topic/781820-help-a-c-newbie/#findComment-9857158
Share on other sites

Link to post
Share on other sites

13 minutes ago, reniat said:

perhaps it could be that you're not calling the function. you are declaring a function of the same name instead with no body :P

 

Book *AddBook(Book *previous);

 

instead of

Book* pSomeBook = AddBook(pSomePreviousBook);

oohh i see thank you very much!!! Can i ask something more? is it possible to call the Book *AddBook(Book *previous); in a void menu()  and call the menu(); in the main?

Link to comment
https://linustechtips.com/topic/781820-help-a-c-newbie/#findComment-9857221
Share on other sites

Link to post
Share on other sites

Just now, Manos said:

oohh i see thank you very much!!! Can i ask something more? is it possible to call the Book *AddBook(Book *previous); in a void menu()  and call the menu(); in the main?

do you mean call the function? Because "Book *AddBook(Book *previous);" is still a function declaration, which you don't need since anywhere in your code since you're not using a header file to separate the declaration from the implementation.

 

If you mean a function call (where you are setting some Book pointer to be equal to the return value of an AddBook call), then yeah you could do something like this:

void boo(Book* pBook)
{
    pBook = AddBook();
}

void foo(Book* pBook)
{
    boo(pBook);
}

main()
{
    Book book = //make a new book
    foo(&book);
}

Keep in mind you'd also have to pass your previous Book* down the call stack so that you can use it as a parameter in Addbook. Is this what you mean? Maybe i misunderstood the question.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
https://linustechtips.com/topic/781820-help-a-c-newbie/#findComment-9857283
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×