//主函數的編寫 #include <stdio.h> #include "head_files.h" intmain(void) { int nights; double hotel_rate; int code; while ((code=menu())!=QUIT) { switch (code) { case 1:hotel_rate=HOTEL1; break; case 2:hotel_rate=HOTEL2; break; case 3:hotel_rate=HOTEL3; break; case 4:hotel_rate=HOTEL4; break; } nights=getnight(); showprice(nights,hotel_rate); } printf("Thank you and goog bye!\n"); return0; }
//定義函數內容 #include <stdio.h> #include "head_files.h" int menu(void) { int code; int return_value;//定義了一個返回值變量 printf("Enter the number of desired hotel :\n"); printf("(1) Fairfield Arms (2) Hotel Olympic\n"); printf("(3) Chertworthy Pleaza (4) The Stockton\n"); printf("(5) Quit\n"); while ((return_value=scanf("%d",&code))!=1||code>5||code<1) { if (return_value!=1)//注意,輸入一個非數字,scanf()會有返回值,但是不會讀取,因此要清空輸入緩存流。 { int ch; while ((ch=getchar())!='\n') { continue; } printf("Please input a number!\n"); } if (code>5||code<1) { printf("Please input a number from 1-5!\n"); } } returncode; } int getnight(void) { int nights; int return_value; printf("Please input the nights you want to live:\n"); while ((return_value=scanf("%d",&nights))!=1||nights<0) { if (return_value!=1)//注意,輸入一個非數字,scanf()會有返回值,但是不會讀取,因此要清空輸入緩存流。 { int ch; while ((ch=getchar())!='\n') { continue; } printf("Please input a number!\n"); } if (nights<0) { printf("Please input a number bigger than 0!\n");