How to delete a value in a struct? - c

I am trying to delete a "user" from the struct on my code.
For example, this is the struct I have:
struct clients {
char name[30];
int year;
float amount;
};
And this is the two outputs I have:
Name: Victor
Birth Year: 1998
Amount: 1000.00
Name: John
Birth Year: 1996
Amount: 1500.00
What I want to do is to delete John and all his information from my struct.
I am trying to learn dynamic memmory allocation, so I used this command:
clt = (struct clients*) malloc(n * sizeof(struct clients));
In my case I wish to write a command on case 3 that delete john from the struct, but I really can't figure out how to do that, I guess maybe using realloc? But I didn't even get close to write something it would work.
Here is my entire code so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct clients {
char name[30];
int year;
float amount;
};
int main() {
struct clients *clt;
int option, n, d = 0;
char name[30];
printf("Welcome, Choose an option to begin:");
do {
printf("\n1 - Create a new list;\n");
printf("2 - Find a person in the list;\n");
printf("3 - Delete a person from the list.\n");
printf("4 - End the program.\n");
scanf("%d", &option);
switch (option) {
case 1:
printf("\nType how many clients you want to register:");
scanf("%d", &n);
// allocating memory for n numbers of struct person
clt = (struct clients*) malloc(n * sizeof(struct clients));
for (int i = 0; i < n; i++) {
printf("Type the name and the birth year:");
// To access members of 1st struct person,
scanf("%s %d", (clt + i)->name, &(clt + i)->year);
printf("\nType the amount:");
scanf("%f", &(clt + i)->amount);
}
break;
case 2:
printf("Type the name you want to find:");
scanf("%s", name);
for (int i = 0; i < n; i++) {
if (strcmp(&clt[i].name, name) == 0) {
printf("\nThats the name:");
printf("\nName: %s\n", (clt + i)->name);
printf("Birth Year: %d\n", (clt + i)->year);
printf("Amount: %.2f\n", (clt + i)->amount);
d++;
}
}
if (d == 0)
printf("This name doesn't exist\n");
break;
case 3:
break;
case 4:
break;
}
} while (option != 4);
return 0;
}

You need to discard the element from its location in the list by moving the contents in the remainder of the list up , and overwriting the target element.
for(int i = 0; i < n; i++) {
if (strcmp(&clt[i].name, name)==0)
{
printf("\nThats the name:");
printf("\nName: %s\n", (clt+i)->name);
printf("Birth Year: %d\n", (clt+i)->year);
printf("Amount: %.2f\n", (clt+i)->amount);
d++; // This seems to be your sentinel value?
}
if ( 1 == d && i+1 != n ) // if the sentinel is set, but we're not off the end of the list yet
{
memcpy(clt+i, clt+i+1, sizeof( struct clients ));
}
}
n--; // reduce the list length
You could do this in one memmove call if you prefer. As #Neil says in the comments, memmove is tolerant of overlapping source and destination.
if ( 1 == d && i != n-1 ) // if the sentinel is set, but we're not off the end of the list yet
{
memmove(clt+i, clt+i+1, (n-i-1) * sizeof( struct clients ));
break; // break for i
}

You are describing almost a dynamic array such as C++'s std::vector. It is static, but it could be a dynamic array without that much extra work. You could create a container, and split n into the container's capacity and size.
struct clients_array {
size_t size, capacity;
struct clients *data;
};
Instead of having multiple pointers, one could create a struct clients_array for each list. Sample code to deal with the struct clients_array.
#include <inttypes.h>
#include <assert.h>
#include <errno.h>
/** `a` constructor with `n` spots. Returns true or false depending on whether
it could be constructed. */
static int clients_array_reserve(struct clients_array *const a,
const size_t n) {
assert(a && n);
if(n > SIZE_MAX / sizeof *a->data) return errno = ERANGE, 0;
if(!(a->data = malloc(sizeof *a->data * n))) return 0;
a->capacity = n;
a->size = 0;
return 1;
}
/** `a` destructor; call this when `a` has been successfully constructed. */
static void clients_array_(struct clients_array *const a)
{ if(a) { free(a->data); a->data = 0; a->capacity = a->size = 0; } }
/** Extract a new uninitialized entry out of constructed `a`. Returns null if
there is no more space. */
static struct clients *clients_array_new(struct clients_array *const a) {
assert(a);
return a->capacity > a->size ? a->data + a->size++ : 0;
}
/** Remove index `rm` from `a`. */
static void clients_array_delete(struct clients_array *const a,
const size_t rm) {
assert(a && rm < a->size);
memmove(a->data + rm, a->data + rm + 1,
sizeof *a->data * (a->size - rm - 1));
a->size--;
}
Then one can have different, independent arrays at once. If you want to update the code, say to allow realloc to increase the size of the object, you only need to modify the container.

