为什么`system(" cls")`只有在被某个函数调用时才有效?

时间:2016-09-30 11:03:15

标签: c function

我正在开发一个简单的酒店预订管理系统,但我遇到了一个小问题。仅当system("cls");函数调用mainMenu()函数时,mainMenu()函数中的bookRoom()才起作用。我试过用其他功能做得很好,我不知道为什么会这样。

我的错误在哪里?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>

char exitOpt[1],cfm[1];;
int mainMenu_Opt;
int chk = 0;
int advance[4] = {750, 500, 250, 125};
int roomFee[4] = {1500, 1000, 500 ,250};
int rT[4] = {1,2,3,4};
int roomAvail[4] = {1,2,2,5};

struct guest{
    char id[5];
    char name[30];
    int age;
    int r_type;
    int chk_in_date;
    int chk_out_date;
    int per;
    int totPay;
    int paid;
    int balance;
};
struct guest grec;

FILE *fguest,*ftemp;

void main();
void mainMenu();
void checkRoom();
void putRAV();
int bookRoom();
void vldRT();
void readData();
void exitProgram ();

void mainMenu()
{   
    system("cls");

    for(;;)
    {   
        fguest = fopen("guest_list.dat","rb");
        while(fread(&grec,sizeof(grec),1,fguest)==1 )
        {
            if (grec.r_type == chk)
                roomAvail[chk-1]--;
        }
        chk = 0;
        fclose(fguest);     
        printf("\n     Welcome to HRMS \n\n");
        printf("\tMain Menu\n\n");
        printf("1. Check Room's Availability\n\n");
        printf("2. Book A Room\n\n");
        printf("3. Check Out a Room Guest\n\n");
        printf("4. Edit Reservation\n\n");
        printf("5. Search\n\n");
        printf("6. Exit\n\n");
        printf("Please, enter your choice (1-6): ");
        scanf("%d",&mainMenu_Opt);
        fflush(stdin);
        switch(mainMenu_Opt)
        {
            case 1: {   putRAV();
                        break;          }

            case 2: {
                        bookRoom();
                        break;          }

//          case 3: {   chkoRoom();                 
//                      break;          }
//          
//          case 4: {   editRes();                  
//                      break;          }
//          
            case 5: {   readData();                 
                        break;          }

            case 6: {   exitProgram(); 
                        break;          }

            default: printf("\nInvalid Input. Please try again with valid input (whole number between 1 - 6).\n ");
        }
    }
}

void checkRoom()
{   
    system("cls");
    fguest = fopen("guest_list.dat","rb");
    while(fread(&grec,sizeof(grec),1,fguest)==1 )
    {
        switch (grec.r_type)
        {
            case 1: {
                        roomAvail[0]--; 
                        break;                  }
            case 2: {
                        roomAvail[1]--;
                        break;                  }
            case 3: {
                        roomAvail[2]--; 
                        break;                  }   
            case 4: {
                        roomAvail[3]--; 
                        break;                  }
        }   
    }
    fclose(fguest);
}

void putRAV()
{   
    system("cls");
    int j;
    for (j = 0; j < 4; j++)
    {   
        printf("%d\n", roomAvail[j]);
    }

    printf("Back to main (Y/N)?: "); gets(cfm); fflush(stdin);
    if ((strcmp(cfm,"Y")==0) || (strcmp(cfm,"y")==0))
    {   printf("Returning to main menu...\n");
        Sleep(1000);
        mainMenu();                                     }
    else if ((strcmp(cfm,"N")==0) || (strcmp(cfm,"n")==0))
    {   putRAV();                                           }           
    else
    {   printf("\nInvalid Input. Returning to ReadData\n");
        putRAV();                                       }
}

