Thread: Some help please

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    17

    Some help please

    Hey guys, i was completing a C structure program that stored the name, account number and balance of customers.

    This is what i have done so far.


    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    //Structure 
    struct CustomerStructs
        {
            int AccNum;
            char CustName;
            double CustBalance;
        };
        
    struct CustomerStructs customers [5];
    
    
    void initStructs();
    
    
    
    
    int main()
    {        
        initStructs();
        void printCusData();
        
        printCusData();
    
    
        return 0;    
    }
    
    
    void initStructs(){
        /* Prompts user for specific data*/
        
        for(int i=0; i<3; i++){
            printf("\nEnter Account Number >>");
            scanf("%d", &customers [i]. AccNum);
            
            printf("\nEnter Customer Name >>");
            scanf("%s", &customers [i].CustName);
            
            printf("\nEnter Customer's Balance >>");
    scanf("%lf", &customers[i].CustBalance);
        }
        printf ("Please Wait....\n");
    }//end initStructs
    
    
    
    
    /* Explains what is to be displayed after user inputs information */
    void printCustomerData(){
    
    
        printf("Printing Customer Information...\n\n");
        printf("Account Number\t Customer Name\t\t Customer's Balance\n");
        for(int i=0; i<3; i++){
            printf("%d \t%s \t\t %8.2lf\n",customers[i].AccNum, customers[i].CustName, customers[i].CustBalance);
        }
    
    
    }




    I need help with adding a function to add 5% interest on the customers' balances.

    I also see "ERROR id returned 1 exit status" when i try to run the program


    Thanks to anyone in advance who can help me out

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,074
    * moved to C programming forum *

    Quote Originally Posted by davic01
    I also see "ERROR id returned 1 exit status" when i try to run the program
    That's a linker/loader error that probably has something to do with the fact that you declared and called a function named printCusData, but the function that you actually defined is named printCustomerData.

    Quote Originally Posted by davic01
    I need help with adding a function to add 5% interest on the customers' balances.
    Before you do this, change your global variable customers into a local variable in main. You then pass this variable and the number of customers, or possibly a pointer to a single customer, to functions that need it.
    Last edited by laserlight; 07-14-2020 at 09:47 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2020
    Posts
    17
    i have added "int customers" under main. I am not too sure what you are asking me to do next.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,074
    What I mean is doing something like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct Customer
    {
        int AccNum;
        char CustName;
        double CustBalance;
    };
    
    void initCustomers(struct Customer customers[], size_t num);
    void printCustomerData(const struct Customer customers[], size_t num);
    
    #define NUM_CUSTOMERS 5
    
    int main(void)
    {
        struct Customer customers[NUM_CUSTOMERS];
        initCustomers(customers, NUM_CUSTOMERS);
        printCustomerData(customers, NUM_CUSTOMERS);
    
        return 0;
    }
    
    /* Prompts user for customer data to populate array of customers */
    void initCustomers(struct Customer customers[], size_t num)
    {
        for (size_t i = 0; i < num; i++)
        {
            printf("\nEnter Account Number >>");
            scanf("%d", &customers[i].AccNum);
    
            printf("\nEnter Customer Name >>");
            scanf("%s", &customers[i].CustName);
    
            printf("\nEnter Customer's Balance >>");
            scanf("%lf", &customers[i].CustBalance);
        }
        printf("Please Wait....\n");
    }
    
    /* ... */
    You should also note that using scanf with %s without a field width results in a buffer overflow vulnerability: you should specify a field width equal to the maximum length of the string... which leads us to the next problem: CustName is a char, not an array of char that could store a string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2020
    Posts
    17
    ohhhhh okaay so ive changed it to "char CustName [25]"

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,074
    Okay, then the next step would be to implement a function along the lines of:
    Code:
    void addInterest(struct Customer customers[], size_t num, int interest_rate);
    (Since you're already using a double for CustBalance, you might not be caring about floating point inaccuracy at this stage, so interest_rate could be a double instead of an int.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2020
    Posts
    17
    okay this is the part im kinda lost for...ive entered the void addInterest line as you said. Where do i set the interest at 5% now.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,074
    Quote Originally Posted by davic01
    Where do i set the interest at 5% now.
    You set the interest rate as an argument to the function. You compute the interest for each customer in the loop body, and that's also where you add the computed interest to the balance.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Tags for this Thread