Related

Issues with printing structs in C

I'm incredibly new to this and have a school assignment I have to write a gradebook program that uses a custom struct to hold student IDs and grades. I have been trying unsuccessfully for days to figure out why it will not print properly, or when it does print (after a lot of shifting things around) it only prints the second set of input.
The gradebook.h section is the custom structure.
// gradebook.h
struct gradeID
{
int id;
char grades[25];
};
// Gradebook.h is a header file to define
// a global structure.
#include "gradebook.h"
#include <stdio.h>
#include <stdlib.h>
void sort(struct gradeID *, int);
int main(void)
{
// Variables and structure definitions
int ctr;
char contInput;
int i;
struct gradeID grade;
struct gradeID *identifier;
int *temps;
// Allocates 10 integers worth of memory for the program to use.
// If there is not enough memory, the program will print an error
// statement and terminate the program.
temps = (int *) malloc(10 * sizeof(int));
if (temps == 0)
{
printf("Not enough memory!\n");
exit(1);
}
// Prints basic instructions for the program
printf("\t\tGradebook Recorder\n");
printf("Input student IDs and grades.\n");
printf("These will be sorted by ID and printed.\n");
/* Creates a for loop that will continue until the program
hits the designated number of array elements. For the sake
of expediency, I have set this amount to 10, but it can be
changed as necessary.*/
for(i = 0; i < 10; i++)
{
printf("Input student ID:\n");
scanf(" %d", &grade.id);
printf("Input grade:\n");
scanf(" %s", grade.grades);
// This allows early exit of the program
printf("Do you have more grades to enter?\n");
printf("Y/N\n");
scanf(" %c", &contInput);
if(contInput == 'N' || contInput == 'n')
{
printf("Finalizing and printing input-\n\n");
break;
}
ctr++;
}
printf("Grades Sorted by Student ID:\n\n");
printf("\tStudent ID: Student Grade: \n");
for(i = 0; i < ctr; i++)
{
printf("\t%d", grade.id );
printf("\t%s", grade.grades);
}
identifier[i] = grade;
return(0);
free(temps);
}
void sort(struct gradeID identifier[], int counter)
{
int inner;
int outer;
struct gradeID temp;
// Loops for sorting
for(outer = 0; outer < counter - 1; ++outer)
{
for(inner = outer + 1; inner < counter; ++inner)
{
if(identifier[inner].id < identifier[outer].id)
{
temp = identifier[inner];
identifier[inner] = identifier[outer];
identifier[outer] = temp;
}
}
}
return;
}
The pointer identifier is uninitialized
struct gradeID *identifier;
So this statement
identifier[i] = grade;
independent on the value of i invokes undefined behavior.
In this for loop
for(i = 0; i < 10; i++)
{
printf("Input student ID:\n");
scanf(" %d", &grade.id);
printf("Input grade:\n");
scanf(" %s", grade.grades);
// This allows early exit of the program
printf("Do you have more grades to enter?\n");
printf("Y/N\n");
scanf(" %c", &contInput);
if(contInput == 'N' || contInput == 'n')
{
printf("Finalizing and printing input-\n\n");
break;
}
ctr++;
}
you are entering new data in the same object grade of the structure type. So the new data overrides the previous data stored in the object.
Moreover the variable ctr was not initialized
int ctr;
So this statement in the above for loop
ctr++;
also invokes undefined behavior.
The variable temps that points to a dynamically allocated array
temps = (int *) malloc(10 * sizeof(int));
is not used.
This statement
free(temps);
never gets the control because before it there is the return statement
return(0);
free(temps);
What you need is to define an array of the type struct gradeID as for example
struct gradeID grades[10];
and fill it with values.

