Во-первых, я хотел бы поблагодарить всех здесь заранее.Я с нетерпением жду прогресса в области компьютерных наук и помощи другим, поскольку я становлюсь более опытным.
Теперь вот мой код:
#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()
{
}
Проблема, с которой я сталкиваюсь при работеэто то, что меню отображается дважды после ввода опции.У меня проблемы с пониманием, что происходит не так, но я подозреваю, что это как-то связано с getchar, который хранит выделение и символ новой строки, следовательно, запускает его дважды.Это также означало бы выполнение последнего оператора else, который он выполняет.
Я думаю, что я триангулировал проблему, просто не зная, как ее исправить.Заранее спасибо!