Find the error in C code

Question

I got an error every time I do not know the problem with my compiler or something else:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "w6p2.h"
// Function: 1. Get user input of int type and validate for a positive number
int getIntPositive(int* i_num)
{
int integer_num = 0;
do
{
scanf("%d", &integer_num);
if (integer_num < 1)
{
printf("ERROR: Enter a positive value: ");
}

} while (integer_num < 1);

if (i_num != NULL)
{
*i_num = integer_num;
}
return(integer_num);
}
// Function: 2. Get user input of double type and validate for a positive number
double getDoublePositive(double* d_num)
{
double double_num = 0.0;
while (double_num < 1.0)
{
scanf("%lf", &double_num);
if (double_num < 1)
{
printf("ERROR: Enter a positive value: ");
}
}
if (d_num != NULL)
{
*d_num = double_num;
}
return(double_num);
}
// Function: 3. Opening Message
void openingMessage(const int msg)
{
printf("Cat Food Cost Analysis\n");
printf("======================\n\n");
printf("Enter the details for %d dry food bags of product data for analysis.\n", MAXIMUM_PRODUCT);
printf("NOTE: A 'serving' is %dg\n\n", TOTAL_GRAMS);
}
// Function: 4. Get user input for cat food product
struct CatFoodInfo getCatFoodInfo(const int i)
{
struct CatFoodInfo foodData = { 0 };
double Rate, Weight;
int Sku, Calories;
printf("Cat Food Product #%d\n", i + 1);
printf("--------------------\n");
printf("SKU : ");
foodData.sku = getIntPositive(&Sku);
printf("PRICE : $");
foodData.product_rate = getDoublePositive(&Rate);
printf("WEIGHT (LBS) : ");
foodData.weight_lbs = getDoublePositive(&Weight);
printf("CALORIES/SERV.: ");
foodData.calories = getIntPositive(&Calories);
printf("\n");
return(foodData);
}
// Function: 5. Display the formatted table header
void displayCatFoodHeader(void)
{
printf("\n");
printf("SKU $Price Bag-lbs Cal/Serv\n");
printf("------- ---------- ---------- --------\n");
}
// Function: 6. Display a formatted record of cat food data
void displayCatFoodData(const int sku, const double price, const int calories, const double lbs)
{
printf("%07d %10.2lf %10.1lf %8d\n", sku, price, lbs, calories);
}
// Function: 8. convert lbs to kg
double convertLbsKg(const double* lbs, double* kg)
{
double i;
i = *lbs / KG_IN_LBS;
if (kg != NULL)
{
*kg = i;
}
return(i);
}
// Function: 9. convert lbs to g
int convertLbsG(const double* lbs, int* g)
{
int j;
j = convertLbsKg(lbs, 0) * 1000;
if (g != NULL)
{
*g = j;
}
return(j);
}
// Function: 10. convert lbs to kg / g
void convertLbs(const double* lbs_convert, double* kg, int* g)
{
convertLbsKg(lbs_convert, kg);
convertLbsG(lbs_convert, g);
}
// Function: 11. calculate: servings based on gPerServ
double calculateServings(const int volume, const int sum, double* result)
{
double i;
i = (double)sum / (double)volume;
if (result != NULL)
{
*result = i;
}
return(i);
}
// Function 12. calculate: cost per serving
double calculateCostPerServing(const double* rate, const double* result, double* CostofResult)
{
double cost = 0;
cost = *rate / *result;
if (CostofResult != NULL)
{
*CostofResult = cost;
}
return(cost);
}
// Function 13. calculate: cost per calorie
double calculateCostPerCal(const double* price, const double* calories, double* CostCalories)
{
double cost = 0;
cost = *price / *calories;
if (CostCalories != NULL)
{
*CostCalories = cost;
}
return(cost);
}
// Function 14. Derive a reporting detail record based on the cat food product data
struct ReportData calculateReportData(const struct CatFoodInfo getDetailCatFood)
{
struct ReportData reportnfo;
double calo;
// For SKU, RATE, Weight and Calories
reportinfo.sku = getDetailCatFood.sku;
reportinfo.rate = getDetailCatFood.product_rate;
reportinfo.weightLbs = getDetailCatFood.weight_lbs;
reportinfo.calories = getDetailCatFood.calories;
// For kg and g
reportinfo.kg = convertLbsKg(&getDetailCatFood.weight_lbs, &reportinfo.kg);
reportinfo.g = convertLbsG(&getDetailCatFood.weight_lbs, &reportinfo.g);
reportinfo.sumServe = calculateServings(TOTAL_GRAMS, reportinfo.g, &reportinfo.sumServe);
reportinfo.perServe = calculateCostPerServing(&reportinfo.rate, &reportinfo.sumServe, &reportinfo.perServe);
calo = reportinfo.calories * reportinfo.sumServe;
reportinfo.calPerServe = calculateCostPerCal(&reportinfo.rate, &calo, &reportinfo.calPerServe);
return(reportinfo);
}
// Function 15. Display the formatted table header for the analysis results
void displayReportHeader(void)
{
printf("\n");
printf("Analysis Report (Note: Serving = %dg)\n", TOTAL_GRAMS);
printf("---------------\n");
printf("SKU $Price Bag-lbs Bag-kg Bag-g Cal/Serv Servings $/Serv $/Cal\n");
printf("------- ---------- ---------- ---------- --------- -------- -------- ------- -------\n");
}
// Function 16. Display the formatted data row in the analysis table
void displayReportData(const struct ReportData reportinfo, const int cheap)
{
printf("%07d %10.2lf %10.1lf %10.4lf %9d %8d %8.1lf %7.2lf %7.5lf", reportinfo.sku, reportinfo.rate, reportinfo.weightLbs, reportinfo.kg, reportinfo.g, reportinfo.calories, reportinfo.sumServe, reportinfo.perServe, reportinfo.calPerServe);
}
// Function 17. Display the Cheapest Findings
void displayFinalAnalysis(const struct CatFoodInfo cheaper_product)
{
printf("\n");
printf("Final Analysis\n--------------\n");
printf("Based on the comparison data, the PURRR-fect economical option is:\n");
printf("SKU:%07d Price: $%.2lf\n\n", cheaper_product.sku, cheaper_product.product_rate);
printf("Happy shopping!\n");
}
// 7. Logic entry point
void start(void)
{
struct CatFoodInfo catFoodDetail[MAXIMUM_PRODUCT];
int i = 0, cheapproduct = 0;
openingMessage(MAXIMUM_PRODUCT);
for (i = 0; i < MAXIMUM_PRODUCT; i++)
{
catFoodDetail[i] = getCatFoodInfo(i);
}
displayCatFoodHeader();
for (i = 0; i < MAXIMUM_PRODUCT; i++)
{
displayCatFoodData(catFoodDetail[i].sku, catFoodDetail[i].product_rate, catFoodDetail[i].calories, catFoodDetail[i].weight_lbs);
}
struct ReportData reportArray[MAXIMUM_PRODUCT];
displayReportHeader();
for (i = 0; i < MAXIMUM_PRODUCT; i++)
{
reportArray[i] = calculateReportData(catFoodDetail[i]);
if (reportArray[i].perServe < reportArray[i - 1].perServe)
{
cheapproduct = i;
}
}
int j;
for (j = 0; j < MAXIMUM_PRODUCT; j++)
{
struct ReportData x = { 0 };
x = reportArray[j];
displayReportData(x, cheapproduct);
if (cheapproduct == j)
{
printf(" *");
}
printf("\n");
}
displayFinalAnalysis(catFoodDetail[cheapproduct]);
}
w6p2.h (header file):
#include <stdio.h>
#define MAXIMUM_PRODUCT 3
#define TOTAL_GRAMS 64
#define KG_IN_LBS 2.20462
struct CatFoodInfo
{
int sku;
double product_rate;
int calories;
double weight_lbs;
};
struct ReportData
{
int sku;
double rate;
int calories;
double weightLbs;
double kg;
int g;
double sumServe;
double perServe;
double calPerServe;
};
// Function prototypes
// ===================
// Function: 1. Get user input of int type and validate for a positive number
int getIntPositive(int* i_num);
// Function: 2. Get user input of double type and validate for a positive number
double getDoublePositive(double* d_num);
// Function: 3. Opening Message
void openingMessage(const int msg);
// Function: 4. Get user input for cat food product
struct CatFoodInfo getCatFoodInfo(const int i);
// Function: 5. Display the formatted table header
void displayCatFoodHeader(void);
// Function: 6. Display a formatted record of cat food data
void displayCatFoodData(const int sku, const double* rate, const double* weight,
const int cal_serve)
// Function: 8. convert lbs to kg
double convertLbsKg(const double* lbs, double* kg);
// Function: 9. convert lbs to g
int convertLbsG(const double* lbs, int* g);
// Function: 10. convert lbs to kg / g
void convertLbs(const double* lbs_convert, double* kg, int* g);
// Function: 11. calculate: servings based on gPerServ 
double calculateServings(const int volume, const int sum, double* result);
// Function: 12. calculate: cost per serving
double calculateCostPerServing(const double* rate, const double* result, double* CostofResult);
// Function: 13. calculate: cost per calorie
double calculateCostPerCal(const double* price, const double* calories, double* CostCalories);
// Function: 14. Derive a reporting detail record based on the cat food product data
struct ReportData calculateReportData(const struct CatFoodInfo getDetailCatFood);
// Function: 15. Display the formatted table header for the analysis results
void displayReportHeader(void);
// Function: 16. Display the formatted data row in the analysis table
void displayReportData(const struct ReportData reportinfo, const int cheap);
// Function: 17. Display the Cheapest Findings
void displayFinalAnalysis(const struct CatFoodInfo cheaper_product);
// Function: 7. Logic entry point
void start(void);