Realloc to structure in c

i have a problem with my realloc in the func add order its crash every time
someone can help me here
typedef struct
{
char carname[30]; //car names
int price; //the price of the car
}Vehicle;
typedef struct {
int number; // מספר הזמנה
char *name; // שם המזמין
char ID[10]; // מספר זהות שלו
int number_vehicles; // מספר הרכבים בהזמנה
Vehicle * list;
int sum_order;
}Order;
Order * add_orders(Order * firma, int * ordersize)// adding one more order
{
int i, j;
char temp[80];
int newstart; // because we need to add one more order we need new start and it will be the old size of the number of orders
newstart = *ordersize; //we just did it
(*ordersize)++; // now we have new size +1 for the one more order
for (i = newstart; i < *ordersize; i++) //same as the input function but this one start and finish from the new start and finish
{
printf("enter the ID of order number: %d\n", i + 1);
scanf("%d", &firma[i].number);
while (getchar() != '\n');
printf("enter the name of the client:\n");
gets(temp);
firma[i].name = (char*)malloc(strlen(temp) + 1 * sizeof(char));
if (!firma[i].name)
{
printf("error\n");
return 0;
}
strcpy(firma[i].name, temp);
printf("enter the ID of client (length can only be 10):\n");
gets(firma[i].ID);
printf("enter how many vehicles you want in this order\n");
scanf("%d", &firma[i].number_vehicles);
firma[i].list = (Vehicle*)malloc(firma[i].number_vehicles * sizeof(Vehicle));
if (!firma[i].list)
{
printf("error\n");
return 0;
}
for (j = 0; j < firma[i].number_vehicles; j++)
{
while (getchar() != '\n');
printf("enter the name of the vehicle number %d (only 30 chars):\n", j + 1);
gets(firma[i].list[j].carname);
printf("enter the price of the vehicle\n");
scanf("%d", &firma[i].list[j].price);
}
}
firma = (Order*)realloc(firma, (*ordersize * sizeof(Order))); //after we added the new order we do realloce for the new size for the struct
if (!firma)//if failed
{
printf("error\n");
return 0;
}
}
int main()
{
int i, j;
char temp[80];
char *str = NULL;
int num, x, y;
int number;
int ordersize;
int exit1 = 0;
int exit2 = 0;
int exit3 = 0;
int flag = 0;
int choose;
Order *data = NULL; //pointer to the struct Order
printf("\nenter how many orders you want\n");
scanf("%d", &ordersize); //enter size of orders
data = (Order*)malloc(ordersize * sizeof(Order));//we allocte memory for the struct Order with pointer data
add_orders(data, &ordersize); //send it
return 0;
}
i have a problem with my realloc in the func add order its crash every time
someone can help me here
i have a problem with my realloc in the func add order its crash every time
someone can help me here
i have a problem with my realloc in the func add order its crash every time
someone can help me here
You access out of bound for firma.
data = (Order*)malloc(ordersize * sizeof(Order));//we allocte memory for the struct Order with pointer data
add_orders(data, &ordersize); //send it
As you allocated memory of size orderSize and you access firma[newstart].
newstart = *ordersize; //we just did it
(*ordersize)++; // now we have new size +1 for the one more order
for (i = newstart; i < *ordersize; i++) //same as the input function but this one start and finish from the new start and finish
{
printf("enter the ID of order number: %d\n", i + 1);
scanf("%d", &firma[i].number);
while (getchar() != '\n');
.....
}
Thus first allocate more memory and access.
newstart = *ordersize; //we just did it
(*ordersize)++; // now we have new size +1 for the one more order
firma = (Order*)realloc(firma, (*ordersize * sizeof(Order))); //after we added the new order we do realloce for the new size for the struct
if (!firma)//if failed
{
printf("error\n");
return 0;
}
for (i = newstart; i < *ordersize; i++) //same as the input function but this one start and finish from the new start and finish
{
printf("enter the ID of order number: %d\n", i + 1);
scanf("%d", &firma[i].number);
while (getchar() != '\n');
.....
}
Note:: firma is local variable and allocating memory using realloc
in addOrder function has no effect on data variable in main
function. You may need to pass data using reference or return
updated value from function and assign it to data.

