Thread: Process fails after typing anything in - help with errors appreciated

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    5

    Process fails after typing anything in - help with errors appreciated

    Code:
    
    
    
    // ------------------------------------------------------------------------------------------
    // Includes
    // ------------------------------------------------------------------------------------------
    #include<stdio.h>
    #include<stdlib.h>
    #include <string.h>
    // ------------------------------------------------------------------------------------------
    // Constants
    // ------------------------------------------------------------------------------------------
    
    
    // ------------------------------------------------------------------------------------------
    // Prototypes
    // ------------------------------------------------------------------------------------------
    // Problem #1 void Problem1( );
    void ProcessUser();
    void GetNumberYearsEmployed(int* pintYearsEmployed);
    void GetAmountPreviousPurchase(double* psngAmountPreviousPurchase);
    void GetEmployeeStatus(char* pcharEmployeeStatus);
    void GetTodaysTotalPurchase(double* psngTodaysTotalPurchase);
    void GetAnotherEmployee(char AnotherEmployee[]);
    void CalculateDiscountPercentage(double* pdblDiscount, char* pcharEmployeeStatus, int* pintYearsEmployed);
    double TotalDiscountAllowed(double currentDiscount, double previousDiscount);
    double CalculateTotalWithDiscount(double* psngTodaysTotalPurchase, double applicableDiscount);
    double CalculatePreviousDiscount(double *pCurrentDiscountPercentage, double *psngAmountPreviousPurchase);
    // ------------------------------------------------------------------------------------------
    // Name: main
    // Abstract: This is where the program starts
    // ------------------------------------------------------------------------------------------
    int main()
    {
    
    
        ProcessUser();
    
    
        //pause to hold the screen to allow user to see the output
        system("pause");
        return 0;
    
    
    }
    
    
    
    
    //call values
    void ProcessUser()
    {
    
    
        double totalGrossPurchase = 0;
        double totalNetPurchase = 0;
    
    
        double *pCurrentDiscountPercentage = 0;
    
    
        int* pintYearsEmployed = 0;
        double *psngAmountPreviousPurchase = 0;
        char *pcharEmployeeStatus = 0;
        double *psngTodaysTotalPurchase = 0;
        int intCount = 0;
    
    
        //store user response to process another employee 
        char AnotherEmployee[10];
    
    
        do
        {
    
    
            GetNumberYearsEmployed(pintYearsEmployed);
            GetAmountPreviousPurchase(psngAmountPreviousPurchase);
            GetEmployeeStatus(pcharEmployeeStatus);
            GetTodaysTotalPurchase(psngTodaysTotalPurchase);
    
    
    
    
            //calculate YTD
            double sngYTDAmount = *psngTodaysTotalPurchase + *psngAmountPreviousPurchase;
    
    
            //Get Count
            intCount = intCount + 1;
    
    
            //get discount applicable for employee
            CalculateDiscountPercentage(pCurrentDiscountPercentage, pcharEmployeeStatus, pintYearsEmployed);
    
    
            double currentDiscount = (*pCurrentDiscountPercentage) * (*psngTodaysTotalPurchase);
    
    
            double previousDiscounts = CalculatePreviousDiscount(pCurrentDiscountPercentage, psngAmountPreviousPurchase);
    
    
            double applicableDiscount = TotalDiscountAllowed(currentDiscount, previousDiscounts);
    
    
            double netPurchase = CalculateTotalWithDiscount(psngTodaysTotalPurchase, applicableDiscount);
    
    
            //add sold amount total gross purchase
            totalGrossPurchase += *psngTodaysTotalPurchase;
    
    
            //add net purchase for current purchase to total purchase
            totalNetPurchase += netPurchase;
    
    
            printf("Employee %d Summary\n", intCount);
            printf("The YTD amount in dollars is %lf\n", sngYTDAmount);
            printf("The Total purchase today before discount is %lf\n", *psngTodaysTotalPurchase);
            printf("The employee discount this purchase is %lf\n", applicableDiscount);
            printf("The Total with discount is %lf\n", netPurchase);
            printf("\n");
    
    
            GetAnotherEmployee(AnotherEmployee);
    
    
        } while (AnotherEmployee != 'no');
    
    
        //Display all employee total
        printf("All Employee Summary\n");
        printf("The total Gross is %lf\n", totalGrossPurchase);
        printf("The Total Net is %lf\n", totalNetPurchase);
        printf("\n");
    
    
        printf("\n");
    
    
    
    
    
    
    }
    
    
    double CalculatePreviousDiscount(double *pCurrentDiscountPercentage, double *psngAmountPreviousPurchase)
    {
        return (*pCurrentDiscountPercentage) * (*psngAmountPreviousPurchase);
    }
    
    
    
    
    //GET AND VALIDATE INPUTS
    
    
    //Get user response to process another employee
    void GetAnotherEmployee(char AnotherEmployee[])
    {
    
    
        do
        {
            printf("Another employee? (yes or no):");
            scanf_s("%s", AnotherEmployee);
    
    
        } while (AnotherEmployee != 'no' || AnotherEmployee != 'yes');
    
    
    
    
    }
    
    
    
    
    //Get Number of years employed
    void GetNumberYearsEmployed(int* pintYearsEmployed)
    {
        do
        {
            printf("Enter a number of years employed: ");
            scanf_s(" %d", pintYearsEmployed);
    
    
        } while (*pintYearsEmployed < 1);
    }
    
    
    //Get amount of previous purchase amount
    void GetAmountPreviousPurchase(double* psngAmountPreviousPurchase)
    {
        do
        {
            printf("Enter total amount of previous purchases before discount: ");
            scanf_s(" %lf", psngAmountPreviousPurchase);
        } while (*psngAmountPreviousPurchase <= 0);
    }
    
    
    //Get employee status
    void GetEmployeeStatus(char* pcharEmployeeStatus)
    {
        do
        {
            printf("Enter status (e for employee/m for manager): ");
            scanf_s(" %c", pcharEmployeeStatus);
        } while (*pcharEmployeeStatus != 'e' && *pcharEmployeeStatus != 'm');
    
    
    }
    
    
    //Get todays total purchase amount
    void GetTodaysTotalPurchase(double* psngTodaysTotalPurchase)
    {
        do
        {
            printf("Enter today's total purchase: ");
            scanf_s(" %lf", psngTodaysTotalPurchase);
        } while (*psngTodaysTotalPurchase <= 0);
    }
    
    
    
    
    
    
    //Calculate discount percentage
    
    
    void CalculateDiscountPercentage(double* pdblDiscount, char* pcharEmployeeStatus, int* pintYearsEmployed)
    {
    
    
        if (*pcharEmployeeStatus == 'm')
        {
            if (*pintYearsEmployed >= 1 && *pintYearsEmployed <= 3)
            {
                *pdblDiscount = 0.20;
            }
            else if (*pintYearsEmployed >= 4 && *pintYearsEmployed <= 6)
            {
                *pdblDiscount = 0.24;
            }
            else if (*pintYearsEmployed >= 7 && *pintYearsEmployed <= 10)
            {
                *pdblDiscount = 0.30;
            }
            else if (*pintYearsEmployed >= 11 && *pintYearsEmployed <= 15)
            {
                *pdblDiscount = 0.35;
            }
            else
            {
                *pdblDiscount = 0.40;
            }
        }
        else if (*pcharEmployeeStatus == 'e')
        {
            if (*pintYearsEmployed >= 1 && *pintYearsEmployed <= 3)
            {
                *pdblDiscount = 0.10;
            }
            else if (*pintYearsEmployed >= 4 && *pintYearsEmployed <= 6)
            {
                *pdblDiscount = 0.14;
            }
            else if (*pintYearsEmployed >= 7 && *pintYearsEmployed <= 10)
            {
                *pdblDiscount = 0.20;
            }
            else if (*pintYearsEmployed >= 11 && *pintYearsEmployed <= 15)
            {
                *pdblDiscount = 0.25;
            }
            else
            {
                *pdblDiscount = 0.30;
            }
        }
    }
    
    
    
    
    //Caculate total discount allowed
    double TotalDiscountAllowed(double currentDiscount, double previousDiscount)
    {
    
    
        if (previousDiscount >= 200)
        {
            return 0;
        }
        else if (currentDiscount + previousDiscount <= 200)
        {
            return currentDiscount;
        }
        else
        {
            //partial discount
            return (200 - previousDiscount);
    
    
        }
    
    
    }
    
    
    //calculate total with discount
    double CalculateTotalWithDiscount(double* psngTodaysTotalPurchase, double applicableDiscount)
    {
        double netPurchase = *psngTodaysTotalPurchase - applicableDiscount;
        return netPurchase;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Looks like you didn't learn the lesson from last time.

    Just do yourself a favour and stop digging these huge holes for yourself.
    Code:
    $ gcc -Wall main.c
    main.c: In function ‘ProcessUser’:
    main.c:128:33: warning: multi-character character constant [-Wmultichar]
         } while (AnotherEmployee != 'no');
                                     ^
    main.c:128:30: warning: comparison between pointer and integer
         } while (AnotherEmployee != 'no');
                                  ^
    main.c: In function ‘GetAnotherEmployee’:
    main.c:167:9: warning: implicit declaration of function ‘scanf_s’ [-Wimplicit-function-declaration]
             scanf_s("%s", AnotherEmployee);
             ^
    main.c:170:33: warning: multi-character character constant [-Wmultichar]
         } while (AnotherEmployee != 'no' || AnotherEmployee != 'yes');
                                     ^
    main.c:170:30: warning: comparison between pointer and integer
         } while (AnotherEmployee != 'no' || AnotherEmployee != 'yes');
                                  ^
    main.c:170:60: warning: multi-character character constant [-Wmultichar]
         } while (AnotherEmployee != 'no' || AnotherEmployee != 'yes');
                                                                ^
    main.c:170:57: warning: comparison between pointer and integer
         } while (AnotherEmployee != 'no' || AnotherEmployee != 'yes');
                                                             ^
    You should have figured out that 'no' was a bad thing to try the first time you tried it, not the 10th time you tried it.


    Oh, and stop guessing how scanf_s is supposed to work.
    scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l | Microsoft Docs
    It is NOT just a matter of putting _s on the end of the function and hoping magic happens.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    You've failed to understand how to use pointers to return values.
    You need to declare the variable, then take its address, and pass that to the function expecting a pointer. If you pass a null pointer you will get a crash when the program tries to write to it because it is trying to write a value to memory location zero, which is protected by a hardware trap.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Jun 2020
    Posts
    5
    Quote Originally Posted by Malcolm McLean View Post
    You've failed to understand how to use pointers to return values.
    You need to declare the variable, then take its address, and pass that to the function expecting a pointer. If you pass a null pointer you will get a crash when the program tries to write to it because it is trying to write a value to memory location zero, which is protected by a hardware trap.
    Yeah, I figured that out and rectified that issue. Thank you.

    Quote Originally Posted by Salem View Post
    Looks like you didn't learn the lesson from last time.

    Just do yourself a favour and stop digging these huge holes for yourself.
    Code:
    $ gcc -Wall main.c
    main.c: In function ‘ProcessUser’:
    main.c:128:33: warning: multi-character character constant [-Wmultichar]
         } while (AnotherEmployee != 'no');
                                     ^
    main.c:128:30: warning: comparison between pointer and integer
         } while (AnotherEmployee != 'no');
                                  ^
    main.c: In function ‘GetAnotherEmployee’:
    main.c:167:9: warning: implicit declaration of function ‘scanf_s’ [-Wimplicit-function-declaration]
             scanf_s("%s", AnotherEmployee);
             ^
    main.c:170:33: warning: multi-character character constant [-Wmultichar]
         } while (AnotherEmployee != 'no' || AnotherEmployee != 'yes');
                                     ^
    main.c:170:30: warning: comparison between pointer and integer
         } while (AnotherEmployee != 'no' || AnotherEmployee != 'yes');
                                  ^
    main.c:170:60: warning: multi-character character constant [-Wmultichar]
         } while (AnotherEmployee != 'no' || AnotherEmployee != 'yes');
                                                                ^
    main.c:170:57: warning: comparison between pointer and integer
         } while (AnotherEmployee != 'no' || AnotherEmployee != 'yes');
                                                             ^
    You should have figured out that 'no' was a bad thing to try the first time you tried it, not the 10th time you tried it.


    Oh, and stop guessing how scanf_s is supposed to work.
    scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l | Microsoft Docs
    It is NOT just a matter of putting _s on the end of the function and hoping magic happens.
    I appreciate the link to the information, thank you for your help. I ended up using fgets, strlen, and strcmp for the no/yes loop; which was the only remaining issue in the code. I do appreciate the link though, it helped steer me in the right direction. Sorry for wasting your time.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Code:
    double *pCurrentDiscountPercentage = 0;
    int* pintYearsEmployed = 0;
    double *psngAmountPreviousPurchase = 0;
    char *pcharEmployeeStatus = 0;
    double *psngTodaysTotalPurchase = 0;
    Where are you allocating the memory for these pointers?

    No need to make them pointers. Make them simple variables, and then pass the address of the variables into the functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Autocorrecting typing errors
    By jonathan123456 in forum C Programming
    Replies: 3
    Last Post: 11-13-2017, 02:06 PM
  2. sscanf using '%p' fails to process.
    By MrUmunhum in forum C Programming
    Replies: 1
    Last Post: 01-29-2017, 08:35 PM
  3. Synax errors with my else statements, help appreciated.
    By heyitsmeehd in forum C Programming
    Replies: 6
    Last Post: 02-07-2012, 03:12 PM
  4. Checking for child process errors?
    By Blasz in forum C Programming
    Replies: 11
    Last Post: 05-19-2010, 07:41 AM
  5. Linker Errors, Help Appreciated
    By burntheart in forum Windows Programming
    Replies: 3
    Last Post: 03-27-2006, 07:13 AM

Tags for this Thread