The error message I got:

w6p2.c:1:2: error: expected identifier or '('

*/

^

In file included from w6p2.c:4:

./w6p2.h:56:1: error: unknown type name 'data'

data

^

w6p2.c:76:6: error: conflicting types for 'displayCatFoodData'

void displayCatFoodData(const int sku, const double price, const int calories, const double lbs)

^

./w6p2.h:39:6: note: previous declaration is here

void displayCatFoodData(const int sku, const double* rate, const double* weight,

^

w6p2.c:196:46: error: passing 'double' to parameter of incompatible type 'const double *'; take the address with &

displayCatFoodData(catFoodDetail[i].sku, catFoodDetail[i].product_rate, catFoodDetail[i].calories, catFoodDetail[i].weight_lbs);

^~~~~~~~~~~

&

./w6p2.h:39:54: note: passing argument to parameter 'rate' here

void displayCatFoodData(const int sku, const double* rate, const double* weight,

^

Summary

In the above code, there are minute errors, that are taken care of, and the desired output is obtained.
There are two structures defined here, which are CatFoodInfo and ReportData. The structure CatFoodInfo has four data members to describe the characteristics of particular cat food, and the structure ReportData has nine data members which report a particular cat food.

Explanation

The function getIntPositive() takes in a pointer and asks the user to enter a positive integer and assigns it to the pointer.
And the function getDoublePositive() takes in a double-pointer, gets a double value from the user, which is positive, and assigns it to that pointer.

The function getCatFoodInfo() get the info about a particular CatFoodInfo structure variable from user.
And the function displayCatFoodData() displays the details of a CatFoodInfo structure variable.

The function displayCatFoodHeader() displays a table header.
Similar functions are present for ReportData also, which are displayReportData and displayReportDataHeader.

The function convertLbs converts Lbs to Kg and also Lbs to gm, by calling two other functions convertLbsKg and convertLbsG.

The function calculateServings is defined to find the serving of the data.

Code

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdio.h>
#define MAXIMUM_PRODUCT 3
#define TOTAL_GRAMS 64
#define KG_IN_LBS 2.20462
struct CatFoodInfo
{
int sku;
double product_rate;
int calories;
double weight_lbs;
};
struct ReportData
{
int sku;
double rate;
int calories;
double weightLbs;
double kg;
int g;
double sumServe;
double perServe;
double calPerServe;
};
// Function prototypes
// ===================
// Function: 1. Get user input of int type and validate for a positive number
int getIntPositive(int* i_num);
// Function: 2. Get user input of double type and validate for a positive number
double getDoublePositive(double* d_num);
// Function: 3. Opening Message
void openingMessage(const int msg);
// Function: 4. Get user input for cat food product
struct CatFoodInfo getCatFoodInfo(const int i);
// Function: 5. Display the formatted table header
void displayCatFoodHeader(void);
// Function: 6. Display a formatted record of cat food data
void displayCatFoodData(const int sku, const double rate, const int cal_serve, const double weight);
// Function: 8. convert lbs to kg
double convertLbsKg(const double* lbs, double* kg);
// Function: 9. convert lbs to g
int convertLbsG(const double* lbs, int* g);
// Function: 10. convert lbs to kg / g
void convertLbs(const double* lbs_convert, double* kg, int* g);
// Function: 11. calculate: servings based on gPerServ
double calculateServings(const int volume, const int sum, double* result);
// Function: 12. calculate: cost per serving
double calculateCostPerServing(const double* rate, const double* result, double*
CostofResult);
// Function: 13. calculate: cost per calorie
double calculateCostPerCal(const double* price, const double* calories, double*
CostCalories);
// Function: 14. Derive a reporting detail record based on the cat food product data
struct ReportData calculateReportData(const struct CatFoodInfo getDetailCatFood);
// Function: 15. Display the formatted table header for the analysis results
void displayReportHeader(void);
// Function: 16. Display the formatted data row in the analysis table
void displayReportData(const struct ReportData reportinfo, const int cheap);
// Function: 17. Display the Cheapest Findings
void displayFinalAnalysis(const struct CatFoodInfo cheaper_product);
// Function: 7. Logic entry point
void start(void);
// Function: 1. Get user input of int type and validate for a positive number
int getIntPositive(int* i_num)
{
int integer_num = 0;
do
{
scanf("%d", &integer_num);
if (integer_num < 1)
{
printf("ERROR: Enter a positive value: ");
}
} while (integer_num < 1);
if (i_num != NULL)
{
*i_num = integer_num;
}
return(integer_num);
}
// Function: 2. Get user input of double type and validate for a positive number
double getDoublePositive(double* d_num)
{
double double_num = 0.0;
while (double_num < 1.0)
{
scanf("%lf", &double_num);
if (double_num < 1)
{
printf("ERROR: Enter a positive value: ");
}
}
if (d_num != NULL)
{
*d_num = double_num;
}
return(double_num);
}
// Function: 3. Opening Message
void openingMessage(const int msg)
{
printf("Cat Food Cost Analysis\n");
printf("======================\n\n");
printf("Enter the details for %d dry food bags of product data for analysis.\n", MAXIMUM_PRODUCT);
printf("NOTE: A 'serving' is %dg\n\n", TOTAL_GRAMS);
}
// Function: 4. Get user input for cat food product
struct CatFoodInfo getCatFoodInfo(const int i)
{
struct CatFoodInfo foodData = { 0 };
double Rate, Weight;
int Sku, Calories;
printf("Cat Food Product #%d\n", i + 1);
printf("--------------------\n");
printf("SKU : ");
foodData.sku = getIntPositive(&Sku);
printf("PRICE : $");
foodData.product_rate = getDoublePositive(&Rate);
printf("WEIGHT (LBS) : ");
foodData.weight_lbs = getDoublePositive(&Weight);
printf("CALORIES/SERV.: ");
foodData.calories = getIntPositive(&Calories);
printf("\n");
return(foodData);
}
// Function: 5. Display the formatted table header
void displayCatFoodHeader(void)
{
printf("\n");
printf("SKU $Price Bag-lbs Cal/Serv\n");
printf("------- ---------- ---------- --------\n");
}
// Function: 6. Display a formatted record of cat food data
void displayCatFoodData(const int sku, const double price, const int calories, const double lbs)
{
printf("%07d %10.2lf %10.1lf %8d\n", sku, price, lbs, calories);
}
// Function: 8. convert lbs to kg
double convertLbsKg(const double* lbs, double* kg)
{
double i;
i = *lbs / KG_IN_LBS;
if (kg != NULL)
{
*kg = i;
}
return(i);
}
// Function: 9. convert lbs to g
int convertLbsG(const double* lbs, int* g)
{
int j;
j = convertLbsKg(lbs, 0) * 1000;
if (g != NULL)
{
*g = j;
}
return(j);
}
// Function: 10. convert lbs to kg / g
void convertLbs(const double* lbs_convert, double* kg, int* g)
{
convertLbsKg(lbs_convert, kg);
convertLbsG(lbs_convert, g);
}
// Function: 11. calculate: servings based on gPerServ
double calculateServings(const int volume, const int sum, double* result)
{
double i;
i = (double)sum / (double)volume;
if (result != NULL)
{
*result = i;
}
return(i);
}
// Function 12. calculate: cost per serving
double calculateCostPerServing(const double* rate, const double* result, double* CostofResult)
{
double cost = 0;
cost = *rate / *result;
if (CostofResult != NULL)
{
*CostofResult = cost;
}
return(cost);
}
// Function 13. calculate: cost per calorie
double calculateCostPerCal(const double* price, const double* calories, double* CostCalories)
{
double cost = 0;
cost = *price / *calories;
if (CostCalories != NULL)
{
*CostCalories = cost;
}
return(cost);
}
// Function 14. Derive a reporting detail record based on the cat food product data
struct ReportData calculateReportData(const struct CatFoodInfo getDetailCatFood)
{
struct ReportData reportinfo;
double calo;
// For SKU, RATE, Weight and Calories
reportinfo.sku = getDetailCatFood.sku;
reportinfo.rate = getDetailCatFood.product_rate;
reportinfo.weightLbs = getDetailCatFood.weight_lbs;
reportinfo.calories = getDetailCatFood.calories;
// For kg and g
reportinfo.kg = convertLbsKg(&getDetailCatFood.weight_lbs, &reportinfo.kg);
reportinfo.g = convertLbsG(&getDetailCatFood.weight_lbs, &reportinfo.g);
reportinfo.sumServe = calculateServings(TOTAL_GRAMS, reportinfo.g, &reportinfo.sumServe);
reportinfo.perServe = calculateCostPerServing(&reportinfo.rate, &reportinfo.sumServe, &reportinfo.perServe);
calo = reportinfo.calories * reportinfo.sumServe;
reportinfo.calPerServe = calculateCostPerCal(&reportinfo.rate, &calo, &reportinfo.calPerServe);
return(reportinfo);
}
// Function 15. Display the formatted table header for the analysis results
void displayReportHeader(void)
{
printf("\n");
printf("Analysis Report (Note: Serving = %dg)\n", TOTAL_GRAMS);
printf("---------------\n");
printf("SKU $Price Bag-lbs Bag-kg Bag-g Cal/Serv Servings $/Serv $/Cal\n");
printf("------- ---------- ---------- ---------- --------- -------- -------- ------- -------\n");
}
// Function 16. Display the formatted data row in the analysis table
void displayReportData(const struct ReportData reportinfo, const int cheap)
{
printf("%07d %10.2lf %10.1lf %10.4lf %9d %8d %8.1lf %7.2lf %7.5lf", reportinfo.sku, reportinfo.rate, reportinfo.weightLbs, reportinfo.kg, reportinfo.g, 
reportinfo.calories, reportinfo.sumServe, reportinfo.perServe, reportinfo.calPerServe);
}
// Function 17. Display the Cheapest Findings
void displayFinalAnalysis(const struct CatFoodInfo cheaper_product)
{
printf("\n");
printf("Final Analysis\n--------------\n");
printf("Based on the comparison data, the PURRR-fect economical option is:\n");
printf("SKU:%07d Price: $%.2lf\n\n", cheaper_product.sku, cheaper_product.product_rate);
printf("Happy shopping!\n");
}
// 7. Logic entry point
int main()
{
struct CatFoodInfo catFoodDetail[MAXIMUM_PRODUCT];
int i = 0, cheapproduct = 0;
openingMessage(MAXIMUM_PRODUCT);
for (i = 0; i < MAXIMUM_PRODUCT; i++)
{
catFoodDetail[i] = getCatFoodInfo(i);
}
displayCatFoodHeader();
for (i = 0; i < MAXIMUM_PRODUCT; i++)
{
displayCatFoodData(catFoodDetail[i].sku, catFoodDetail[i].product_rate, catFoodDetail[i].calories, 
catFoodDetail[i].weight_lbs);
}
struct ReportData reportArray[MAXIMUM_PRODUCT];
displayReportHeader();
for (i = 0; i < MAXIMUM_PRODUCT; i++)
{
reportArray[i] = calculateReportData(catFoodDetail[i]);
if (reportArray[i].perServe < reportArray[i - 1].perServe)
{
cheapproduct = i;
}
}
int j;
for (j = 0; j < MAXIMUM_PRODUCT; j++)
{
struct ReportData x = { 0 };
x = reportArray[j];
displayReportData(x, cheapproduct);
if (cheapproduct == j)
{
printf(" *");
}
printf("\n");
}
displayFinalAnalysis(catFoodDetail[cheapproduct]);
}

Output

Find the error in C code output

 

Also read, Break and Continue in C

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *