2017-04-17 27 views
-1

我想要獲取變量:float managerTotal,hourlyTotal,commissionTotal和pieceworkerTotal從它們各自的函數打印在switch語句中:'q'在choiceInput函數中。昨天我有很多很多的錯誤,現在我讓它們通過,但是當我爲變量輸入一個輸入時,它不會傳遞值,它們只會返回0.請有人幫忙解釋我做錯了什麼。謝謝。C編程 - 返回不工作,因爲他們應該

下面是代碼:

#include<stdio.h> 
#include<conio.h> 


void welcome() { 
    printf("-----------------------------------------------\n"); 
    printf("Welcome to the Factory A Payroll\n"); 
} 

void choosePayroll() { 
    printf("-----------------------------------------------\n"); 
    printf("Press '1' to access the Manager's payroll\n"); 
    printf("Press '2' to access the Hourly worker's payroll\n"); 
    printf("Press '3' to access Commission worker's payroll\n"); 
    printf("Press '4' to access Pieceworkers's payroll\n"); 
    printf("Press 'Q' to access Manager's payroll\n"); 
    printf("-----------------------------------------------\n"); 
    choiceInput(); 
} 

float managerIntro() { 
    float managerTotal; 
    char charReturn = 0; 
    printf("-----------------------------------------------\n"); 
    printf("You have selected 'Manager's payroll'\n"); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter your fixed weekly salary: $"); 
    scanf_s("%f", &managerTotal); 
    printf("-----------------------------------------------\n"); 
    printf("You have entered $%.2f as the weekly fixed salary\n", managerTotal); 
    printf("-----------------------------------------------\n"); 
    printf("Press 'R' to return back to the main menu\n\n"); 
    scanf_s(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace 
    switch (charReturn) 
    { 
    case 'r': 
     choosePayroll(); 
    default: 
     printf("You have entered an invalid key, press any key to try again\n"); 
     getchar(); 
     getchar(); 
     hourlyIntro(); 
    } 
    return managerTotal; 
} 

float hourlyIntro() { 
    char charReturn = 0; 
    float hourlyWage = 0; 
    float hoursWorked = 0; 
    float overTimeHours = 0; 
    float normalPay = 0; 
    float overTimePay = 0; 
    float hourlyTotal; 

    printf("-----------------------------------------------\n"); 
    printf("You have selected 'Hourly worker's payroll'\n"); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the hourly wage for the worker: $"); 
    scanf_s("%f", &hourlyWage); 
    printf("-----------------------------------------------\n"); 
    printf("You have entered $%.2f as the hourly wage.", hourlyWage); 
    printf("\n-----------------------------------------------"); 
    printf("\nPlease enter how many hours the worker has worked this week: "); 
    scanf_s("%f", &hoursWorked); 
    printf("\n-----------------------------------------------"); 
    if (hoursWorked > 40) { 
     overTimeHours = hoursWorked - 40; 
     hoursWorked = 40; 
    } 
    normalPay = hourlyWage * hoursWorked; 
    overTimePay = (hourlyWage * 1.5) * overTimeHours; 
    hourlyTotal = normalPay + overTimeHours; 
    printf("\nNormal weekly pay : $%.2f\nOvertime weekly pay: $%.2f\n\nTotal weekly pay: $%.2f\n\n", normalPay, overTimePay, hourlyTotal); 
    printf("Press 'R' to return back to the main menu\n"); 
    scanf_s(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace 
    switch (charReturn) 
    { 
    case 'r': 
     choosePayroll(); 
    default: 
     printf("You have entered an invalid key, press any key to try again\n"); 
     getchar(); 
     getchar(); 
     hourlyIntro(); 
    } 
    return hourlyTotal; 
} 

float commissionIntro() { 
    char charReturn = 0; 
    float basicSalary = 250; 
    float itemAPrice = 0; 
    float itemBPrice = 0; 
    float itemCPrice = 0; 
    float itemAQuantity = 0; 
    float itemBQuantity = 0; 
    float itemCQuantity = 0; 
    float itemACommission = 0; 
    float itemBCommission = 0; 
    float itemCCommission = 0; 
    float totalCommission = 0; 
    float commissionTotal = 0; 

    printf("-----------------------------------------------\n"); 
    printf("You have selected 'Commission worker's payroll'\n"); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the price of Item A: $"); 
    scanf_s("%f", &itemAPrice); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the quantity of Item A sold this week: "); 
    scanf_s("%f", &itemAQuantity); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the price of Item B: $"); 
    scanf_s("%f", &itemBPrice); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the quantity of Item B sold this week: "); 
    scanf_s("%f", &itemBQuantity); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the price of Item C: $"); 
    scanf_s("%f", &itemCPrice); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the quantity of Item C sold this week: "); 
    scanf_s("%f", &itemCQuantity); 
    printf("\n-----------------------------------------------\n"); 

    itemACommission = itemAQuantity * (itemAPrice * 0.057); 
    itemBCommission = itemBQuantity * (itemBPrice * 0.064); 
    itemCCommission = itemCQuantity * (itemCPrice * 0.072); 
    totalCommission = itemACommission + itemBCommission + itemCCommission; 
    commissionTotal = totalCommission + basicSalary; 
    printf("Commission for item A: $%.2f\n", itemACommission); 
    printf("Commission for item B: $%.2f\n", itemBCommission); 
    printf("Commission for item C: $%.2f\n", itemCCommission); 
    printf("Total commission: $%.2f\n", totalCommission); 
    printf("Total pay: $%.2f\n", commissionTotal); 
    printf("-----------------------------------------------\n"); 
    printf("Press 'R' to return back to the main menu\n\n");; 
    scanf_s(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace 
    switch (charReturn) 
    { 
    case 'r': 
     choosePayroll(); 
    default: 
     printf("You have entered an invalid key, press any key to try again\n"); 
     getchar(); 
     getchar(); 
     hourlyIntro(); 
    } 
    return commissionTotal; 
} 

float pieceworkerIntro() { 
    char charReturn; 
    float quantityItem1; 
    float quantityItem2; 
    float quantityItem3; 
    float priceItem1 = 22.50; 
    float priceItem2 = 24.50; 
    float priceItem3 = 26; 
    float payItem1; 
    float payItem2; 
    float payItem3; 
    float pieceworkerTotal; 

    printf("-----------------------------------------------\n"); 
    printf("You have selected 'Pieceworker's payroll'\n"); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the quantity of Item 1 produced this week: "); 
    scanf_s("%f", &quantityItem1); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the quantity of Item 2 produced this week: "); 
    scanf_s("%f", &quantityItem2); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the quantity of Item 3 produced this week: "); 
    scanf_s("%f", &quantityItem3); 
    printf("-----------------------------------------------\n"); 

    payItem1 = quantityItem1 * priceItem1; 
    payItem2 = quantityItem2 * priceItem2; 
    payItem3 = quantityItem3 * priceItem3; 
    pieceworkerTotal = payItem1 + payItem2 + payItem3; 
    printf("\nItem 1 pay: $%.2f\nItem 2 pay: $%.2f\nItem 3 pay: $%.2f\n\nTotal pay: $%.2f", payItem1, payItem2, payItem3); 
    printf("Press 'R' to return back to the main menu\n\n"); 
    scanf_s(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace 
    switch (charReturn) 
    { 
    case 'r': 
     choosePayroll(); 
    default: 
     printf("You have entered an invalid key, press any key to try again\n"); 
     getchar(); 
     getchar(); 
     hourlyIntro(); 
    } 
} 

void returnMenu() { 

} 

float choiceInput(float *managerTotal, float *hourlyTotal, float 
    *commissionTotal, float *pieceworkerTotal) { 
    char userChoice = 0; 
    scanf_s(" %c", &userChoice); 
    do { 
     switch (userChoice) { 
     case '1': 
      managerIntro(); 
      break; 
     case '2': 
      hourlyIntro(); 
      break; 
     case '3': 
      commissionIntro(); 
      break; 
     case '4': 
      pieceworkerIntro(); 
      break; 
     case 'q': 
      printf("Manager total is: $%.2f", &managerTotal); 
      printf("Hourly total is: $%.2f", &hourlyTotal); 
      printf("Commission total is: $%.2f", &commissionTotal); 
      printf("Pieceworker total is: $%.2f", &pieceworkerTotal); 
      exit(0); 
     default: 
      printf("You have entered an invalid key, please try again\n"); 
      getchar(); 
      getchar(); 
      choosePayroll(); 
      break; 
     } 
    } while (userChoice != 'Q'); 
} 

int main(void) { 
    welcome(); 
    choosePayroll(); 

    return 0; 
} 

注:我道歉,這是稍長的代碼,但原因我一直保留着這樣的說法是因爲上次我把它縮短它最終沒有工作對於我的實際代碼,只有我發佈的短代碼作爲示例。

+2

尋求調試幫助的問題(「爲什麼不是這個代碼工作?」)必須包含所需的行爲,特定的問題或錯誤以及在問題**中重現**所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。請參閱:如何創建[mcve]。 – Olaf

+1

歡迎來到Stack Overflow!您介意創建[___MCVE___](http://stackoverflow.com/help/mcve)? –

+0

我會再給它:)給我5,如果在任何人想要給它一槍,我會很感激。 – Napstur

回答

0

我在您的代碼中進行了更改。檢查一下,如果這個改變不是按照你的要求告訴我的。

#include <stdio.h> 


float choiceInput(); 
void welcome(); 
void choosePayroll(); 
float managerIntro(); 
float hourlyIntro(); 
float commissionIntro(); 
float pieceworkerIntro(); 
float choiceInput(); 

int main() 
{ 
    welcome(); 
    choosePayroll(); 

    return 0; 
} 

void welcome() 
{ 
    printf("-----------------------------------------------\n"); 
    printf("Welcome to the Factory A Payroll\n"); 
} 

void choosePayroll() 
{ 
    printf("-----------------------------------------------\n"); 
    printf("Press '1' to access the Manager's payroll\n"); 
    printf("Press '2' to access the Hourly worker's payroll\n"); 
    printf("Press '3' to access Commission worker's payroll\n"); 
    printf("Press '4' to access Pieceworkers's payroll\n"); 
    printf("Press 'Q' to access Manager's payroll\n"); 
    printf("-----------------------------------------------\n"); 
    choiceInput(); 
} 

float managerIntro() 
{ 
    float managerTotal; 
    char charReturn = 0; 
    printf("-----------------------------------------------\n"); 
    printf("You have selected 'Manager's payroll'\n"); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter your fixed weekly salary: $"); 
    scanf("%f", &managerTotal); 
    printf("-----------------------------------------------\n"); 
    printf("You have entered $%.2f as the weekly fixed salary\n", managerTotal); 
    printf("-----------------------------------------------\n"); 
    printf("Press 'R' to return back to the main menu\n\n"); 
    scanf(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace 
    switch (charReturn) 
    { 
    case 'r': 
     choosePayroll(); 
    default: 
     printf("You have entered an invalid key, press any key to try again\n"); 
     getchar(); 
     getchar(); 
     hourlyIntro(); 
    } 
    return managerTotal; 
} 

float hourlyIntro() 
{ 
    char charReturn = 0; 
    float hourlyWage = 0; 
    float hoursWorked = 0; 
    float overTimeHours = 0; 
    float normalPay = 0; 
    float overTimePay = 0; 
    float hourlyTotal; 

    printf("-----------------------------------------------\n"); 
    printf("You have selected 'Hourly worker's payroll'\n"); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the hourly wage for the worker: $"); 
    scanf("%f", &hourlyWage); 
    printf("-----------------------------------------------\n"); 
    printf("You have entered $%.2f as the hourly wage.", hourlyWage); 
    printf("\n-----------------------------------------------"); 
    printf("\nPlease enter how many hours the worker has worked this week: "); 
    scanf("%f", &hoursWorked); 
    printf("\n-----------------------------------------------"); 
    if (hoursWorked > 40) { 
     overTimeHours = hoursWorked - 40; 
     hoursWorked = 40; 
    } 
    normalPay = hourlyWage * hoursWorked; 
    overTimePay = (hourlyWage * 1.5) * overTimeHours; 
    hourlyTotal = normalPay + overTimeHours; 
    printf("\nNormal weekly pay : $%.2f\nOvertime weekly pay: $%.2f\n\nTotal weekly pay: $%.2f\n\n", normalPay, overTimePay, hourlyTotal); 
    printf("Press 'R' to return back to the main menu\n"); 
    scanf(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace 
    switch (charReturn) 
    { 
    case 'r': 
     choosePayroll(); 
    default: 
     printf("You have entered an invalid key, press any key to try again\n"); 
     getchar(); 
     getchar(); 
     hourlyIntro(); 
    } 
    return hourlyTotal; 
} 

float commissionIntro() { 
    char charReturn = 0; 
    float basicSalary = 250; 
    float itemAPrice = 0; 
    float itemBPrice = 0; 
    float itemCPrice = 0; 
    float itemAQuantity = 0; 
    float itemBQuantity = 0; 
    float itemCQuantity = 0; 
    float itemACommission = 0; 
    float itemBCommission = 0; 
    float itemCCommission = 0; 
    float totalCommission = 0; 
    float commissionTotal = 0; 

    printf("-----------------------------------------------\n"); 
    printf("You have selected 'Commission worker's payroll'\n"); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the price of Item A: $"); 
    scanf("%f", &itemAPrice); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the quantity of Item A sold this week: "); 
    scanf("%f", &itemAQuantity); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the price of Item B: $"); 
    scanf("%f", &itemBPrice); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the quantity of Item B sold this week: "); 
    scanf("%f", &itemBQuantity); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the price of Item C: $"); 
    scanf("%f", &itemCPrice); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the quantity of Item C sold this week: "); 
    scanf("%f", &itemCQuantity); 
    printf("\n-----------------------------------------------\n"); 

    itemACommission = itemAQuantity * (itemAPrice * 0.057); 
    itemBCommission = itemBQuantity * (itemBPrice * 0.064); 
    itemCCommission = itemCQuantity * (itemCPrice * 0.072); 
    totalCommission = itemACommission + itemBCommission + itemCCommission; 
    commissionTotal = totalCommission + basicSalary; 
    printf("Commission for item A: $%.2f\n", itemACommission); 
    printf("Commission for item B: $%.2f\n", itemBCommission); 
    printf("Commission for item C: $%.2f\n", itemCCommission); 
    printf("Total commission: $%.2f\n", totalCommission); 
    printf("Total pay: $%.2f\n", commissionTotal); 
    printf("-----------------------------------------------\n"); 
    printf("Press 'R' to return back to the main menu\n\n");; 
    scanf(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace  
    switch (charReturn)  
    {  
    case 'r': 
     choosePayroll(); 
    default: 
     printf("You have entered an invalid key, press any key to try again\n"); 
     getchar(); 
     getchar(); 
     hourlyIntro(); 
    } 
    return commissionTotal; 
}  

float pieceworkerIntro() { 
    char charReturn; 
    float quantityItem1; 
    float quantityItem2; 
    float quantityItem3; 
    float priceItem1 = 22.50; 
    float priceItem2 = 24.50; 
    float priceItem3 = 26; 
    float payItem1; 
    float payItem2; 
    float payItem3; 
    float pieceworkerTotal; 

    printf("-----------------------------------------------\n"); 
    printf("You have selected 'Pieceworker's payroll'\n"); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the quantity of Item 1 produced this week: "); 
    scanf("%f", &quantityItem1); 
    printf("-----------------------------------------------\n"); 
    printf("Please enter the quantity of Item 2 produced this week: "); 
    scanf("%f", &quantityItem2);  
    printf("-----------------------------------------------\n"); 
    printf("Please enter the quantity of Item 3 produced this week: "); 
    scanf("%f", &quantityItem3); 
    printf("-----------------------------------------------\n"); 

    payItem1 = quantityItem1 * priceItem1; 
    payItem2 = quantityItem2 * priceItem2; 
    payItem3 = quantityItem3 * priceItem3; 
    pieceworkerTotal = payItem1 + payItem2 + payItem3; 
    printf("\nItem 1 pay: $%.2f\nItem 2 pay: $%.2f\nItem 3 pay: $%.2f\n\nTotal pay: $%.2f", payItem1, payItem2, payItem3); 
    printf("Press 'R' to return back to the main menu\n\n"); 
    scanf(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace 
    switch (charReturn) 
    { 
    case 'r':  
     choosePayroll(); 
    default: 
     printf("You have entered an invalid key, press any key to try again\n"); 
     getchar(); 
     getchar(); 
     hourlyIntro(); 
    } 
} 

float choiceInput() 
{ 
    char userChoice; 
    float managerTotal; 
    float hourlyTotal; 
float commissionTotal; 
float pieceworkerTotal; 

    scanf(" %c", &userChoice); 
    do { 
     switch (userChoice) { 
    case '1':  
     managerTotal = managerIntro();  
     break;  
    case '2': 
     hourlyTotal = hourlyIntro(); 
     break; 
    case '3': 
     commissionTotal = commissionIntro(); 
     break; 
    case '4': 
     pieceworkerTotal = pieceworkerIntro(); 
     break; 
    case 'q': 
     printf("Manager total is: $%.2f", managerTotal); 
     printf("Hourly total is: $%.2f", hourlyTotal); 
     printf("Commission total is: $%.2f", commissionTotal); 
     printf("Pieceworker total is: $%.2f", pieceworkerTotal); 
     return 0; 
    default: 
     printf("You have entered an invalid key, please try again\n"); 
     getchar(); 
     getchar(); 
     choosePayroll(); 
     break; 
     } 
    } while (userChoice != 'Q'); 
} 
相關問題