2016-09-30 7 views
-3

switch文を使用する単純なプロジェクトです。私は4つの選択肢があり、1〜3つはうまくいく。 switch文(case 4、duh)の4番目の選択肢は、「ありがとう!」とユーザにプロンプ​​トを表示してから、数秒待ってから読んでからプログラムを終了させる必要があります。私は "ありがとう!"パートダウン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; 

} 

EDIT 2016年11月19日:ここで

はコードだ

私は使用している可能性がシステム( "PAUSE");ユーザーは終了時に入力を待つ。私はこれを投稿して以来、プログラミングにもっと熟練しています。サイトを混乱させて申し訳ありません。

+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; 
} 
関連する問題