2

Been trying to figure out how to fix it, nothing seems to work. CodeBlocks Console Issue DevC++ Console Issue Basically tried resetting the compiler settings to default, tried reinstalling problem still prevails, how do I fix this?


    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    struct Subject {
    char scode[30];
    char descript[50];
    int units;
    int lect;
    int lab;
    };
    
    
    int add(struct Subject b[], int *n);
    int display(struct Subject b[], int n);
    int search(struct Subject b[], int n);
    int update(struct Subject b[], int n);
    int del(struct Subject b[], int *n);
    void exit_program();
    void SaveFile(struct Subject b[], int n);
    void LoadFile(struct Subject b[], int *n);
    
    int main() {
    struct Subject b[50];
    int n = 0, ch;
    int i;
    
    LoadFile(b, &n);
    
    do {
        printf("\nA. Add");
        printf("\nB. Display");
        printf("\nS. Search");
        printf("\nU. Update");
        printf("\nD. Delete");
        printf("\nX. Exit");
        printf("\nYour Option -> ");
        ch = getchar();
    
    switch (tolower(ch)) {
        case 'a':
                add(b, &n);
                break;
        case 'b':
                display(b, n);
                break;
        case 's':
                search(b, n);
                break;
        case 'u':
                update(b, n);
                break;
        case 'd':
                del(b, &n);
                break;
        case 'x':
            SaveFile(b, n);
            exit_program();
            break;
        default:
            printf("Invalid option. Please try again.\n");
    }
    
    while (getchar() != '\n');
    } while (ch != 'x');
    
    return 0;
    }
    
    void SaveFile(struct Subject b[], int n) {
    FILE *file = fopen("subjects.txt", "w");
    if (file == NULL) {
    perror("Error opening file for writing");
    return;
    }
    
    for (int i = 0; i < n; i++) {
    fprintf(file, "%s %s %d %d %d\n", b[i].scode, b[i].descript, b[i].units, b[i].lect, b[i].lab);
    }
    
    fclose(file);
    }
    
    void LoadFile(struct Subject b[], int *n) {
    FILE *file = fopen("subjects.txt", "r");
    if (file == NULL) {
    perror("Error opening file for reading");
    return;
    }
    
    while (fscanf(file, "%s %s %d %d %d\n", b[*n].scode, b[*n].descript, &b[*n].units, &b[*n].lect, &b[*n].lab) == 5) {
    (*n)++;
    if (*n >= 50) {
    printf("Warning: Maximum number of subjects reached.\n");
    break;
    }
    }
    
    fclose(file);
    }
    
    int display(struct Subject b[], int n) {
    FILE *file = fopen("subjects.txt", "r");
    int i;
    printf("The Records are: \n");
    printf("SubCode\t\tDescript\tUnits\tLecs\tLabs\n");
    for (i = 0; i < n; i++) {
    printf("\n%s\t%s\t%d\t%d\t%d", b[i].scode, b[i].descript, b[i].units, b[i].lect, b[i].lab);
    }
    fclose(file);
    return 0;
    }
    
    int add(struct Subject b[], int *n) {
    int i;
    printf("Enter number of Subjects: ");
    scanf("%d", n);
    
    for (i = 0; i<*n; i++) {
    printf("\nEnter Subject Code: ");
    scanf("%s", b[i].scode);
    printf("Enter Description: ");
    scanf("%s", b[i].descript);
    printf("Enter Units: ");
    scanf("%d", &b[i].units);
    printf("Enter Lec Units: ");
    scanf("%d", &b[i].lect);
    printf("Enter Lab Units: ");
    scanf("%d", &b[i].lab);
    }
    SaveFile(b, *n);
    
    return 0;
    }

This is the code, basically it's an structure code incorporated with file handling, the issue I'm facing is when I try to display the inputted data, the table is messed up.

**Edit 2 I've asked a friend to run the code and it works fine with his console. Fixed Display on CodeBlocks Fixed Display on DevC++

Any idea what's the issue?

6
  • Apologies first time posting here.
    – Snuffles
    Commented Dec 18, 2023 at 4:44
  • Welcome! Not to discourage you at all; more details encourage better answers. Seems from the screenshot like it's working... do you want it to respond immediately?
    – Neil
    Commented Dec 18, 2023 at 4:56
  • 1
    I've managed to paste a snippet of it, here though I think you have a general idea in how it should work. The display function is messed up somehow, even though It's properly arranged already..
    – Snuffles
    Commented Dec 18, 2023 at 5:03
  • I see in display you clobber the file FILE *file = fopen("subjects.txt", "r"); and you close it right after; this will be an empty file. What do you want it to do? Seems like struct Subject *b and int n are inextricably linked, maybe they should also be a struct?
    – Neil
    Commented Dec 18, 2023 at 5:22
  • Apologies for the late reply, the code works fine it can Add, Delete, Display, Update, and Search. The only issue on me is that it does display somehow, but it's all messed up.
    – Snuffles
    Commented Dec 18, 2023 at 7:41

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.