C 编程 - 返回无法正常工作

标签 c function parameters

我正在尝试从各自的函数中获取变量:float managerTotal、hourlyTotal、commissionTotal 和pieceworkerTotal,以在switch 语句中打印 case: 'q' 在 choiceInput 函数中。昨天我遇到了很多很多错误,现在我让它们通过了,但是当我输入变量的输入时,它没有传递值,它们只返回 0。请有人帮助解释我做错了什么。谢谢。

代码如下:

#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;
}

注意:我很抱歉它的代码有点长,但我保持这种方式的原因是因为上次我把它缩短了,它最终并没有适用于我的实际代码,只有我发布的短代码示例。

最佳答案

我对你的代码进行了更改。检查一下,如果此更改不符合您的要求,请告诉我。

#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');
}

关于C 编程 - 返回无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43447395/

相关文章:

c - 安全使用 MultiByteToWideChar

c - 如何在C中声明参数

c - 默认类型 signed、unsigned、const 和 volatile

c - 不知道如何正确接收和使用用户输入

javascript - 如何将长值传递给javascript函数

javascript子函数将返回值传递给父函数返回值

c - 如何从函数返回两个值?

jsp - Struts2:如何从 <jsp:include> 读取参数

performance - 忽略 T-SQL 中的 NULL 参数

c - 如何在 C 中创建一个由固定长度数组组成的表