首页 > 解决方案 > C中字符串旁边的垃圾

问题描述

当我运行我的代码并且我已经插入了两部电影时,当我按下选项 3 搜索其中一部电影时,我的代码找到了这部电影,但随后它在电影标题旁边显示了一些垃圾,并停在了导演姓名处。如果有人尽快给我答案,我将不胜感激,因为这是明天晚上的任务。这里是

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

typedef struct date {
    int day, month, year;
} date;

typedef struct director_info {
    char* director_surname, *director_name;
} director_info;

typedef struct movie {
    int id;
    char* title;
    director_info* director;
    date* release_date;
} movie;


int main() { 

    printf("\t\t1.Insert a new movie");
    printf("\n\t\t2.Delete a movie based on movie's id");
    printf("\n\t\t3.Search a movie based on movie's title");
    printf("\n\t\t4.Search a movie based on movie's director");
    printf("\n\t\t5.EXIT\n");
    
    int selection;
    char str[50];
    int i = 0;
    char surname[50];
    char name[50];
    int uu;
    int day;
    int month;
    int year;
    
    movie *movies;
    movies = malloc(sizeof(movies));
    FILE *f =fopen("movie.TXT", "a+");
    
    do { 
        printf("\nPlease enter your selection:");
        scanf("%d",&selection);
        if(selection==1){
            char title[50];
            movies = malloc(sizeof(movies) * i);
            movies[i].director = malloc(sizeof(movies[i].director));
            movies[i].release_date = malloc(sizeof(movies[i].release_date));
            printf("\nInsert the movies data");
            printf("\nEnter the title of the movie:");
            movies[i].title = malloc(50);   
            fflush(stdin);
            gets(movies[i].title);
            //strcpy(movies[i].title, title);
            
            
            printf("\nEnter the name of the director:");
            movies[i].director->director_name = malloc(50);
            gets(movies[i].director->director_name);    
            //strcpy(movies[i].director->director_name, name);
            
            printf("\nEnter the surname of the director:");
            movies[i].director->director_surname = malloc(50);
            gets(movies[i].director->director_surname); 
            //strcpy(movies[i].director->director_surname, surname);
        
            printf("\nEnter the date  of the movie: (in the current form /_/):");
            scanf("%d/%d/%d",&day,&month,&year);
        
            movies[i].release_date->day = day;
            movies[i].release_date->month = month;
            movies[i].release_date->year = year;
            movies[i].id=i+100;
            i=i+1;
        }
        if(selection==2){
            int id;
            FILE *fr = fopen("movie.TXT", "r");
            printf("\nEnter movie's id:");
            scanf("%d",&id);
            printf("%d\n",id);
            
            
            fclose(fr);
        }
        
        if(selection==3){
            char title[50];
            printf("%d",i);
            int j;
            int u = 0;
            printf("\nEnter movie name:");
            scanf("%s", title);
            printf("%s",title);
            for(j=0;j<i;j++){
                if(strcmp(movies[j].title, title) == 0){
                    u=1;
                    uu=j;
                } 
                printf("%s\n",movies[j].title);
            }
            if(u==0) printf("\nNot found!\n");
            else {
                printf("\nID: %d\nTitle: %s\nName: %s\nSurname: %s\nDay: %d\nMonth: %d\nYear: %d",movies[uu].id, movies[uu].title, movies[uu].director->director_name, movies[uu].director->director_surname, movies[uu].release_date->day, movies[uu].release_date->month, movies[uu].release_date->year);
            }
            main();
        }
    } 
    while(selection!=5&&selection>=1&&selection<5);
    if(selection==5){
        printf("\nExiting...");
        exit(0);
    }
    else{
        printf("\nInvalid input\n\n");
        main();
    }
    return(0);
}

标签: c

解决方案


推荐阅读