Beginner in C: Can't properly printf first and last element in double array? - arrays

I'm trying to display a Wish List on the command line. The user enters the cost of the item, the level of priority (1-3), and whether or not it has financing options (y/n). The inputted values are put in multiple arrays. After the user enters all their values, they're displayed in a table at the end.
Everything in my code works fine except when I try to printf the user inputed cost values (double itemCosts[numOfItems];) in the table. The first and last element don't print properly, even when I input the same price (6225.88) for all of them. The last element is just 0.000000. See included pic of table output.
I tried debugging the issue by separating the cost-related loops and compiling/running them as another file and the costs display correctly...so I'm thinking the bug is somewhere else, but I can't find it.
#define MAX_ITEMS 10
#include <stdio.h>
int main()
{
const double MIN_INCOME = 500, MAX_INCOME = 400000;
int numOfItems;
double netIncome, itemTotal;
double itemCosts[numOfItems];
int itemPriors[numOfItems];
char itemFinOps[numOfItems];
printf("+--------------------------+\n");
printf("+ Wish List Forecaster |\n");
printf("+--------------------------+\n\n");
// Prompt for net monthly income
do
{
printf("Enter your monthly NET income: $");
scanf("%lf", &netIncome);
if (netIncome < MIN_INCOME)
{
printf("ERROR: You must have a consistent monthly income of at least $500.00\n\n");
}
else if (netIncome > MAX_INCOME)
{
printf("ERROR: Liar! I'll believe you if you enter a value no more than $400000.00\n\n");
}
} while (!(netIncome >= MIN_INCOME && netIncome <= MAX_INCOME));
printf("\n");
// Prompt for # of wish list items
do
{
printf("How many wish list items do you want to forecast?: ");
scanf("%d", &numOfItems);
if (!(numOfItems > 0 && numOfItems <= MAX_ITEMS))
{
printf("ERROR: List is restricted to between 1 and 10 items.\n\n");
}
printf("\n");
} while (!(numOfItems > 0 && numOfItems <= MAX_ITEMS));
// Store wish list item details
for (int i = 0; i < numOfItems; i++)
{
printf("Item-%d Details:\n", i + 1);
do //////////////// ******** PROMPT COST ********** //////////
{
printf("Item cost: $");
scanf("%lf", &itemCosts[i]);
if (!(itemCosts[i] >= (double)100))
{
printf(" ERROR: Cost must be at least $100.00\n");
}
} while (!(itemCosts[i] >= (double)100));
do // prompt priority
{
printf("How important is it to you? [1=must have, 2=important, 3=want]: ");
scanf("%d", &itemPriors[i]);
if (!(itemPriors[i] >= 1 && itemPriors[i] <= 3))
{
printf(" ERROR: Value must be between 1 and 3\n");
}
} while (!(itemPriors[i] >= 1 && itemPriors[i] <= 3));
do // prompt finance options
{
printf("Does this item have financing options? [y/n]: ");
scanf(" %c", &itemFinOps[i]);
if (!(itemFinOps[i] == 'y' || itemFinOps[i] == 'n'))
{
printf(" ERROR: Must be a lowercase 'y' or 'n'\n");
}
} while (!(itemFinOps[i] == 'y' || itemFinOps[i] == 'n'));
printf("\n");
}
///////// display summary of item details in TABLE //////////
printf("Item Priority Financed Cost\n");
printf("---- -------- -------- -----------\n");
for (int j = 0; j < numOfItems; j++)
{
printf(" %d %d %c %lf\n", j + 1, itemPriors[j], itemFinOps[j], itemCosts[j]);
itemTotal += itemCosts[j];
}
return 0;
}

int numOfItems;
double netIncome, itemTotal;
double itemCosts[numOfItems];
int itemPriors[numOfItems];
char itemFinOps[numOfItems];
It is undefined behaviour as your numOfItems is not inilized. In C table will not grow or shrink to the size when you change this variable
Change to:
double itemCosts[MAX_ITEMS];
int itemPriors[MAX_ITEM];
char itemFinOps[MAX_ITEM];
Always check the result of scanf. Exmaple:
if(scanf("%lf", &netIncome) != 1){ /* handle error*/}