Proper use of structures and pointers

I have to declare a vector with the "struct" type which, for every n students, it creates a value for the group that student belongs to (which is like a counter), their names and their grades.
The program has to output the name of the students with the highest grade found in these groups. I have to allocate the vector on the heap (I only know the theoretical explanation for heap, but I have no idea how to apply it) and I have to go through the vector using pointers.
For example if I give n the value 4, there will be 4 students and the program will output the maximum grade together with their names as shown here.
This will output Ana 10 and Eva 10.
I gave it a try, but I have no idea how to expand it or fix it so I appreciate all the help I can get with explanations if possible on the practical application of heap and pointers in this type of problem.
#include <stdio.h>
#include <stdlib.h>
struct students {
int group;
char name[20];
int grade;
};
int main()
{
int v[100], n, i;
scanf("%d", n);
for (i = 0; i < n; i++) {
v[i].group = i;
scanf("%s", v[i].name);
scanf("%d", v[i].grade);
}
for (i = 0; i < n; i++) {
printf("%d", v[i].group);
printf("%s", v[i].name);
printf("%d", v[i].grade);
}
return 0;
}
Here I was just trying to create the vector, nothing works though..
It appears, int v[100]; is not quite what you want. Remove that.
You can follow either of two ways.
Use a VLA. After scanning the value of n from user, define the array like struct students v[n]; and carry on.
Define a fixed size array, like struct students v[100];, and use the size to limit the loop conditions.
That said,
scanf("%d", n); should be scanf("%d", &n);, as %d expects a pointer to integer type argument for scanf(). Same goes for other cases, too.
scanf("%s", v[i].name); should better be scanf("%19s", v[i].name); to avoid the possibility of buffer overflow by overly-long inputs.
Even though you are asking for the number of students (groups) using scanf, you hardcoded the upper bound of this value using v[100]. So, I passed your input variable n (the number of students) to malloc in order to allocate the space you need for creating an array of n students.
Also, I used qsort to sort the input array v where the last element would be the max value. Here qsort accepts an array of structs and deference the pointers passed to the comp function to calculate the difference of the comparison.
Finally, I printed the sorted array of structs in the last loop.
#include <stdio.h>
#include <stdlib.h>
struct students {
int group;
char name[20];
int grade;
};
int comp(const void *a, const void *b)
{
return ((((struct students *)a)->grade > ((struct students *)b)->grade) -
(((struct students *)a)->grade < ((struct students *)b)->grade));
}
int main()
{
int n;
printf("Enter number of groups: ");
scanf("%d", &n);
printf("\n");
struct students *v = malloc(n * sizeof(struct students));
int i;
for(i = 0; i < n; i++)
{
v[i].group = i;
printf("\nName: ");
scanf("%s", v[i].name);
printf("Grade: ");
scanf("%d", &v[i].grade);
}
qsort(v, n, sizeof(*v), comp);
for(i = 0; i < n; i++)
{
printf("Group %d, Name %s, grade %d\n", v[i].group, v[i].name, v[i].grade);
}
return (0);
}
You need to replace the standalone array v[100], with an array of structs referencing your structure:
struct students v[100];
However, if you want to use malloc to allocate memory on the heap, you will need to do something like this:
struct students *students = malloc(n * sizeof(struct students));
/* Check void* return pointer from malloc(), just to be safe */
if (students == NULL) {
/* Exit program */
}
and free the requested memory from malloc() at the end, like this:
free(students);
students = NULL;
Additionally, adding to #Sourav Ghosh's answer, it is also good to check the return value of scanf(), especially when dealing with integers.
Instead of simply:
scanf("%d", &n);
A more safe way is this:
if (scanf("%d", &n) != 1) {
/* Exit program */
}
With all this said, your program could look something like this:
#include <stdio.h>
#include <stdlib.h>
#define NAMESTRLEN 20
typedef struct { /* you can use typedef to avoid writing 'struct student' everywhere */
int group;
char name[NAMESTRLEN+1];
int grade;
} student_t;
int
main(void) {
int n, i;
printf("Enter number of students: ");
if (scanf("%d", &n) != 1) {
printf("Invalid input.\n");
exit(EXIT_FAILURE);
}
student_t *students = malloc(n * sizeof(*students));
if (!students) {
printf("Cannot allocate memory for %d structs.\n", n);
exit(EXIT_FAILURE);
}
for (i = 0; i < n; i++) {
students[i].group = i;
printf("Enter student name: ");
scanf("%20s", students[i].name);
printf("Enter students grade: ");
if (scanf("%d", &(students[i].grade)) != 1) {
printf("Invalid grade entered.\n");
exit(EXIT_FAILURE);
}
}
printf("\nStudent Information:\n");
for (i = 0; i < n; i++) {
printf("Group: %d Name: %s Grade: %d\n",
students[i].group,
students[i].name,
students[i].grade);
}
free(students);
students = NULL;
return 0;
}