int bookRoom()
{   
    system("cls");
    fflush(stdin);
    grec.totPay = 0;
    fguest = fopen("guest_list.dat","ab+");
    printf("\n\tBook A Room");
    printf("\n\nGuest\'s ID\t\t: ");    scanf("%s",grec.id);                fflush(stdin);
    printf("Guest\'s Name\t\t: ");      scanf("%30s",grec.name);            fflush(stdin);
    printf("Guest\'s Age\t\t: ");       scanf("%d",&grec.age);              fflush(stdin);
    printf("Room\'s Type\t\t: ");       scanf("%d",&grec.r_type);           fflush(stdin);
    vldRT();
    printf("Check-in Date\t\t: ");      scanf("%d",&grec.chk_in_date);      fflush(stdin);
    printf("Check-out Date\t\t: ");     scanf("%d",&grec.chk_out_date);     fflush(stdin);
    printf("Staying Period\t\t: ");     scanf("%d",&grec.per);              fflush(stdin);
    grec.totPay = (roomFee[grec.r_type - 1] * grec.per) - advance[grec.r_type-1];
    printf("Total Payment\t\t: %d\n", grec.totPay); 
    printf("Total Paid\t\t: ");         scanf("%d",&grec.paid);             fflush(stdin);
    grec.balance = grec.totPay - grec.paid;
    printf("Balance\t\t\t: %d \n\n",grec.balance);
    printf("\t\t Confirm Booking (Y/N)?: "); gets(cfm);                     fflush(stdin);
    if ((strcmp(cfm,"Y")==0) || (strcmp(cfm,"y")==0))
    {   fwrite(&grec,sizeof(grec),1,fguest);
        fclose(fguest); 
        chk = grec.r_type;
        printf("Room successfully booked...\n");
        printf("Returning to main menu...\n");
        Sleep(1000);
        return chk;
    }
    else if ((strcmp(cfm,"N")==0) || (strcmp(cfm,"n")==0))
    {   bookRoom();                                             }           
    else
    {   printf("\nInvalid Input. Returning to Book A Room\n");
        bookRoom();                                             }   
    mainMenu();
}

void vldRT()
{
    if (grec.r_type <= 0 || grec.r_type >4)
    {
        printf("Invalid input!! Input must be between 1 - 4\n");
        printf("Please try again:\n");                          
        printf("Room\'s Type\t\t: ");       scanf("%d",&grec.r_type);           fflush(stdin);  }

}

void exitProgram ()
{
    printf("\nExit program (Y/N)? "); gets(exitOpt); fflush(stdin);
    if ((strcmp(exitOpt,"Y")==0) || (strcmp(exitOpt,"y")==0))
        exit(0);
    else if ((strcmp(exitOpt,"N")==0) || (strcmp(exitOpt,"n")==0))
        mainMenu();
    else 
        printf("\nInvalid Input. Please try again with valid input (Y/N). \n");
        exitProgram();
}

void readData()
{   
    system("cls");
    fguest = fopen("guest_list.dat","rb");
    rewind(fguest);
    while(fread(&grec,sizeof(grec),1,fguest)==1)        //continue reading until there's no more struct data
    {
        printf("\n\nGuest\'s ID\t\t: %s", grec.id);
        printf("\nGuest\'s Name\t\t: %s",grec.name);        
        printf("\nGuest\'s Age\t\t: %d",grec.age);      
        printf("\nRoom\'s Type\t\t: %d",grec.r_type);       
        printf("\nCheck-in Date\t\t: %d",grec.chk_in_date); 
        printf("\nCheck-out Date\t\t: %d",grec.chk_out_date);   
        printf("\nStaying Period\t\t: %d",grec.per);            
        printf("\nTotal Payment\t\t: %d", grec.totPay); 
        printf("\nTotal Paid\t\t: %d",grec.paid);
        printf("\nBalance\t\t\t: %d \n\n",grec.balance);
    }
    fclose(fguest);
    printf("Back to main (Y/N)?: "); gets(cfm); fflush(stdin);
    if ((strcmp(cfm,"Y")==0) || (strcmp(cfm,"y")==0))
    {   printf("Returning to main menu...\n");
        Sleep(1000);
        mainMenu();                                     }
    else if ((strcmp(cfm,"N")==0) || (strcmp(cfm,"n")==0))
    {   readData();                                         }           
    else
    {   printf("\nInvalid Input. Returning to ReadData\n");
        readData();                                         }   

}

void main()
{   
    checkRoom();
    mainMenu();
}

1 个答案:

答案 0 :(得分:-2)

为什么不制作可移植的自己的cls功能?

void my_cls(void) {
  int i = 5000;
  while (i-->0)
    printf("\n");
}