As Vlad from Moscow and 0___________ said, you cannot define an array using a not initialised variable for the number of its elements. You can define the variables as
double *itemCosts;
int *itemPriors;
char *itemFinOps;
and after the variable numOfItems is given a value, you dynamically allocate memory as follows:
itemCosts = (double*) malloc(numOfItems * sizeof(double));
itemPriors = (int*) malloc(numOfItems * sizeof(int));
itemFinOps = (char*) malloc(numOfItems * sizeof(char));
or
itemCosts = (double*) calloc(numOfItems, sizeof(double));
itemPriors = (int*) calloc(numOfItems, sizeof(int));
itemFinOps = (char*) calloc(numOfItems, sizeof(char));
calloc initialises all elements of array to 0
At the end you must free the allocated memory:
free(itemCosts);
free(itemPriors);
free(itemFinOps);

Related

I want to display inputs of for loop

I have created a user input.
I want to display the inputs I have taken from the for loop.
i dont know how to arrays tho (edited)
This is all I have. My code explains more:
Item Priority Financed Cost
---- -------- -------- -----------
1 1 n 39030.15
2 3 y 1200000.00
3 2 n 350500.25
---- -------- -------- -----------
$ 1589530.40
something like this
here is my code
#define _CRT_SECURE_NO_WARNINGS
#define minINCOME 500.00
#define maxINCOME 400000.00
#include <stdio.h>
int main(void)
{
float userIncome ;
int wishlistItems;
float itemCost;
const float minitemCost = 100.00;
int itemImp;
char finance;
printf("+--------------------------+\n"
"+ Wish List Forecaster |\n"
"+--------------------------+\n\n");
do
{
printf("Enter your monthly NET income: $");
scanf(" %f", &userIncome);
if (userIncome < minINCOME)
{
printf("ERROR: You must have a consistent monthly income of at least $500.00\n\n");
}
else if (userIncome > maxINCOME)
{
printf("ERROR: Liar! I'll believe you if you enter a value no more than $400000.00\n\n");
}
} while( (userIncome > maxINCOME || userIncome < minINCOME));
do
{
printf("How many wish list items do you want to forecast?: ");
scanf(" %d", &wishlistItems);
if (wishlistItems > 10 || wishlistItems < 1)
{
printf("ERROR: List is restricted to between 1 and 10 items.\n\n");
}
} while (wishlistItems > 10 || wishlistItems < 1);
for (size_t i = 1; i <= wishlistItems; i++ )
{
do {
printf("Item-%d Details:\n", i);
printf(" Item cost: $");
scanf(" %f", &itemCost);
if (itemCost < minitemCost)
{
printf(" ERROR: Cost must be at least $100.00\n");
}
} while (itemCost < minitemCost);
do {
printf(" How important is it to you? [1=must have, 2=important, 3=want]: ");
scanf(" %d", &itemImp);
if (itemImp < 1 || itemImp >3 )
{
printf(" ERROR: Value must be between 1 and 3\n");
}
} while (itemImp < 1 || itemImp >3);
do {
printf(" Does this item have financing options? [y/n]: ");
scanf(" %c", &finance);
if (finance != 'y' || finance != 'n')
{
printf(" ERROR: Must be a lowercase 'y' or 'n'\n");
}
} while (finance != 'y' || finance != 'n');
}
return 0;
}
Given that wishlistItems is limited to 10, there shouldn't be any issue using an array. Even if the user doesn't fill it up fully, 10 items is not that much memory to be concerned about.
On the other hand, given that theoretically you want to build a scalable program, and wishlistItems can change, I would consider a linked list here.
It will require pointers and dynamic memory allocation (as pointed by Steve), but allows you to maintain all the data in memory, while allocating the exact memory size required here.
Each item on the list will look approximately like this:
struct wish_list_item {
float cost;
int impact;
char finance;
struct wish_list_item *next;
};
Where next points to the next item on the list, or is NULL if it is the last item.
Then I would suggest to implement a couple of helper functions to work with the list more comfortably (add_element, del_element, print_list, find_element, insert_element), all according to your needs.
Should be easy to find example implementations, given that Linked list is the second most-used data structure after array.

Finding Mean in C program

