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.
Any idea what's the issue?
display
you clobber the fileFILE *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 likestruct Subject *b
andint n
are inextricably linked, maybe they should also be astruct
?