2016-04-07 29 views
-1

我正在開發一個程序。這是我想要實現的; 該計劃應該提供一個工資率選擇菜單。使用開關選擇工資率。運行的開始應該如下所示: enter image description hereC代碼,我錯過了這個控制語句程序?

如果選擇了選項1到4,程序應該請求工作的小時數。程序應該循環使用,直到輸入5。如果輸入了選擇1至5之外的其他內容,程序應提醒用戶適當的選擇,然後回收。使用#defined常量獲取各種收益率和稅率。

這是我到目前爲止的代碼,我錯過了什麼?

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

int main() 
{ 
int choice, hour; 
float taxe, total; 

printf("****************************************************************\n"); 
printf("\nEnter the number corresponding to the desired pay rate or action"); 
printf("\n1)$8.75/hr"); 
printf("\n2)$9.33/hr"); 
printf("\n3)$10.00/hr"); 
printf("\n4)$11.20hr"); 
printf("\n5)Quit"); 
printf("**********************************************************\n"); 

scanf("%d", &choice); 

printf("Please enter number of hours: "); 
scanf("%d", &hour); 

switch(choice){ 
case 1: 
    total = 8.75* hour; 
    break; 
case 2: 
    total = 9.33*hour; 
    break; 
case 3: 
    total = 10.00*hour; 
    break; 
case 4: 
    total = 11.20*hour; 
    break; 
case 5: 
    break; 
return 0; 
} 
} 
+0

這聽起來像功課給我,是嗎? – anderas

+0

@KlasLindbäck謝謝你,這是有道理的。我是否需要使用「while」循環,還是需要成爲「do while」循環? – user3247128

回答

2

我缺少什麼?

你缺少的是實際的迭代。

該程序應該回收,直到輸入5。

這意味着你必須做一個循環,這將每次檢查,如果讀出的值是5,您才能停止迭代需要裏面所有的工作。您可以使用下列方法之一:

  1. while循環:

    scanf("%d", &choice); 
    while (choice != 5) 
    { 
        .... 
        switch(choice){ 
         .... 
        } 
        .... 
    scanf("%d", &choice); 
    } 
    
  2. for循環:

    for (scanf("%d", &choice); choice != 5; scanf("%d", &choice);) 
    { 
        .... 
        switch(choice){ 
         .... 
        } 
        .... 
    } 
    
  3. do-while循環:

    do{ 
        scanf("%d", &choice); 
        .... 
        switch(choice){ 
         .... 
        } 
        .... 
    }while(choice != 5); 
    
+0

這是驚人的謝謝你! – user3247128

+0

@ user3247128我很高興它幫助:) – Marievi

2

你沒有使用循環。直到5進入才能回收。

服用選擇後,插入一個while循環:

while(choice != 5){ 

和過程通常交換機,無視5的值,如果進入了另一個值增加一個「默認」的情況。

如果您希望整個程序能夠回收,請使用do {} while();從你的printf開始,結束在交換機之後。

+0

總是取決於用例。按照這些問題來了解要使用的循環。在介紹用例之後,你會明白應該把它放在哪裏。 你知道該程序必須做多少循環嗎? => for循環 程序是否必須在循環中執行一個或多個時間? => while while循環 否則,while循環。有時候,如果聲明不正確,程序可能會中斷。隨着時間的推移,程序必須執行一次聲明,或者更多的聲明是真實的。 – Aeldred

+0

你是如此的樂於助人,謝謝親切的:) – user3247128

1

你喲使用do while循環這裏像下面

do 
{ 
    clrscr(); 
    printf("****************************************************************\n"); 
    printf("\nEnter the number corresponding to the desired pay rate or action"); 
    printf("\n1)$8.75/hr"); 
    printf("\n2)$9.33/hr"); 
    printf("\n3)$10.00/hr"); 
    printf("\n4)$11.20hr"); 
    printf("\n5)Quit"); 
    printf("**********************************************************\n"); 

    scanf("%d", &choice); 

    printf("Please enter number of hours: "); 
    scanf("%d", &hour); 

    switch(choice){ 
    case 1: 
     total = 8.75* hour; 
     break; 
    case 2: 
     total = 9.33*hour; 
     break; 
    case 3: 
     total = 10.00*hour; 
     break; 
    case 4: 
     total = 11.20*hour; 
     break; 
    case 5: 
     break; 
    default : 
     printf("\n please enter proper choice"); 
     getch(); 
    } 
}while(choice !=5);