Home > database >  What am I doing wrong when getting this user input?
What am I doing wrong when getting this user input?

Time:10-18

I am not understanding why the user who runs this program does not get an option to enter the students full name after they selected #4 on the menu... It prompts for all of the other variables of the struct, but it skips over the first one.

I have just been trying out random things, so I apologize if this code looks like a mess, I can hardly read it myself at this point. We are not allowed to use arrays in this project, which is why I am doing all these weird things with pointers.

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

// Ok, first we must make a struct that holds these things:
//  StudentID
//  Class Name
//  ClassID (like INET3101)
//  StudentFullName


struct record {
    int studentID;
    char *class_name;
    char *classID;
    char *full_name;
};

int num_rec = 0;
void add_record(struct record);
void remove_record(struct record);
void print_record(struct record);

struct record *database;



void add_record(struct record addTemp){

    num_rec  ;
    if (num_rec ==1) {
        database = malloc(sizeof(struct record));
        memcpy(database, &addTemp, sizeof(struct record));
    }
    else {
        struct record *tempMem = malloc(sizeof(struct record) * num_rec);
        struct record *mover = database;
        struct record *head = tempMem;
        int i;
        for (i=0; i< num_rec; i  ){
            memcpy(tempMem  , mover  , sizeof(struct record));  
        }
        memcpy(--tempMem, &addTemp, sizeof(struct record));
        free(database);
        database = head;

    }
}

void remove_record(struct record removeTemp){
    if (num_rec == 0){
        printf("There is nothing to delete here!");
        return;
    }

    else{

        struct record *tempMem= malloc(sizeof(struct record) * num_rec-1);
        struct record *mover = database;
        struct record *head = tempMem;
        int i;

                for (i = 0; i < num_rec-1; i  ){
            memcpy(tempMem, &removeTemp, sizeof(struct record));
        }
        memcpy(--tempMem, &removeTemp, sizeof(struct record));
        free(database);
        database = head;
        num_rec--;

    }
}
    

void printRecords(){
    int i;
    for(i = 0; i < num_rec; i  ) {  
        int k = i   1;
        printf("Student #%d\n", k);
        fprintf(stderr, "Student full name: %s\n", (database   i)->full_name);
        fprintf(stderr, "Student ID: %d\n", (database   i)->studentID);
        fprintf(stderr, "Class name: %s\n", (database   i)->class_name);
        fprintf(stderr, "Class ID: %s\n", (database)->classID);
        printf("\n");
    }
}




// }


// 1. print all records
// 2. print number of records
// 3. print size of database
// 4. add record
// 5. delete record
// 6. print number of accesses to a database
// 7. exit



int main(){

    int BUFSIZE = 100;
    struct record ultimate;

    static int accesses=0; 
    int choice=0;

    while (true) {
        printf("1. print all records \n");
        printf("2. print number of records \n");
        printf("3. print size of database \n");
        printf("4. add record \n");
        printf("5. delete record \n");
        printf("6. print number of accesses to a database \n");
        printf("7. exit \n");
        scanf("%d", &choice);
    
        switch (choice){
            case 1: 
            ;
            
            printf("This is our current database of records: ");
            if (num_rec == 0){
                printf("Oh... It looks like the data base is currently empty.\n");
                accesses  ;
                break;
            }
            else{
                printRecords();
                accesses  ;
                break;
            }
            
            case 2:
                ;
                printf("The current number of student records is: %d records\n", num_rec);
                accesses  ;


            // 3. print size of database
            case 3:
                ;
                int size_database;
                int size_struct = sizeof(struct record);
                size_database = size_struct * num_rec;
                printf("The size of our database is %i\n", size_database);
                break;
            
            // 4. add record
            case 4:
                ;
                char *buf1 = (char*) malloc(BUFSIZE);
                char *buf2 = (char*) malloc(BUFSIZE);
                char *buf3 = (char*) malloc(BUFSIZE);
                char *buf4 = (char*) malloc(BUFSIZE);
                
                
                struct record ultimat;
                

                printf("Please enter this students Full Name: \n");
                fgets(buf1, BUFSIZE, stdin);
                if ((strlen(buf1) > 0) && (buf1[strlen (buf1) - 1] == '\n'))
                    buf1[strlen (buf1) - 1] = '\0';
                ultimat.full_name = malloc(strlen(buf1) 1);
                strcpy(ultimat.full_name, buf1);

                int id;
                printf("Please enter this students Student ID: \n");
                fgets(buf2, BUFSIZE, stdin);
                if ((strlen(buf2) > 0) && (buf2[strlen (buf2) - 1] == '\n'))
                    buf2[strlen (buf2) - 1] = '\0';
                ultimat.studentID = id;

                printf("Please enter the Class Name: \n");
                fgets(buf3, BUFSIZE, stdin);
                if ((strlen(buf3) > 0) && (buf3[strlen (buf3) - 1] == '\n'))
                    buf3[strlen (buf3) - 1] = '\0';
                ultimat.class_name = malloc(strlen(buf3) 1);
                strcpy(ultimat.class_name, buf3);

                printf("Please enter this students Class ID: \n");
                fgets(buf4, BUFSIZE, stdin);
                if ((strlen(buf4) > 0) && (buf4[strlen (buf4) - 1] == '\n'))
                    buf4[strlen (buf4) - 1] = '\0';
                ultimat.classID = malloc(strlen(buf4) 1);
                strcpy(ultimat.classID, buf4);

                add_record(ultimat);
                printf("Alright, I added this student to the database! \n");
                
                free(buf1);
                free(buf2);
                free(buf3);
                free(buf4);
                
                accesses  ;
                break;
            
            // 5. delete record
            case 5:
                ;
                remove_record(ultimate);
                printf("Alright! I got the student at the top of the list deleted! \n");
                break;

            // 6. print number of accesses to a database
            case 6:
                ;
                printf("The current number of data base accesses is: %d acesses\n", accesses);
                break;

            case 7:
                exit(0);
        }
    }return 0;
}

CodePudding user response:

After this call of scanf

scanf("%d", &choice);

the input buffer contains the new line character '\n' that you need to remove before the first call of fgets

fgets(buf1, BUFSIZE, stdin);

Otherwise an empty string will be entered.

You could do it for example the following way

while (getchar() != '\n');
  • Related