C - Displaying the parts in ascending order by part number

I'm new here, so I have no idea if I posted this correctly. I did everything that the instructor told us to do for this program, but this last one has me stumped because we never talked about sorting in class. It says, "Modify the print() function so that it displays the parts sorted in ascending order by part number." I tried looking through the book and on the internet, but I just caused myself to become even more confused. Can anyone help me? Here's my code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define NAME_LEN 25
#define MAX_PARTS 100
struct part {
int number;
char name[NAME_LEN + 1];
int on_hand;
float price;
};
int find_part(int number, const struct part inv[], int np);
void insert(struct part inv[], int *np);
void search(const struct part inv[], int np);
void update(struct part inv[], int np);
void print(const struct part inv[], int np);
int read_line(char [], int);
/**************************************************************
* main: Prompts the user to enter an operation code, *
* then calls a function to perform the requested *
* action. Repeats until the user enters the *
* command 'q'. Prints an error message if the user *
* enters an illegal code. *
**************************************************************/
int main(void)
{
char code;
struct part inventory[MAX_PARTS];
int num_parts = 0;
for (;;)
{
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != '\n') /* skips to end of line */
{
;
}
switch (code)
{
case 'i':
insert(inventory, &num_parts);
break;
case 's':
search(inventory, num_parts);
break;
case 'u':
update(inventory, num_parts);
break;
case 'p':
print(inventory, num_parts);
break;
case 'q':
return 0;
default:
printf("Illegal code\n");
break;
}
printf("\n");
}
}
/************************************************************
* find_part: Looks up a part number in the inv array. *
* Returns the array index if the part number *
* is found; otherwise, returns -1. *
************************************************************/
int find_part(int number, const struct part inv[], int np)
{
int i;
for (i = 0; i < np; i++)
{
if (inv[i].number == number)
{
return i;
}
}
return -1;
}
/****************************************************************
* insert: Prompts the user for information about a new *
* part and then inserts the part into the inv *
* array. Prints an error message and returns *
* prematurely if the part already exists or the *
* array is full. *
****************************************************************/
void insert(struct part inv[], int *np)
{
int part_number;
if (*np == MAX_PARTS)
{
printf("Database is full; can't add more parts.\n");
return;
}
printf("Enter part number: ");
scanf("%d", &part_number);
if (find_part(part_number, inv, *np) >= 0)
{
printf("Part already exists.\n");
return;
}
inv[*np].number = part_number;
printf("Enter part name: ");
read_line(inv[*np].name, NAME_LEN);
printf("Enter quantity on hand: ");
scanf("%d", &inv[*np].on_hand);
printf("Enter the price of the item: ");
scanf("%f", &inv[*np].price);
(*np)++;
}
/************************************************************
* search: Prompts the user to enter a part number, then *
* looks up the part in the inv array. If the *
* part exists, prints the name and quantity on *
* hand; if not, prints an error message. *
************************************************************/
void search(const struct part inv[], int np)
{
int i, number;
printf("Enter part number: ");
scanf("%d", &number);
i = find_part(number, inv, np);
if (i >= 0)
{
printf("Part name: %s\n", inv[i].name);
printf("Quantity on hand: %d\n", inv[i].on_hand);
printf("Item Price: %.2f\n", inv[i].price);
}
else
{
printf("Part not found.\n");
}
}
/*************************************************************
* update: Prompts the user to enter a part number. *
* Prints an error message if the part can't be *
* found in the inv array; otherwise, prompts the *
* user to enter change in quantity on hand and *
* updates the array. *
*************************************************************/
void update(struct part inv[], int np)
{
int i, number, change, userChoice, changePartNum;
float changePrice;
char *changeName[] = {""};
printf("Enter part number: ");
scanf("%d", &number);
i = find_part(number, inv, np);
if (i >= 0)
{
printf("Enter your selection to edit this particular part:\n"
"\t\t Type 1 to change the Part Number\n"
"\t\t Type 2 to change the Part Name\n"
"\t\t Type 3 to change the Price\n"
"\t\t Type 4 to change the Quantity on Hand\n"
"\t\t Type 5 to exit without making changes\n\n"
"\t\t Enter your choice here: ");
scanf("%d", &userChoice);
switch ( userChoice )
{
//printf("Would you like to change the Part Number? \nType 1 for yes or 2 for no.");
//scanf("%d", &userChoice);
case 1:
printf("Enter new part number: ");
scanf("%d", &changePartNum);
inv[i].number = changePartNum;
printf("Change part num: %d\n", changePartNum);
printf("inv[i].number: %d\n", inv[i].number);
break;
//printf("Would you like to change the Part Name? \nType 1 for yes or 2 for no.");
//scanf("%d", &userChoice);
case 2:
printf("Enter new name of part: ");
scanf("%s", changeName);
printf("Change part name: %s\n", changeName);
//strcpy (*changeName, inv[i].name[NAME_LEN + 1]);
//printf("&inv[i].name[NAME_LEN + 1]: %d\n", &inv[i].name[NAME_LEN + 1]);
break;
//printf("Would you like to change the price? \nType 1 for yes or 2 for no.");
//scanf("%d", &userChoice);
case 3:
printf("Enter change in item price: ");
scanf("%f", &changePrice);
inv[i].price = changePrice;
break;
//printf("Would you like to change the quantity on hand? \nType 1 for yes or 2 for no.");
//scanf("%d", &userChoice);
case 4:
printf("Enter change in quantity on hand: ");
scanf("%d", &change);
inv[i].on_hand = change;
break;
case 5:
printf("Exiting the editor.");
break;
default:
printf("Your choice is not on the list.");
break;
}
}
else
{
printf("Part not found.\n");
}
}
/************************************************************
* print: Prints a listing of all parts in the inv array, *
* showing the part number, part name, and *
* quantity on hand. Parts are printed in the *
* order in which they were entered into the *
* array. * *
************************************************************/
void print(const struct part inv[], int np)
{
int i;
printf("Part Number Part Name "
"Quantity on Hand "
" Price\n");
for (i = 0; i < np; i++)
{
printf("%7d\t\t %-5s%31d\t%.2f\n", inv[i].number,
inv[i].name, inv[i].on_hand, inv[i].price);
}
}
/*************************************************************
* read_line: Skips leading white-space characters, then *
* reads the remainder of the input line and *
* stores it in str. Truncates the line if its *
* length exceeds n. Returns the number of *
* characters stored. *
*************************************************************/
int read_line(char str[], int n)
{
int ch = 0;
int i = 0;
while (isspace (ch = getchar()))
{
;
}
while (ch != '\n' && ch != EOF)
{
if (i < n)
{
str[i++] = ch;
}
ch = getchar();
}
str[i] = '\0';
return i;
}
If your professor allows you to use a function from the standard libary, check out C library function to do sort, which provides an answer that shows you how to use qsort(). If you're on linux, you can also get documentation on the function (and other standard library functions) by doing man qsort. If you're not on linux, check out any of the various online man pages.
If not, your professor probably expects you to do your own research. Generally the bubble sort algorithm is taught to beginners because of its simplicity. rosettacode provides pseudo-code on what a bubble sort should look like:
repeat
hasChanged := false
decrement itemCount
repeat with index from 1 to itemCount
if (item at index) > (item at (index + 1))
swap (item at index) with (item at (index + 1))
hasChanged := true
until hasChanged = false
Note that in your print function, you already have all you need, the array and its length. You demonstrated this by looping through it and printing out all the member variables. Now you just need to write the algorithm for the sort. One thing you need for the algorithm is the comparator function. You stated that you need to sort by part number. That means your comparator function will probably look like this:
int compare(struct part a, struct part b)
{
return (a.number < b.number);
}
For qsort():
qsort(inv, np, sizeof(part), compare);
In your sorting algorithm (that you should write yourself):
if (item.number at index) > (item.number at (index + 1))

