0

this is the whole code of what im doing, im trying to create a song library that will put what the user enter into file. now the compiler says that passing argument 2 of strcpy makes pointer from integer without a cast and i dont know why. also can u check my linked list for the struct. im so noob at linked list :(

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

struct node {
    //definition of struct node to create struct node song
    int SongID;
    char Title[100];
    char Artist[100];
    char Composer[100];
    char Album[100];
    char Genre[100];
    int Rating;
    char Remarks[1000];
    struct node*next;
};

add_song(int SongID, char Title, char Artist, char Composer, char Album, char Genre, int Rating, char Remarks) {
    //this is the add song function as stated in the mp2 specs
    FILE*fp;
    fp=fopen("song.txt","r+");

    int i=1, j, choice;
    struct node* temp=malloc(sizeof(struct node));

    temp->SongID=SongID;
    fprintf(fp,"%d",SongID);

    strcpy(temp->Title, Title);
    fprintf(fp,"%s",Title);

    strcpy(temp->Artist, Artist);
    fprintf(fp,"%s",Artist);

    strcpy(temp->Composer, Composer);
    fprintf(fp,"%s",Composer);

    strcpy(temp->Album, Album);
    fprintf(fp,"%s",Album);

    strcpy(temp->Genre, Genre);
    fprintf(fp,"%s",Genre);

    temp->Rating=Rating;
    fprintf(fp,"%d",Rating);

    strcpy(temp->Remarks, Remarks);
    fprintf(fp,"%s",Remarks);

    fclose(fp);
}

int main ()
{
    struct node song;
    int choice;
    int k, i;
    int SongID;
    char Title[100];
    char Artist[100];
    char Composer[100];
    char Album[100];
    char Genre[100];
    int Rating;
    char Remarks[1000];

   /* do
     {
        printf("Enter 1 to add song, 2 to update song, or 3 to list songs: ");
        scanf("%d\n", &choice1);

        if (choice1==1)
        {*/
        srand(time(NULL));
        song.SongID=rand();

        printf("Enter Title: ");
        fgets(Title,100,stdin);

        printf("Enter Artist: ");
        fgets(Artist,100,stdin);

        printf("Enter Composer: ");
        fgets(Composer,100,stdin);

        printf("Enter Album: ");
        fgets(Album,100,stdin);

        //for easier code, numbers are being chosen as input
        printf("Press 1 for Art Music, 2 for Popular Music, or 3 for Traditional Music): ");
        scanf("%d", &choice);

        if (choice==1)
        {
            strcpy(song.Genre,"Art Music");
        }
        else if (choice==2)
        {
            strcpy(song.Genre,"Popular Music");
        }
        else if (choice==3)
        {
            strcpy(song.Genre,"Traditional Music");
        }
        else
        {
            printf("You entered a blank genre.\n");
        }

        printf("Enter your rating, choose from 1-5: ");
        scanf("%d", &Rating);

        printf("Enter Remarks: ");
        fgets(Remarks,1000,stdin);

        add_song(SongID, Title, Artist, Composer, Album, Genre, Rating, Remarks);
            /*k=0;
            break;
        }
        else if (choice1==2)
        {
            //update_song(song);
            k=0;
            break;
        }
        else if (choice1==3)
        {
           // list_songs(song);
            k=0;
            break;
        }
        else
        {
            k=1;
            printf("That is not a valid input.\n");
        }
    }while (k==1);*/

    return 0;
}
3
  • add_song(int SongID, char Title, char Artist, char Composer, char Album, char Genre, int Rating, char Remarks) should be void add_song(int SongID, char *Title, char *Artist, char *Composer, char *Album, char *Genre, int Rating, char *Remarks). They are strings, not single chars. Commented Dec 1, 2015 at 10:37
  • change to void add_song(int SongID, char* Title, char* Artist, char* Composer, char* Album, char* Genre, int Rating, char* Remarks) Commented Dec 1, 2015 at 10:38
  • what compiler were you using that you didn't get drowned with warnings for that? no return type, passing char to a function that expects char *... Commented Dec 1, 2015 at 11:16

3 Answers 3

1

Your function definition don't match with the arguments you pass. It should be

    void add_song(int SongID, char *Title, char *Artist, char *Composer, \
char *Album, char *Genre, int Rating, char *Remarks) {

       ...
       ...
    }

Another issue is that songID is uninitialized in main(). Reading from an uninitialized variable is undefind behaviour.

Another problem you might face is that fgets() reads the newline \n into buffer if there's space available which might be problematic. Something to be aware of and you need trim it if nececcary.

Sign up to request clarification or add additional context in comments.

4 Comments

i dont really get the newline buffer thing would u mind explaining it further because im new at c sorry, but i did what you told me and there are only 2 errors left
When you read a string like "abc" using fgets(), it's actually "abc\n". This may or may not be an issue. But it actually reads in the newline if there's buffer space.
based on what i know, scanf doesn't read space and fgets would be better so i used it, can u suggest a better function for this one?
@shiiranyan fgets() is better than scanf(). You are fine to use fgets(). I am just saying if you don't need the newline, you can trim it. For example, after each call to fgets(), you can do: char *p = strchr(my_array, '\n'); if (p) *p = 0; to remove the newline.
0

This is working copy of your code except one thing "Remark". Before executing scanf for Remark process exits . Change your arguments as I did here and also change fopen mode as +w so if file is not exist it can be created automatically. strcpy prototype is `char *strcpy(char *dest, const char *src). So I have changed accordingly

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

struct node {
    //definition of struct node to create struct node song
    int SongID;
    char Title[100];
    char Artist[100];
    char Composer[100];
    char Album[100];
    char Genre[100];
    int Rating;
    char Remarks[1000];
    struct node *next;
};

void add_song(int SongID, char *Title, char *Artist, char *Composer, char *Album, char *Genre, int Rating, char *Remarks) {

    //this is the add song function as stated in the mp2 specs
    FILE *fp;
    fp=fopen("song.txt","a+");

    int i=1, j, choice;
    struct node* temp=malloc(sizeof(struct node));

    temp->SongID=SongID;
    fprintf(fp,"%d",SongID);

    strcpy(temp->Title, Title);
    fprintf(fp,"%s",Title);

    strcpy(temp->Artist, Artist);
    fprintf(fp,"%s",Artist);

    strcpy(temp->Composer, Composer);
    fprintf(fp,"%s",Composer);

    strcpy(temp->Album, Album);
    fprintf(fp,"%s",Album);

    strcpy(temp->Genre, Genre);
    fprintf(fp,"%s",Genre);

    temp->Rating=Rating;
    fprintf(fp,"%d",Rating);

    strcpy(temp->Remarks, Remarks);
    fprintf(fp,"%s",Remarks);

    fclose(fp);
}

int main ()
{
    struct node song;
    int choice;
    int k, i;
    int SongID;
    char Title[100];
    char Artist[100];
    char Composer[100];
    char Album[100];
    char Genre[100];
    int Rating;
    char Remarks[1000];

   /* do
     {
        printf("Enter 1 to add song, 2 to update song, or 3 to list songs: ");
        scanf("%d\n", &choice1);

        if (choice1==1)
        {*/
        srand(time(NULL));
        song.SongID=rand();

        printf("Enter Title: ");
        fgets(Title,100,stdin);

        printf("Enter Artist: ");
        fgets(Artist,100,stdin);

        printf("Enter Composer: ");
        fgets(Composer,100,stdin);

        printf("Enter Album: ");
        fgets(Album,100,stdin);

        //for easier code, numbers are being chosen as input
        printf("Press 1 for Art Music, 2 for Popular Music, or 3 for Traditional Music): ");
        scanf("%d", &choice);

        if (choice==1)
        {

            strcpy(song.Genre,"Art Music");
        }
        else if (choice==2)
        {

             strcpy(song.Genre,"Popular Music");
        }
        else if (choice==3)
        {

            strcpy(song.Genre,"Traditional Music");
        }
        else
        {
            printf("You entered a blank genre.\n");
        }

        printf("Enter your rating, choose from 1-5: ");
        scanf("%d",&Rating);

        printf("Enter Remarks: \n");
        fgets(Remarks,1000,stdin);

        add_song(SongID, Title, Artist, Composer, Album, Genre, Rating, Remarks);
            /*k=0;
            break;
        }
        else if (choice1==2)
        {
            //update_song(song);
            k=0;
            break;
        }
        else if (choice1==3)
        {
           // list_songs(song);
            k=0;
            break;
        }
        else
        {
            k=1;
            printf("That is not a valid input.\n");
        }
    }while (k==1);*/

    return 0;
}

6 Comments

thank you i did what u said, but the problem here is that i cant use w+ because it overwrites the file, and i need to put all the things that the users entered without the other things entered before being overwritten and the problem here i think is the linked list, although i haven't implemented linked list yet, i dont know how do i do it here
I guess you want want to append changes at last .So you can use a+ mode . I update my answer as per it
i dont know if this might be related to your code but when i open the txt file, it isn't responding
I am ready to help :-).Can you tell me please not responding means what do you mean ? I have already compiled and its working for me .
i solved it already, but the problem now is when i removed the comment for the statements in main, the thing that happens with remarks (where it skips the remarks part) happened with the title too i dont know why
|
0

Didn't go through the whole code, but strcpy problem is obvious. According to definition of function strcpy (http://www.tutorialspoint.com/c_standard_library/c_function_strcpy.htm), it expects two char pointers (aka strings in C)

char *strcpy(char *dest, const char *src)

You're passing only char arguments to a function add_song, which is just not the pointer. Change the signature of the function add_song and then you should be fine with strcpy

2 Comments

so in here strcpy(temp->Title, Title) for example, i need to make Title a pointer?
Well, yes. But as I and others already suggested, your main problem is that you are actually passing only chars into add_song (actually a really weird chars, that is first 4 bytes of value of those pointers) - you need to actually pass char pointers into add_song (by having correct definition of char * in a add_song header. Once you do that, then you will actually already have pointers inside add_song function and don't need to change anything in strcpy calls

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.