I need some advice or hints about how to calculate the average of insurance, tax, gross and net salary. Just need some enlightment.
Tried to add average function before but it seems that its hard to get the value from other function.
Any ideas? This code is used to find the salary of employee and its average.
char EmpName[50];
int EmpID, EmpAge, EmpNum, i;
float gross_salary, net_salary;
float insurance, tax, total, total_insurance, total_tax;
int main()
{
int i;
FILE *file;
file = fopen("UserDetails.txt", "wt");
printf("Number of Employee to process: ");
scanf("%d", &EmpNum);
i = 1;
EmployeeName: while( i <= EmpNum)
{
printf("\nEmployee name: "); scanf("%s", &EmpName);
if (strlen(EmpName) <= 50){
EmployeeID: printf("Employee ID: "); scanf("%d", &EmpID);
if (EmpID >= 1000 && EmpID <= 9999){
EmployeeAge: printf("Employee Age: "); scanf("%d", &EmpAge);
if (EmpAge >= 18 && EmpAge <= 99){
printf("Employee Salary: "); scanf("%g", &gross_salary);
if (gross_salary >= 0.0 && gross_salary <= 9999.99){
goto Total;
}
}
else{
printf("\nInvalid Input!\nEmployee Age Is Between 18 To 99\n");
goto EmployeeAge;
}
}
else{
printf("\nInvalid Input!\nEmployee ID Number is Between 1000 To 9999\n");
goto EmployeeID;
}
}
else{
printf("\nInvalid Input!\nMax Character is 50\n");
goto EmployeeName;
}
Total: printf("\n");
SetInsurance(EmpAge);
IncomeTax(gross_salary);
TaxDeduction(tax);
InsuranceDeduction(insurance);
NetSalary();
Average(insurance,gross_salary,net_salary);
i++;
fprintf(file, "Employee name: %s\n", EmpName); //print data inside file
fprintf(file, "Employee ID: %d\n", EmpID);
fprintf(file, "Employee Age: %d\n", EmpAge);
fprintf(file, "Employee Salary: %g\n\n", gross_salary);
}
fclose(file);
}
float SetInsurance(int x){
while (i <= EmpNum){
if (x <= 35)
insurance = 110;
else if (x >=36 && x <= 65)
insurance = 160;
else if (x > 65)
insurance = 250;
else
printf("Under Age!");
total = insurance;
printf("\nInsurance: %.2f\n", total);
return total;i++;
};
}
float IncomeTax(float salary) {
int i = 0;
while(i < EmpNum){
if (salary <= 999.99)
tax = 0;
else if (salary >= 1000 && salary <= 2999.99)
tax = 2.5;
else if (salary >= 3000)
tax = 5;
else
printf("Invalid Input!\n");
printf("Income Tax Rate: %.2f%\n", tax);
return tax;
i++;
}
}
float InsuranceDeduction(float insurance){
if (insurance == 110){
total_insurance =+ 110;
}else if (insurance == 160){
total_insurance =+ 160;
}else if (insurance == 250){
total_insurance =+ 250;
}
return total_insurance;
}
float TaxDeduction(float tax){
if(tax == 2.5){
total_tax = gross_salary * 0.025;
}else if(tax == 5){
total_tax = gross_salary * 0.05;
}
return total_tax;
}
void NetSalary(){
total = TaxDeduction(tax) + InsuranceDeduction(insurance);
net_salary = gross_salary - total;
printf("Net Salary: RM%.2f\n", net_salary);
}
I order to solve the problem you face, I'd follow the recomendations you have received from other responses, and in addition:
If you fopen() a file, just check that the returned pointer is valid, as you can have several errors derived of:
Not having permissions to read the file.
file doesn't exist.
and these should be handled before you start processing the file.
Second, You don't need to store the data from the file, except for error purposes (it's preferable if you say that Martina Navratilova cannot be paying such ridiculous amount of taxes than saying that record 6437 in the file has a ridiculous amount of taxes to be payd -- you should search for it by counting, while your program has already done it)
If I would to decide how the input file is formatted, I should make a two line record, one line to specify the user name (which so, can have spaces embedded, as the new line marks the end of the name) so it can be read with fgets(), and a second line with all the numeric data you are going to fscanf(), after the sequence is done, you have all the data for a single user. Another possibility (to allow the name with spaces) is to put the name as the last field of the data file, so you can read all the last field characters upto the \n as the user name. This would allow to put each user in one single line
"I need some advice or hints., ...,any idea?"
First: Read and act on each of suggestions in first 3 comments under your post from #Chux.
Other suggestions:
Avoid using goto tag ... :tag statements to traverse code.
Create functions that can be called to do a simple task, then return.
Use a struct to contain related data when there can be more that one related data set.
Change
char EmpName[50];
int EmpID, EmpAge, EmpNum, i;
float gross_salary, net_salary;
float insurance, tax, total, total_insurance, total_tax;
To for example: (Note, not all these members may be necessary, depending on how you finally implement the records needed for each employee.)
typedef struct {
char EmpName[50];
int EmpID;
int EmpAge;
double gross_salary;
double net_salary;
double insurance;
double total_insurance; //may not be necessary in struct
double tax;
double total_tax; //may not be necessary in struct
}account_s;
Note, if "total" members are to be used as totals for all employees, then they should probably not be a members in the struct.
Create instance(s) of this struct in local space, Eg.
int main(void)
{
int EmpNum = 0;
...
printf("Enter number of Employees to process: ");
scanf("%d", &EmpNum);
account_s account[EmpNum];//creates an array of instances of account_s
memset(account, 0, sizeof account);//initializes array
Incorporate account_s into functions as return type, and/or function parameters. Eg.
The following is a very brief, but compilable and runable example to illustrate several things including how to pass a single function argument containing all the information needed to update and contain employee information (via struct).
//prototypes
void populateAccounts(int count, account_s record[count]);
void SetInsurance(account_s *record);
double Get_ave_salery(int c, account_s *account);
int main(void)
{
int EmpNum = 0;
printf("Number of Employee to process: ");
scanf("%d", &EmpNum);
account_s account[EmpNum];//creates an array of instances of account_s
memset(account, 0, sizeof account);//initializes array
populateAccounts(EmpNum, account);
double ave_salary = Get_ave_salery(EmpNum, account);
printf("Average Gross Salary: %lf\n", ave_salary);
return 0;
}
void populateAccounts(int count, account_s record[count])
{
//Read in information for each account
for(int i=0; i<count; i++)
{
printf("\nEnter employee name: ");
scanf("%s", record[i].EmpName);
printf("\nEnter employee ID: ");
scanf("%d", &record[i].EmpID);
printf("\nEnter employee Age: ");
scanf("%d", &record[i].EmpAge);
printf("\nEnter employee Salary: ");
scanf("%lf", &record[i].gross_salary);
SetInsurance(&record[i]);//function to set insurance via function argument
// etc.
}
}
void SetInsurance(account_s * record){
if (record->EmpAge <= 35)
record->insurance = 110;
else if (record->EmpAge >=36 && record->EmpAge <= 65)
record->insurance = 160;
else if (record->EmpAge > 65)
record->insurance = 250;
else
printf("Under Age!");
// total = record->insurance;
printf("\nInsurance: %.2f\n", record->insurance);
}
double Get_ave_salery(int c, account_s *account)
{
double ave = 0.0;
double sum = 0.0;
for(int i=0; i<c; i++)
{
sum += account[i].gross_salary;
}
return sum/c;
}
There is more for you to do here, but this should help you to get started.

segmentation fault (core dumped) gcc ubuntu

I was trying to make a simple function to make a group of number that user enters them, using pointer of pointer but i keep getting this error and its hard to tell where the problem is, if there any other option to use something else that tells me where the problem is instead of this weird error.
#include <stdio.h>
void BuildGroub(int** group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count);
while(*count != 0){
printf("Enter the %d number of the group:\n", i);
j=0;
scanf("%d", &**(group+i));
while(**(group+i)!=**(group+j)){
j++;
}
if(j==i){
i++;
count--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = &group;
BuildGroub(&groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
With this question, you do not need to use double pointer. If you want to learn how to use the double pointer, you can google then there are a ton of examples for you, for example, Double Pointer (Pointer to Pointer) in C.
In BuildGroub you decrease the count pointer
if(j==i){
i++;
count--;
}
, but in the condition of while loop, you compare the value that count pointer points to. it seems strange.
while(*count != 0)
Even if you change count-- to (*count)--, it will decrease the number of elements that you enter to 0 when you get out of the while loop, then in main function:
for(int i=0;i<count;i++){} // count is equal to 0 after calling BuildGroub function if you use (*count--) in while loop.
You should use a temp value for while loop function, for example:
int size = *count;
while(size != 0){
...
if (i == j) {
i++;
size--;
}
}
You should use, for example, group[i] instead of *(group+i). It will be easier to read your code.
The code complete:
#include <stdio.h>
void BuildGroub(int* group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", count);
int size = *count;
while(size != 0){
printf("Enter the %d_th number of the group:\n", i);
j=0;
scanf("%d", &group[i]);
while(group[i] != group[j]) {
j++;
}
if(j==i){
i++;
size--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = group;
BuildGroub(groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
The test:
./test
Enter the size of the group
5
Enter the 0_th number of the group:
1
Enter the 1_th number of the group:
2
Enter the 2_th number of the group:
2
you have already entered this number please try again:
Enter the 2_th number of the group:
3
Enter the 3_th number of the group:
3
you have already entered this number please try again:
Enter the 3_th number of the group:
4
Enter the 4_th number of the group:
5
1, 2, 3, 4, 5,
If you want to use a double pointer, you need to change your function like this:
void BuildGroub(int** group, int* count) {
int i = 0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count); //I think this is redundant but works.
while (*count != 0) {
printf("Enter the %d number of the group:\n", i);
j = 0;
scanf("%d", (*group + i)); //The content of group + i
while ( *( *group + i) != *(*group + j)) { //the content of the content
j++;
}
if (j == i) {
i++;
(*count)--; //The content decrement
} else {
printf("you have already entered this number please try again: \n");
}
}
}
But you have a big problem in main and it is because you are using the parameter count to decrement until zero inside the function. So when the function finish, count value is zero and you don't print anything... You need to change this, using a internal variable to make the count, and finaly, setting the parameter to be using in main.

My program compiles and when ran, it doesn't give me the input i put in

The purpose of my program thus far is to create 4 arrays. 1 'char' array which will take in 10 different string values with each element having a maximum of 25 characters. And 3 other array's that will take in 10 integer values and store them into my array. I compile and run my program, and cycle through my for loops and once completed, I get some weird integer values in the place of my teamName array, and for the 'teamWins' 'teamLosses' and 'teamTies' arrays, it gives me the first value I input for ALL elements in those arrays. I really want to understand how arrays work but I am having trouble declaring them, and using them with input, and output. Can anyone see and explain how I can take in 10 strings with values of 25 characters in each element, and take in 10 integers in the other 3 arrays with elements of 10? I will attach my source code below.
#include <stdio.h>
#include <stdlib.h>
#define NUM_TEAM 10
void displayWelcome(void);
int main(void)
{
char * teamName[NUM_TEAM + 1][30] = { "" };
int teamWins[NUM_TEAM] = {0};
int teamLosses[NUM_TEAM] = {0};
int teamTies[NUM_TEAM] = {0};
int i, bestPercent, worstPercent;
displayWelcome();
//Team Name
for (i = 0; i < NUM_TEAM; i++)
{
//Prompt and enter team name
printf("Enter %i's team name: ", i + 1);
fgets (teamName[NUM_TEAM], sizeof teamName[NUM_TEAM], stdin);
//Data validation
}
//Team wins
for (i = 0; i < NUM_TEAM; i++)
{
printf("Enter wins for team number %i : ", i + 1);
scanf("%i", &teamWins[ i ]);
/*Data validation
while ( 1 != scanf("%i", & teamWins) || teamWins <= 0)
{
fflush(stdin);
printf("Enter a numerical value greater than zero: ");
}*/
}
//Team losses
for (i = 0; i < NUM_TEAM; i++)
{
printf("Enter losses for team number %i : ", i + 1);
scanf("%i", &teamLosses[ i ]);
/*Data validation
while ( 1 != scanf("%i", & teamLosses) || teamLosses <= 0)
{
fflush(stdin);
printf("Enter a numerical value greater than zero: ");
}*/
}
//Team ties
for (i = 0; i < NUM_TEAM; i++)
{
printf("Enter ties for team number %i : ", i + 1);
scanf("%i", &teamTies[ i ]);
/*Data validation
while ( 1 != scanf("%i", & teamTies) || teamTies <= 0)
{
fflush(stdin);
printf("Enter a numerical value greater than zero: ");
}*/
}
//Display Data
for (i = 0; i < NUM_TEAM; i++)/* output each word read */
{
printf("%s", teamName);
printf("wins losses ties\n");
printf("%i %i %i\n", teamName[i], teamWins[i], teamLosses[i], teamTies[i]);
}
return 0;
}
void displayWelcome(void)
{
printf("Welcome to my Football Stats\n\n");
}
You've got four issues:
Declaring teamName as char * teamName[NUM_TEAM + 1][30] = { "" }; is incorrect; if you want an array of strings, it's sufficient to declare char teamName[NUM_TEAM + 1][30] = { "" }; (you want a 2D array of chars, not char *s).
In fgets (teamName[NUM_TEAM], sizeof teamName[NUM_TEAM], stdin);, you're writing each team name to the same unused element. Instead, use fgets (teamName[i], sizeof teamName[NUM_TEAM], stdin); to write to the proper team during each iteration.
Printing using printf("%s", teamName); is incorrect; you want to print each team name rather than attempt to print the address of the teamName array: printf("%s", teamName[i]);
You have an extra argument in printf("%i %i %i\n", teamName[i], teamWins[i], teamLosses[i], teamTies[i]);; since you've already printed teamName[i] in Point 3, you should remove it in this printf() call: printf("%i %i %i\n", teamWins[i], teamLosses[i], teamTies[i]);

my program fails when I build and succeeds when I rebuild?

Whenever I build my program using visual studio 2015, it says it fails, but when I rebuild immediately after, it says it succeeded. Another question is how do i store multiple inputs for SKU, price and price and then properly output it.
#include <stdio.h>
#define MAX_ITEMS 10
struct Item
{
int sku_[10];
int quantity_[10];
float price_[10];
};
int main(void)
{
int size = 0;
int input =1;
int i;
int j;
struct Item items[10];
printf("Welcome to the Shop\n");
printf("===================\n");
printf("Please select from the following options\n");
while (size <= MAX_ITEMS && input != 0)
{
printf("1) Display the inventory.\n2) Add to the inventory.\n0)Exit.\n);
printf("Select:");
scanf_s("%d", &input);
while (input < 0 || input >2 && input != 0)
{
printf("Invalid input, try again: Please select from the following);
printf("1)Display the inventory.\n2)Add to the inventory.\n0) Exit.\n");
printf("Select:");
scanf_s("%d", &input);
}
if (input == 1)
{
printf("Inventory\n");
printf("====================\n");
printf("Sku Price Quantity\n");
printf("%d", items[size].sku_);
}
else if (input == 2)
{
printf("Please input a SKU number:");
if (size >= MAX_ITEMS)
{
printf("The inventory is full");
}
else if (size < MAX_ITEMS)
{
scanf_s("%d", &items[size].sku_);
printf("Quantity:");
scanf("%d", &items[size].quantity_);
printf("Price:");
scanf("%f", &items[size].price_);
printf("The item is successfully added to the inventory.\n");
size += 1;
}
}
else if (input == 0)
{
printf("Good bye");
}
}
}
Here are errors detected in the source code:
1- As WhozCraig suggests, two printf() calls are bad terminated.
Instead of:
printf("1) Display the inventory.\n2) Add to the inventory.\n0)Exit.\n);
...
printf("Invalid input, try again: Please select from the following);
Add a text terminator:
printf("1) Display the inventory.\n2) Add to the inventory.\n0)Exit.\n");
...
printf("Invalid input, try again: Please select from the following");
2- When entering items[size].sku_, or .quantity_, or .price_, use a pointer to a value instead of a pointer to a array of value. The struct Item is malformed.
Just modify the struct Item:
struct Item
{
int sku_; // unexpected use [10];
int quantity_; // unexpected use [10];
float price_; // unexpected use [10];
};
3- When printing the inventory, use a loop and not the last index. And format all attributes of each items[i] to align with the header.
printf("Sku Price Quantity\n");
for(i=0;i<size;i++) {
printf("%6d %8d %6.2f\n", items[i].sku_,items[i].quantity_,items[i].price_);
}

Resources