C Program - adding, removing, and printing dominos from an array of dominos

Here are the program specs:
This assignment will be to add and remove dominos from an array of dominos. Your structure will be a domino. It will hold 3 fields (used, number1 and number2).
The used field will either hold Y or N. If used is a Y, then number1 and number 2 are the two numbers for the domino. Your main function will only allocate the array
of dominos and read the option for which action to take. Actions are print, add, remove and quit (exits the program).
You must write at least the following four functions.
int getIndex();
void addDomino(Domino * d, int size);
void removeDomino(Domino * d, int offset);
void print(Domino * d, int size);
The getIndex function will get the index to be removed.
The addDomino function will find a free spot, and then read in number1 and number2 for that domino. It will also set that domino as being used (used = ‘Y’).
The removeDomino function will remove the domino at the location index. This can be done by setting the used flag for that index to ’N’);
Problem: I am not getting the removeDomino function to work properly. It only removes one element and wont work again.
Problem: I am not getting the addDomino function to write over the removed domino index and then continue as normal. My function will only add to the end of the array.
Please Help.
Here is the program I wrote:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int number1;
int number2;
char used;
}domino;
int getIndex();
void addDomino(domino * d, int size);
void removeDomino(domino * d, int offset);
void print(domino * d, int size);
int main()
{
int size = 0;
int index = 0;
int offset = 100;
int counter = 0;
int i = 0;
char * dummy = malloc(sizeof(char));
printf("Enter the max number of dominoes: ");
scanf("%d%c", &size, dummy);
domino * d = malloc(sizeof(domino) * size);
domino current[size];
char * input = malloc(sizeof(char));
(*input) = ' ';
while((*input) != 'q')
{
printf("Enter (p) to print, (a) to add, (r) to remove, and (q) to quit: ");
scanf("%c%c", input, dummy);
switch(*input)
{
case 'p':
index = counter;
printf("Printing Dominoes\n");
for(index = 0; index < counter; index++)
{
if(current[index].used == 'y')
{
print(&current[index], index);
}
else
{
}
}
break;
case 'a':
addDomino(&current[index], index);
index++;
counter = index;
break;
case 'r':
getIndex();
removeDomino(&current[offset], offset);
break;
case 'q':
printf("All done\n");
break;
default:
break;
}
}
return 0;
}
int getIndex()
{
int offset = 0;
char dummy = ' ';
printf("Enter the index:");
scanf("%d%c", &offset, &dummy);
return offset;
}
void addDomino(domino * d, int size)
{
char * dummy = malloc(sizeof(char));
printf("Enter number 1\n:");
scanf("%d%c", &d->number1, dummy);
printf("Enter number 2\n:");
scanf("%d%c", &d->number2, dummy);
d->used = 'y';
}
void removeDomino(domino * d, int offset)
{
d->used = 'n';
}
void print(domino * d, int index)
{
printf("Domino %d (%d,%d)\n", index, d->number1, d->number2);
}
In your 'r' case, you forgot to use the return value of GetIndex.
The offset used in getIndex is local to that function and doesn't change the offset variable in your main.
if you change getIndex(); to offset = getIndex(); I think it should work correctly.

Resources