'Use the values of an array from a function using scanf and pass it to the main function

I am trying to achieve this problem: EDITED

a. Create a program that will compute an electric bill.

b. Let the user provide the following data:

  • account name
  • address
  • type
  • previous reading
  • present reading
  • date of bill

-For residential, rate per kilowatt will be P15.50 while business is P25.75.

c. Create a subfunction for the following:

  • inputting of the above account details
  • computation of bill
  • display of the account information,
  • current consumed kilowatt and
  • the billing amount for the month.

d. Example Output:

Enter name: Juan Cruz

Enter address: Nasipit, Adn

Type (Residential or Business): Residential

Previous Reading: 10

Present Reading: 15

Date of Bill: March 15, 2022

Display:

Account name: Juan Cruz

Address: Nasipit, Adn

Type: Residential

Date: March 15, 2022

Previous Reading: 10

Present Reading: 15

Consumed Kilowatt: 5

Amount Billed: P77.50

Formulas: consumed_kilowatt=present_reading-previous_reading

billed_amount=consumed_kilowatt*rate_per_kilowatt

Here's the code so far.

#include <stdio.h>
#include <strings.h>

void details(char* info);
float consumed(float a, float b);
float business(float a);
float residence(float a);

void main(){
    float prev, pres;
    float billed_amount, consumed_kilowatt;
    char* info[200];
    char bus[]="Business", res[]="Residential";
    char *acc_nm, *add, *type, *prevread, *presread, *date;
    char account_info[25];
    
    details(info);
    
    prev=atof(prevread);
    pres=atof(presread);
    
    consumed_kilowatt=consumed(pres, prev);
    if(!strcmp(type,bus)){
        billed_amount=business(consumed_kilowatt);
    } else {
        billed_amount=residence(consumed_kilowatt);
    }
    
    }

void details(char* info){
    char array[][20] = { "Account Name", "Address", "Type", "Previous Reading", "Present Reading", "Date"};
    int i;
    
    printf("Please provide your data by following the format: Account_Name, Address(St_Brgy_City_State), Type(Residential or Business), Previousreading, PresentReading, Date(MM/DD/YYYY)");
    printf("\n\nNote: Please make sure to follow format. Avoid unnecessary spaces and symbols.\n\n");
    
    for (i=0; i<6; i++){
        printf("Enter %s: ", array[i]);
        scanf("%s", &info[i]);
    }

}

float consumed(float a, float b){
    float read;
    
    read=a-b;
    return read;
}

float business(float a){
    float business=25.75, val;
    
    val=a*business;
    return val;
}

float residence(float a){
    float residence=15.50, val;
    
    val=a*residence;
    return val;
}

I am trying to use the values I collected as an array in details() within the void main() so I can compute consumed_kilowatt and billed_amount formulas.

as you can see I am new to c programming. I have been reading online resources only and I really don't have any idea where should I start learning. I found this challenge and I thought of trying this out but I can't seem to get it.



Solution 1:[1]

There are inconsistencies in the parameter you are passing to details. That function is declared to accept a char *, but you are passing it an array of pointers. But the function you have written seems to expect an array of char *, rather than the char * that it is defined to take. I think you are looking for something like:

#include <stdio.h>
#include <stdlib.h>

void details(char (*info)[200]);

struct column {
    char header[20];
    char format[40];
};

struct column c[] = {
    { "Account Name", "" },
    { "Address", " (St_Brgy_City_State)" },
    { "Type", " (Residential or Business)" },
    { "Previous Reading", "" },
    { "Present Reading", "" },
    { "Date", " (MM/DD/YYYY)" }
};
int
main(void)
{
    char info[6][200];
    details(info);
    for( int i = 0; i < 6; i++ ){
        printf("%20s: %s\n", c[i].header, info[i]);
    }
}

void
details(char (*info)[200])
{
    int i;

    printf("%s", "Please provide your data by following the format: ");
    for( i = 0; i < 6; i++ ){
        printf("%s%s%s", c[i].header, c[i].format, i == 5 ? "\n" : ", ");
    }
    printf("Note: Please make sure to follow format. Avoid unnecessary spaces and symbols.\n\n");

    for( i = 0; i < 6; i++ ){
        printf("Enter %s: ", c[i].header);
        if( scanf(" %199[^\n]", info[i]) != 1 ){
            fprintf(stderr, "Invalid input\n");
            exit(1);
        }
    }
}

Note that scanf is a terrible tool. You will be much happier if you abandon its use now. Learning its foibles will not help you learn the language and will merely cause hours and hours of pointless frustration.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1