Reputation: 119
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!
Upvotes: 2
Views: 162
Reputation: 22385
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.
Upvotes: 0
Reputation: 1201
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];
Upvotes: 2
Reputation: 160
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.
Upvotes: 0