为什么我的C程序没有接收任何输入或提示插入功能启动?

时间:2015-02-18 02:32:24

标签: c

我知道有一些错误,但有人可以向我解释为什么当我被按下时它甚至不会起作用?非常困惑 我只包括我的部分代码 另外我知道我尝试对数组中的联系人进行排序的方法还不错。你可以忽略它。谢谢!

#define max 100
typedef enum {diploma, bachelor, master, doctor} education;

struct person {                 
char name[30];
char email[30];
int phone;
education degree;
};

 struct person directory[max];   //an array of structures
 int tail = 0;                   //global variable

 void flush();                   
 void branching(char c);
 int insertion();
 int print_persons(int i);
 int print_all();
 int search_person();
 int delete_person();

 int main() {                    //print a menu for selection
 char ch = 'i';

 ungetc('\n', stdin);        //inject the newline character into input buffer

do{
    printf("Enter your selection \n");
    printf("\ti: Insert a new entry \n");
    printf("\td: Delete an entry \n");
    printf("\ts: Search an entry \n");
    printf("\tp: Print all entries \n");
    printf("\tq: Quit \n");

    flush();                //flush the input buffer
    ch = tolower(getchar());
    branching(ch);

} while (ch!=113);
return 0;
}

   void flush(){
    int c;
    do {
        c = getchar();
    }
    while (c != 'n' && c != EOF); //EOF = end of file
}

void branching (char c) {      //branch to different tasks
     switch (c) {
        case 'i':
           insertion();
           break;
        case 's':
           search_person();
           break;
        case 'd':
           delete_person();
           break;
        case 'p':
            print_all();
            break;
        case 'q':
            break;
        default:
            printf("Invalid input\n");
    }
}

 int insertion() {             //insert a new entry at the end

 if (tail == max){

    printf("There are no more places to insert.\n");
    return -1;
}
else{
    printf("Enter name, phone, email, education(diploma, bachelor, master, doctor):\n"); //added education
    scanf("%s", directory[tail].name);
    scanf("%d", &directory[tail].phone, sizeof(directory[tail].phone));
    scanf("%s", directory[tail].email);
    scanf("%d", &directory[tail].degree,sizeof(education));
    tail++;
    printf("The number of entries = %d\n", tail);
    return 0;
}

0 个答案:

没有答案