2016-09-30 115 views
-3

使用switch語句的簡單項目。我有4個選擇,1-3個很好。 switch語句中的第4個選項(case 4,duh)必須提示用戶「謝謝!」,然後等待幾秒鐘以便他們可以讀取它,然後程序結束。我得到了「謝謝你!」部分向下xD。提示用戶,等待幾秒鐘,然後結束程序?

我只是不知道如何自動結束程序。我嘗試了exit(0)函數,沒有運氣。另外請記住,我需要在關閉之前將消息顯示幾秒鐘。

下面的代碼:

/* ------------------------------------------------- 
    The purpose of this program is to simulate a 
    basic ATM machine and display four key options: 
    deposit, withdraw, check balance, and exit. 
    A switch statement must be used. 
------------------------------------------------ */ 


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

int main() 
{ 

    // Assume a balance of $500.00 for all users. 
    #define BALANCE 500.00 

    // Declare variables. 
    int iMenuSelect = 0; 
    double dUserDeposit = 0.0, dNewBalance = 0.0; 
    double dUserWithdraw = 0.0; 

    //Print the menu to the console. 
    printf("\t*******************\n"); 
    printf("\t1 - Deposit\n"); 
    printf("\t2 - Withdraw\n"); 
    printf("\t3 - Check Balance\n"); 
    printf("\t4 - Exit\n"); 
    printf("\t*******************\n\n"); 

    //Prompt the user for their selection and store the value. 
    printf("Please type the number of the option you would like to perform > "); 
    scanf("%d", &iMenuSelect); 

    //Begin switch statement of variable iMenuSelect. 
    switch(iMenuSelect) 
    { 
     // Deposit, create new balance. 
     case 1: 

      // Ask for deposit amount, then add it and print new balance. 
      printf("\nHow much would you like to deposit? > "); 
      scanf("%lf", &dUserDeposit); 

      // Create and display new balance after deposit. 
      dNewBalance = dUserDeposit + BALANCE; 

      printf("\nYour new balance is $%.2f.\n", dNewBalance); 
      break; 

     // Withdraw, create new balance. 
     case 2: 

      // Ask for withdraw amount, then subtract it and print new balance. 
      printf("\nHow much would you like to withdraw? > "); 
      scanf("%lf", &dUserWithdraw); 

      // Create and display new balance. 
      dNewBalance = BALANCE - dUserWithdraw; 

      if(dUserWithdraw <= 500) 
      { 
       printf("\nHere is your money. Your new balance is $%.2f.\n", dNewBalance); 
      } 

      else 
      { 
       printf("\nYou have insufficient funds.\n"); 
      } 

      break; 

     // Check balance, display BALANCE. 
     case 3: 

      // Display balance. 
      printf("\nYour balance is %.2f\n", BALANCE); 

      break; 

     // Exit program. 
     case 4: exit(EXIT_FAILURE); 
      break; 

     default: printf("\n\nWARNING: Invalid option selected.\n\n"); 
    } 

    return 0; 

} 

編輯2016年11月19日:

我可以只使用系統( 「暫停」);並等待用戶輸入何時退出。自發布以來,我已經更精通編程了。抱歉讓網站混亂。

+0

沒有代碼它會很難... –

+0

請您附上代碼以供參考。查看關於[關於調試幫助的完整最小和可驗證示例]的指導原則(http://stackoverflow.com/help/mcve)。 –

+0

「* show *」和「* close *」「* message *」?你使用任何窗口系統? – alk

回答

1

您可以使用標準的clock()功能來製作您自己的便攜式延遲功能。

#include <stdio.h> 
#include <time.h> 

void sleeper(unsigned seconds) 
{ 
    clock_t start, period, elapsed; 
    period = seconds * CLOCKS_PER_SEC; 
    start = clock(); 
    do { 
     elapsed = clock() - start; 
    } while(elapsed < period); 
} 

int main(void) { 
    printf("Hello, World!\n"); 
    sleeper(3); 
    return 0; 
} 
相關問題