0

I made this simple function for a program to record some sort of reminder for a user. I'm asking the title of the reminder, little details about it, and the month to when the reminder is due. Supposedly I should be including the day and year but I only included here the month just to test out if my int month is working. For some reason, scanf("%d",&sc.month) seems to not work because when I call it in another function, it prints a different number instead. This is my first big project in C and I'm a little excited about it. I'm currently using dosbox since the school requires us to use dosbox but I'm writing my code in sublime. I tried isolating the int cause I thought maybe the " % ^\n]%*c" syntax is messing up everything, but the problem still happens.

struct schedule{
    char title[50],details[100];
    int day,month,year;
};

void createSchedule(){

    struct schedule sc;
    FILE *fp;
    fp = fopen("schedule","ab");
    if(fp==NULL){
        printf("File not found");
        getch();
        main();
    }
    printf("Enter title:");scanf(" %[^\n]%*c",&sc.title);
    printf("\nEnter details: ");scanf(" %[^\n]%*c",&sc.details);
    printf("\nEnter number: ");scanf("%d",&sc.day);
    printf("\nSchedule created!");
    fwrite(&sc,sizeof(struct schedule),1,fp);
    
    getch();
    fclose(fp);
    active();
}


void readSchedule(){

    struct schedule sc;
    FILE *fp;
    fp = fopen("schedule","rb");

    printf("Schedules:\n\n");

    if(fp==NULL){
        printf("File not found");
        getch();
        main();
    }
    while(fread(&sc,sizeof(struct schedule),1,fp)){
        
        printf("Title: %s\n",&sc.title);
        printf("Details: %s\n",&sc.details);
        printf("Day: %d",&sc.day);
    }
    getch();
    fclose(fp);

    active();
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Always check the return value from `scanf` so you know if it worked or not. If you need to read a complete line consider `fgets` instead and learn how to remove the newline from the end. What is the input you're using to test this? – Retired Ninja Feb 10 '22 at 04:07
  • @RetiredNinja by return of scanf, do you mean its output when I call it in a printf function? My input for month are numbers from 1-12. I tried inputting 2, but i get -776 instead. Ill look into fgets and see how I can remove the new line. Thanks – Strict_tomato Feb 10 '22 at 04:11
  • [scanf](https://en.cppreference.com/w/c/io/fscanf) returns a value that tells you how many items it scanned, or 0 if none were scanned. It can also return `EOF`. You should always capture that return value so you can tell if it worked properly or not. `int result = scanf(...); if (result != numberOfItemsInTheFormatString) { /* handle the error */ }` When we ask for input we're asking for all of it. You're reading 3 lines, so what 3 lines are you typing when you're trying to test this? – Retired Ninja Feb 10 '22 at 04:18
  • 2
    Side note: Don't call `main()` when `fp==NULL`, instead `return`. – JASLP doesn't support the IES Feb 10 '22 at 04:29
  • 1
    Also, remove the `&` before `sc.title`, `sc.details` and `sc.day` in the `printf()`s. – JASLP doesn't support the IES Feb 10 '22 at 04:36
  • 1
    `scanf` is good for reading things like single integers (`%d`). But when the majority of your `scanf` calls are using a jawbreaker format string like `" %[^\n]%*c"`, you might as well abandon `scanf` entirely, and graduate to [more powerful, more useful techniques](https://stackoverflow.com/questions/58403537). – Steve Summit Feb 10 '22 at 04:44
  • 1
    Note that the `&` in `scanf(" %[^\n]%*c",&sc.title);` is not wanted — you're passing an `int (*)[50]` but `scanf()` expects an `int *`, which is a quite different type. Drop the `&` there. Similarly for `sc.details`. And you don't want the `&` in the calls to `printf()` either. – Jonathan Leffler Feb 10 '22 at 04:55

0 Answers0