2

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dustin
  • 21
  • 4
  • [Update your question](https://stackoverflow.com/posts/65999678/edit) with a proper [mcve], including code needed to reproduce the problem, required input, and expected output, and actual output *as text*. We do not know what a `student` looks like, nor do we know what you're providing as input, and we're not mind-readers; you have to *show us*. If we can copy/paste/build/run the code you post (and *only* the code you post), provide the input you specify (and *only* the input you specify), and produce the problem, we can probably help you nail down where that problem comes from. – WhozCraig Feb 01 '21 at 20:33
  • 4
    @tadman: `"%s"` by itself already ignores leading newlines. You really do not want to add `"\n"` to the end of `scanf()` *format string*. – pmg Feb 01 '21 at 20:35
  • Is that the actual code? The broken line shows there is a space on the left of each prompt shown, but it is not in the code. But the code does have a space at the right of each prompt, which does not appear in the output. – Weather Vane Feb 01 '21 at 20:35
  • That said, `&student.name` as a argument to `scanf` is highly questionable (likewise to all the other instances). If `name` is a `char[N]`, then that should be `student.name`, if it anything *other* than some `char[N]`, it is most-certainly wrong anyway. Also, `%s` skips preceding whitespace, including newline, on extraction, and terminates extraction at end-of-string or encounter of whitespace. Therefore, there should be no reason to special handle it if all those fields are really `char[N]` and `%s` use is proper. – WhozCraig Feb 01 '21 at 20:38
  • @Dustin Thanks for posting `struct Student`. Based on that, the `scanf` calls are not obviously wrong. (Well, you need to get rid of the `&` on each one, but that's not what's causing your problem.) – Steve Summit Feb 01 '21 at 20:44
  • updated the code to display the entire function, and the struct to see size of chars. Can remove the & from &student.name and still same issue. – Dustin Feb 01 '21 at 20:44
  • 2
    Using `%s` without any length restriction is as bad as using `gets()`. You should have, for example `scanf("%8s", student.id);` Perhaps the damage was caused by a previous input overflowing array bounds. – Weather Vane Feb 01 '21 at 20:44
  • @WeatherVane True, but that's not what's causing his problem, I don't think. – Steve Summit Feb 01 '21 at 20:45
  • @Steve I added a little more: perhaps *already* damaged. – Weather Vane Feb 01 '21 at 20:46
  • @Dustin Your printout "No matching ID found" is misplaced, but I don't think that's causing your problem, either. – Steve Summit Feb 01 '21 at 20:46
  • @Dustin Other than the weird broken line in the "Updated email ID" prompt, is the rest of the update code working? – Steve Summit Feb 01 '21 at 20:48
  • @SteveSummit yeah the rest of the code is working fine, it's just the strange broken line. Removed the "No Matching ID found" thanks! – Dustin Feb 01 '21 at 20:49
  • @Dustin I don't think we're going be able to solve it based on the information we have here. You're going to get lots of suggestions, either good ones but which don't solve your problem (don't use `&`, do use `%9s`), or things that won't make any difference at all (`fflush(stdout)`). – Steve Summit Feb 01 '21 at 20:51
  • hmm tried the fflush(stdout); after every printf and still same issue, on the email printf. @user3121023 – Dustin Feb 01 '21 at 20:52
  • @Dustin Weather Vane is right, there's some chance that some earlier part of your program (that we can't see) is doing something squirrelly and causing the problem. – Steve Summit Feb 01 '21 at 20:53
  • Thanks @SteveSummit I really appreciate it. I've added the entire code, which I probably should have done from the beginning. – Dustin Feb 01 '21 at 20:53
  • 1
    I tried your program on my computer and I don't see the "broken" line. You've got a few more extra `&` still to get rid of, and it wouldn't hurt to use `%9s` for ID, `%29s` for name, etc., but those aren't what's causing the problem you asked abut. As I suspected, there's nothing we can see that explains the problem you're asking about, it's weird. – Steve Summit Feb 01 '21 at 21:00
  • 4
    Use an actual dedicated terminal program, do not rely on the IDE properly emulating one. – n. m. could be an AI Feb 01 '21 at 21:01
  • @SteveSummit Thanks for the help, will definitely implement the changes and remove the extra &'s. It's good to know, I'll run it in a dedicated terminal program also to see. – Dustin Feb 01 '21 at 21:04
  • 1
    You may also want to know that your program has zero error checks, so a user's mistake can easily lead to [undefined behaviour](https://en.wikipedia.org/wiki/Undefined_behavior) and UI from hell, so a user is guaranteed to make mistakes. – n. m. could be an AI Feb 01 '21 at 21:12
  • @n.'pronouns'm. thanks! Yeah, definitely planning to add error checks was just curious about this issue. Ran the program in a terminal and like you said had no weird spacing issue. – Dustin Feb 01 '21 at 21:14

0 Answers0