Strange new line issues in scanf and printf in C program

Viewed 234

When I run through these prints and scans I get the user input properly, but for some reason the lines seem to break midway through, at bizarre spots.

I can fix it, by adding \n to the start of every printf but then there is a full space between each line.

Is there a way to fix it?

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

struct Student
{
    char id[9];
    char name[30];
    char emailId[10];
    char courseId[10];
    char grade[4];
};
FILE *studentFile;

bool exitMenu = false;

void displayMainMenu();
int makeChoice();
void createRecord();
void displayRecord();
void seekRecord();
void updateRecord();
void deleteRecord();

int main()
{
    while(!exitMenu)
    {
        displayMainMenu();
        makeChoice();
    }
    return 0;
}

void displayMainMenu()
{
    printf("\tM A I N  M E N U");
    printf("\n1. Create the Binary File");
    printf("\n2. Display the contents of the file");
    printf("\n3. Seek a specific record");
    printf("\n4. Update the contents of a record");
    printf("\n5. Delete a record for the specific name");
    printf("\n6. Exit");
    printf("\n\n\t Please Enter your choice .... ");
}

int makeChoice()
{
    int choice = 0;
    scanf("%d", &choice);

    switch(choice)
    {
        case 1:
            //Create the Binary File
            createRecord();
            break;
        case 2:
            //Display the contents of the file
            displayRecord();
            break;
        case 3:
            //Seek a specific record
            seekRecord();
            break;
        case 4:
            //Update a record for the specific name
            updateRecord();
            displayRecord();
            break;
        case 5:
            //Delete a record for the specific name
            deleteRecord();
            displayRecord();
            break;
        case 6:
            //Exit
            exitMenu = true;
            break;
        default:
            break;
    }
    return choice;
}

void createRecord()
{
    studentFile = fopen("studentFile.bin","ab");
    int i;
    struct Student student;

    printf("Enter Student ID: ");
    scanf("%s",student.id);

    printf("Enter Student Name: ");
    scanf("%s",student.name);

    printf("Enter Email ID: ");
    scanf("%s",student.emailId);

    printf("Enter Course ID: ");
    scanf("%s",student.courseId);

    printf("Enter Grade: ");
    scanf("%s",student.grade);

    fwrite(&student,sizeof(student),1,studentFile);
    fclose(studentFile);
}

void displayRecord()
{
    studentFile=fopen("studentFile.bin","rb");
    struct Student student;
    while(fread(&student,sizeof(student),1,studentFile))
    {
        printf("\n------------------------------------------\n");
        printf("Student ID: %s",student.id);
        printf("\nStudent Name: %s",student.name);
        printf("\nEmail ID: %s",student.emailId);
        printf("\nCourse ID: %s",student.courseId);
        printf("\nGrade: %s",student.grade);
        printf("\n------------------------------------------\n");
    }
    fclose(studentFile);
}

void seekRecord()
{
    struct Student student;
    char id[9];
    printf("Enter Student ID: ");
    scanf("%s",id);
    studentFile = fopen("studentFile.bin","rb");

    while(fread(&student,sizeof(student),1,studentFile))
    {
        if(strcmp(student.id,id)==0)
        {
            printf("\tStudent Record Found.");
            printf("\n------------------------------------------\n");
            printf("Student ID: %s",&student.id);
            printf("\nStudent Name: %s",&student.name);
            printf("\nEmail ID: %s",&student.emailId);
            printf("\nCourse ID: %s",&student.courseId);
            printf("\nGrade: %s",&student.grade);
            printf("\n------------------------------------------\n");
            break;
        }
    }
}

void updateRecord()
{
    FILE *tempFile;
    char id[9];
    printf("Enter Student ID: ");
    scanf("%s",id);

    studentFile=fopen("studentFile.bin","rb");
    tempFile = fopen("tempFile.bin","wb");

    struct Student student;
    while(fread(&student,sizeof(student),1,studentFile))
    {
        if(strcmp(student.id,id)==0)
        {
            printf("Enter Updated Student ID: ");
            scanf("%s",student.id);

            printf("Enter Updated Student Name: ");
            scanf("%s",student.name);

            printf("Enter Updated Email ID: ");
            scanf("%s",student.emailId);

            printf("Enter Updated Course ID: ");
            scanf("%s",student.courseId);

            printf("Enter Updated Grade: ");
            scanf("%s",student.grade);
            fwrite(&student,sizeof(student),1,tempFile);
        }
        else
        {
            fwrite(&student,sizeof(student),1,tempFile);
        }
    }
    fclose(studentFile);
    fclose(tempFile);
    remove("studentFile.bin");
    rename("tempFile.bin","studentFile.bin");
}

void deleteRecord()
{
    FILE *tempFile;
    struct Student student;
    char name[30];
    printf("Enter Student Name: ");
    scanf("%s",name);
    studentFile = fopen("studentFile.bin","rb");
    tempFile = fopen("tempFile.bin","wb");

    while(fread(&student,sizeof(student),1,studentFile))
    {
        if(strcmp(student.name,name) !=0)
        {
            fwrite(&student,sizeof(student),1,tempFile);
        }
    }

    fclose(studentFile);
    fclose(tempFile);
    remove("studentFile.bin");
    rename("tempFile.bin","studentFile.bin");
}

This is written in C, I'm also using C Lion if that helps. Image posted below of whats displaying

enter image description here

0 Answers
Related