Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct
{
char studentName[50];
int studentId;
} database;
int main(int argc, char *argv[])
{
database school;
FILE *file_ptr;
char text[50];
int begin,temp;
while(1)
{
printf("Input Data? (Yes = 1) : ");
scanf("%d",&begin);
if(begin != 1)
{
break;
}
else
{
printf("\nEnter Student Name: ");
fgets(school.studentName, 50 , stdin);
printf("\nEnter Student ID: ");
scanf("%d", &school.studentId);
printf("\nAdd to school.csv ?(y/n) : ");
getchar();
scanf("%c", &temp);
if(temp == 'y' || temp == 'Y')
{
file_ptr = fopen("school.csv" , "a");
printf("\nFile school.csv opened\n");
fprintf(file_ptr, "%s, %d,\n", school.studentName, school.studentId);
fclose(file_ptr);
printf("File Saved.\n\n");
}
else
{
printf("\nFile not Saved.\n\n");
}
}
}
system("PAUSE");
return 0;
}
The program is skipping over the student name input, so Im assuming the fget is incorrect, how do i fix that?
Code:
printf("\nEnter Student Name: ");
fgets(school.studentName, 50 , stdin);
Also, when I get to the prompt to save, I enter y and it says file not saved. Is it because my fprintf is wrong, if so how do i fix it?
Code:
printf("\nAdd to school.csv ?(y/n) : ");
getchar();
scanf("%c", &temp);
if(temp == 'y' || temp == 'Y')
{
file_ptr = fopen("school.csv" , "a");
printf("\nFile school.csv opened\n");
fprintf(file_ptr, "%s, %d,\n", school.studentName, school.studentId);
fclose(file_ptr);
printf("File Saved.\n\n");
}
other than that, it complies with no bugs atm =/