How would I resolve parameter issue?

Errors:

**main.c:2:5:** warning: type of ‘PWF’ defaults to ‘int’ [-Wimplicit-int]
 
int main(PWF)
     
----^~~~

**main.c:3:6:** error: declaration for parameter ‘getPackage’ but no such parameter
 
char getPackage(); 
      
-------^~~~~~~~~~

main.c: In function ‘calculatePkg_C’:

**main.c:80:39:** warning: implicit declaration of function ‘getPackage’ [-Wimplicit-function-declaration]
                                 
pkg = getPackage();
                                       
--------^~~~~~~~~~

main.c:130:1: error: expected declaration or statement at end of input
 
}
 
^

Code:

#include <stdio.h>
int main(PWF)
char getPackage(); 
{
        char ch;
        printf("Enter the package name(A, B, C): ");\
        fflush(stdin);
        ch = getchar();
        return ch;
}

int validPackage(char package) 
{
        if (package == 'A' || package == 'B' || package == 'C')
            return 1;
        return 0;
}

int getHours() 
{
        int hours;
        printf("Enter the value for hours(0-720): ");
        scanf("%d", &hours);
        return hours;
}

int validHours(int hours) 
{
        if (hours >= 0 && hours <= 720) 
    {
            return 1;
    }
    return 0;
}

double calculatePkg_A(int hours) 
{
        if(hours > 50) 
        {
                return 15 + (hours - 50) * 2;
        }
        return 15;
}

double calculatePkg_B(int hours) 
{
        if (hours > 100) 
        {
            return 20 + (hours - 100) * 1.5;
        }
        return 20;
}

double calculatePkg_C(int hours) 
{
        if(hours > 150) 
        {
                return 25 + (hours - 150);
        }
        return 25;


{
    
        int hours, choice;
        double amount;
        char pkg;

        while (1) 
        {
                printf("------- Main Menu -------\n");
                printf("1. Calculate Charges\n");
                printf("2. Exit\n");
                printf("Enter Choice: ");
                scanf("%d", &choice);
                switch (choice) 
            {
                    case 1:
                            // Get The Package
                            pkg = getPackage();

                            // Validate Package
                            if (validPackage(pkg)) 
                                {

                                    // Get The Hours
                                    hours = getHours();

                                    // Validate Hours
                                    if (validHours(hours)) 
                                        {

                                            // Calulate Amount
                                            if (pkg == 'A') 
                                                {
                                                    amount = calculatePkg_A(hours);
                                                } 
                                            else if (pkg == 'B') 
                                                {
                                                    amount = calculatePkg_B(hours);
                                                } 
                                            else 
                                                {
                                                    amount = calculatePkg_C(hours);
                                                }

                                            // Display Bill
                                            printf("\n------- Bill -------\n");
                                            printf("Package Selected: %c\n", pkg);
                                            printf("Hours Of Usage: %d\n", hours);
                                            printf("Amount: $%.2f\n", amount);
                                        } 
                                        else 
                                        {
                                            printf("Invalid Hours.\n");     
                                        }
                                } 
                            else 
                                {
                                    printf("Invalid Package.\n");
                                }
                            break;
                    case 2:
                            return 0;
                    default:
                            printf("Invalid Choice.\n");
                    }
                fflush(stdin);
                printf("\n");
            }
}

1 answer

  • answered 2021-05-05 08:00 chqrlie

    Q: How would I resolve parameter issue?

    A: there are multiple problems in your code:

    • do not nest function definitions
    • use a correct prototype for main: either int main(void) or int main(int argc, char *argv[])
    • define functions that do not ha parameters as int getHours(void)
    • do not use fflush(stdin), it has undefined behavior and is not necessary if you read and discard pending input after scanf().

    Here is a modified version:

    #include <stdio.h>
    
    void flushInput(void) {
        int c;
        while ((c = getchar()) != EOF && c != '\n')
            continue;
    }
    
    char getPackage(void) {
        char ch = 0;
        printf("Enter the package name(A, B, C): ");
        scanf(" %c", &ch);
        flushInput();
        return ch;
    }
    
    int validPackage(char package) {
        if (package == 'A' || package == 'B' || package == 'C')
            return 1;
        return 0;
    }
    
    int getHours(void) {
        int hours = -1;
        printf("Enter the value for hours(0-720): ");
        scanf("%d", &hours);
        flushInput();
        return hours;
    }
    
    int validHours(int hours) {
        if (hours >= 0 && hours <= 720) {
            return 1;
        }
        return 0;
    }
    
    double calculatePkg_A(int hours) {
        if (hours > 50) {
            return 15 + (hours - 50) * 2;
        }
        return 15;
    }
    
    double calculatePkg_B(int hours) {
        if (hours > 100) {
            return 20 + (hours - 100) * 1.5;
        }
        return 20;
    }
    
    double calculatePkg_C(int hours) {
        if (hours > 150) {
            return 25 + (hours - 150);
        }
        return 25;
    }
    
    int main(void) {
        int hours, choice;
        double amount;
        char pkg;
    
        while (1) {
            printf("------- Main Menu -------\n");
            printf("1. Calculate Charges\n");
            printf("2. Exit\n");
            printf("Enter Choice: ");
            if (scanf("%d", &choice) != 1)
                break;
            flushInput();
            switch (choice) {
            case 1:
                // Get The Package
                pkg = getPackage();
    
                // Validate Package
                if (validPackage(pkg)) {
    
                    // Get The Hours
                    hours = getHours();
    
                    // Validate Hours
                    if (validHours(hours)) {
    
                        // Calculate Amount
                        if (pkg == 'A') {
                            amount = calculatePkg_A(hours);
                        } else
                        if (pkg == 'B') {
                            amount = calculatePkg_B(hours);
                        } else {
                            amount = calculatePkg_C(hours);
                        }
    
                        // Display Bill
                        printf("\n------- Bill -------\n");
                        printf("Package Selected: %c\n", pkg);
                        printf("Hours Of Usage: %d\n", hours);
                        printf("Amount: $%.2f\n", amount);
                    } else {
                        printf("Invalid Hours.\n");
                    }
                } else {
                    printf("Invalid Package.\n");
                }
                break;
            case 2:
                return 0;
            default:
                printf("Invalid Choice.\n");
            }
            printf("\n");
        }
    }