Firstly, I would like to thank everyone here in advance. I look very forward to advancing in the realm of computer science, and helping others as I become more proficient.
Now here is my code:
#include <stdio.h> #include <stdlib.h> #define RECORDS 30 /*Questions Formatting display() - can we use spaces to format? Is the patient structure supposed to be global or local in enter()? */ void enter(); void display(); void update(); void loadDisk(); void writeDisk(); void emptyDisk(); void sort(); void clear(); struct patient { char * name; int age; double highBP, lowBP, riskFactor; }; struct patient * db[RECORDS]; int counter = 0; main() { int flag = 1; while (flag == 1) { printf("---------------------------------------------------------\n"); printf("|\t(N)ew record\t(D)isplay db\t(U)pdate record |\n"); printf("|\t(L)oad disk\t(W)rite disk\t(E)mpty disk |\n"); printf("|\t(S)ort db\t(C)lear db\t(Q)uit |\n"); printf("---------------------------------------------------------\n"); printf("choose one: "); char selection = getchar(); printf("selection %c\n", selection); if ((selection == 'n') || (selection == 'N')) { //New record enter(); } else if ((selection == 'd') || (selection == 'D')) { //Display db //printf("display %d\n", flag); display(); } else if ((selection == 'u') || (selection == 'U')) { //Update db update(); } else if ((selection == 'l') || (selection == 'L')) { //Load disk loadDisk(); } else if ((selection == 'w') || (selection == 'W')) { //Write disk writeDisk(); } else if ((selection == 'e') || (selection == 'E')) { //Empty disk emptyDisk(); } else if ((selection == 's') || (selection == 'S')) { //Sort db sort(); } else if ((selection == 'c') || (selection == 'C')) { //Clear db clear(); } else if ((selection == 'q') || (selection == 'Q')) { //Quit flag = 0; } else { printf("not a vaild input\n"); } } } void enter() { /*struct patient temp; printf("name: "); sscanf("%s", temp.name); printf("age: "); scanf("%d", temp.age); printf("high bp: "); scanf("%f", temp.highBP); printf("low bp: "); scanf("%f", temp.lowBP); db[counter] = (struct patient *) calloc(1, sizeof(temp)); *db[counter] = temp; //printf("%s, %d, %f, %f", db[counter]->name, db[counter]->age, db[counter]->highBP, db[counter]->lowBP); counter++;*/ } void display() { } void update() { } void loadDisk() { } void writeDisk() { } void emptyDisk() { } void sort() { } void clear() { } The issue I am having when running it is that the menu displays twice after I enter an option. I am having trouble understanding what is going wrong, but I suspect it has something to do with getchar which storing the selection and the new line character, hence running it twice. This would also mean the final else statement would run, which it does.
I think I have triangulated the problem, just unsure how to fix it. Thank you in advance!
getcharreturns anint, not achar. Before casting it back to anchar, you must first test it against the constantEOFto see if you've hit the end-of-input (e.g., control-D on a Unix-ish terminal, or from end of input in a pipe, or redirected file). You should probably treat that as if the user had enteredq. Also, doesn't C have aswitchstatement?