2

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!

1
  • 1
    FYI: getchar returns an int, not a char. Before casting it back to an char, you must first test it against the constant EOF to 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 entered q. Also, doesn't C have a switch statement? Commented Jul 13, 2011 at 18:48

4 Answers 4

2

If the problem is with getchar, which it does look to be, why not use a different function?

Try replacing:

char selection = getchar(); 

With this:

char selection; scanf("%c",&selection); 

If you're worried about overflow in the single character, then do a scanf() for a string and only use the first character in your checks:

char selection, selectionstr[20]; scanf("%s",selectionstr); selection = selectionstr[0]; 
Sign up to request clarification or add additional context in comments.

3 Comments

I tried your first solution, it didn't work. Same problem. Am I trying your second one now. Thanks!
Bingo! Your second one worked! Now I am trying to figure out why your first didn't, as I tried the exact same thing before...Thanks again!
Happy to help. I would recommend looking at the comment @derobert left in your original statement about using a switch-case block rather than a bunch of if-else statements.
1

getchar also returns '\n' character.

1 Comment

Thanks! I guessed that was what was causing the issue.
0

Yeah, the problem is that your input is always a string, at least one character followed by a newline. I would either change your loop so that it terminates if selection is 'q' or use a function other than getchar and prune your input.

2 Comments

Any recommendations besides getchar? Thanks!
I would try scanf or one of it's cousins.
0

I think you can do this using curses! Here is a a website which you may find useful. Curses is a cursor control library for c.

From the manual:

Initially the terminal may or may not be in cbreak mode, as the mode is inherited; therefore, a program should call cbreak or nocbreak explic- itly. Most interactive programs using curses set the cbreak mode. Note that cbreak overrides raw.

1 Comment

Hmm I'll look into that. For the time being, I have to do what my teacher is asking :) Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.