swap strings function in c with pointers - c

When I try to swap between strings in the function Update_student it doesn't make it. Why?
#include <stdio.h>
#include <string.h>
#define SIZE 1
struct student {
int id_number;
char name[50];
char sex[6];
int quiz_score[2];
int total_score;
};
void Add_Student_Records(struct student *pupil) {
printf("ID:");
scanf("%d", &pupil->id_number);
printf("Name: ");
scanf("%s", &pupil->name);
printf("Sex :");
scanf("%s", &pupil->sex);
for (int i = 0; i < 2; i++) {
printf("Quit score %d:", i + 1);
scanf("%d", &pupil->quiz_score[i]);
}
pupil->total_score = pupil->quiz_score[0] + pupil->quiz_score[1];
return;
}
void Add_Students(struct student *students) {
for (int i = 0; i < SIZE; i++) {
printf("Student %d:\n", i + 1);
Add_Student_Records(students);
}
return;
}
void Print_Students(struct student *students) {
for (int i = 0; i < SIZE; i++) {
printf("Student %d details: \n", i + 1);
printf("ID:%d\n", students->id_number);
printf("Name:%s\n", students->name);
printf("Sex:%s\n", students->sex);
for (int i = 0; i < 2; i++) {
printf("Quit score %d:\n", students->quiz_score[i]);
}
printf("Total score: %d\n", students->total_score);
students++;
}
return;
}
void Replace_Strings(char **old_string, char **new_string) {
*old_string = *new_string;
return;
}
void Update_Student(struct student *students) {
int i = 0;
char name[50], new_name[50], cur_name[50];
printf("You can update name, and scores.\n");
printf(" current Name: ");
scanf("%s", name);
printf("new name: ");
scanf("%s", &new_name);
while (i < SIZE) {
strcpy(cur_name, students->name);
if (strcmp(cur_name, name) == 0) {
char *ptr_old_stud_name = students->name;
char *ptr_new_stud_name = new_name;
Replace_Strings(&ptr_old_stud_name, &ptr_new_stud_name);
}
i++;
students++;
}
return;
}
int main() {
struct student students[SIZE];
char ch;
/*1.Add student, 2. Print student*/
printf("1.Add student\n2.Print students\n3.Update student\n");
scanf("%c", &ch);
while (ch != 'E') {
if (ch == '1') {
Add_Students(&students[0]);
}
else if (ch == '2') {
Print_Students(&students[0]);
}
else if (ch =='3') {
Update_Student(&students[0]);
}
printf("Another operation:\t");
scanf("%c", &ch);
}
}

Replace_Strings(&ptr_old_stud_name,&ptr_new_stud_name); passes the addresses of ptr_old_stud_name and ptr_new_stud_name to ReplaceStrings.
ptr_old_stud_name and ptr_new_stud_name are local variables. The first is a pointer that has been set to point to students->name. The second is a pointer that has been set to point to new_name.
Replace_Strings changes the first thing it is passed a pointer to to the second thing it is passed a pointer to. So it changes ptr_old_stud_name to have the value of ptr_new_stud_name.
The result is that the local variable ptr_old_stud_name has a new value. This does not change the thing it points to, students->name.
More specifically, ptr_old_stud_name was pointing to the first character of students->name. students->name is an array, and it cannot be altered by changing pointers to it, and its address cannot be changed. To change its contents, you must copy new values into the bytes within it, which you could do by using strcpy to copy bytes into it from new_name.

Your function Update_student is confusing, you should just iterate through the array of students and compare the student's name with cur_name and replace the name when there is a match.
You should also pass the number of students to handle as an argument.
Here is a modified version:
void Update_Student(struct student *students, int count) {
char cur_name[50], new_name[50];
printf("You can update name, and scores.\n");
printf(" current Name: ");
scanf("%49s", cur_name);
printf("new name: ");
scanf("%49s", new_name);
for (int i = 0; i < count; i++) {
if (strcmp(cur_name, students[i].name) == 0) {
strcpy(students[i].name, new_name);
}
}
}
Call from main as Update_Student(students, SIZE);
Note also that you should ignore whitespace when reading the commands by adding a space before the %c:
scanf(" %c", &ch);
Here is a modified version with for multiple students:
#include <stdio.h>
#include <string.h>
#define SIZE 10
struct student {
int id_number;
char name[50];
char sex[6];
int quiz_score[2];
int total_score;
};
int Add_Student_Records(struct student *pupil) {
printf("ID:");
if (scanf("%d", &pupil->id_number) != 1)
return 0;
printf("Name: ");
if (scanf("%49s", pupil->name) != 1)
return 0;
printf("Sex :");
if (scanf("%1s", pupil->sex) != 1)
return 0;
for (int i = 0; i < 2; i++) {
printf("Quiz score %d:", i + 1);
if (scanf("%d", &pupil->quiz_score[i]) != 1)
return 0;
}
pupil->total_score = pupil->quiz_score[0] + pupil->quiz_score[1];
return 1;
}
int Add_Students(struct student *students, int count) {
int i;
for (int i = 0; i < count; i++) {
printf("Student %d:\n", i + 1);
if (Add_Student_Records(students + i) == 0)
break;
}
return i;
}
void Print_Students(struct student *students, int count) {
for (int i = 0; i < count; i++) {
printf("Student %d details: \n", i + 1);
printf("ID:%d\n", students->id_number);
printf("Name:%s\n", students->name);
printf("Sex:%s\n", students->sex);
for (int i = 0; i < 2; i++) {
printf("Quit score %d:\n", students->quiz_score[i]);
}
printf("Total score: %d\n", students->total_score);
students++;
}
}
void Update_Student(struct student *students, int count) {
char cur_name[50], new_name[50];
printf("You can update name, and scores.\n");
printf(" current Name: ");
if (scanf("%49s", cur_name) != 1)
return;
printf("new name: ");
if (scanf("%49s", new_name) != 1)
return;
for (int i = 0; i < count; i++) {
if (strcmp(cur_name, students[i].name) == 0) {
strcpy(students[i].name, new_name);
}
}
}
int main() {
struct student students[SIZE];
int n = 0;
char ch = 'E';
/* print the menu */
printf("1. Add student\n"
"2. Print students\n"
"3. Update student\n");
scanf(" %c", &ch);
while (ch != 'E') {
if (ch == '1') {
if (n == SIZE) {
printf("student array is full\n");
} else {
/* add more students */
n += Add_Students(&students[n], SIZE - n);
}
} else
if (ch == '2') {
Print_Students(students, n);
} else
if (ch =='3') {
Update_Student(students, n);
}
scanf("%*[^\n]"); // consume the rest of the pending input line
printf("Another operation:\t");
if (scanf(" %c", &ch) != 1)
break;
}
return 0;
}

Related

C program dynamic memory allocation error, string value issue

I have tried multiple methods for a long time but it didn't work, the output automatically updates all previously entered values for the name.
The find method is not required to be checked, but the first display itself provides invalid output:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int phone;
char *name;
} phonecontact;
phonecontact *create(phonecontact *p, int phone, char name[], int i) {
(p+i)->name = (char *)calloc(10, sizeof(char));
(p+i)->name = name;
(p+i)->phone = phone;
printf("\n%s\n", (p+i)->name);
return p;
}
void display(phonecontact *p, int i) {
printf("Name : %s , Phone number : %d\n", p[i].name, p[i].phone);
}
int find(phonecontact *p, char name[], int *num) {
int phone, i = 0;
for (i = 0; i < *num; i++) {
if (strcmp(p[i].name, name) == 1)
display(p, i);
return 1;
}
p = (phonecontact *)realloc(p, ++(*num) * sizeof(phonecontact));
printf("Enter phone number for entered person : ");
scanf("%d", &phone);
p = create(p, phone, name, i + 1);
return 0;
}
int main() {
int num, phone;
char name[30];
printf("Enter number of contacts : ");
scanf("%d", &num);
phonecontact *p;
p = (phonecontact *)calloc(num, sizeof(phonecontact));
for (int i = 0; i < num; i++) {
printf("Enter name : ");
scanf("%s", name);
printf("Enter phone number : ");
scanf("%d", &phone);
p = create(p, phone, name, i);
}
printf("\n%s\n", (p)->name);
printf("\n%s\n", (p + 1)->name);
printf("\n%s\n", (p + 2)->name);
for (int i = 0; i < num; i++) {
display(p, i);
}
printf("Enter name of contact : ");
scanf("%s", &name);
find(p,name, &num);
}
There are many problems in the code:
For each contact to have its own name string, you must allocate a copy of the create() string argument:
phonecontact *create(phonecontact *p, int phone, const char *name, int i) {
p[i].name = strdup(name);
p[i].phone = phone;
printf("\n%s\n", p[i].name);
return p;
}
the function strcmp() returns 0 when the strings compare equal, a negative value if the first argument compares less than the second in lexicographical order and a value greater than zero otherwise.
You should use if (strcmp(p[i].name, name) == 0) and return 1 only if the strings compare equal.
It is confusing for a function named find() to append a new entry in the array.
The array reallocated by find() is not passed back to the caller, and the previous pointer may have become invalid, causing undefined behavior.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int phone;
char *name;
} phonecontact;
int flush_input(void) {
int c;
while ((c = getchar()) != EOF && c != '\n')
continue;
return c;
}
void init_contact(phonecontact *p, int phone, const char *name) {
p->name = strdup(name);
p->phone = phone;
}
void display_contact(const phonecontact *p) {
printf("Name: %s, Phone number: %d\n", p->name, p->phone);
}
int add_contact(phonecontact **pp, int *pnum, const char *name) {
int phone;
int num = *pnum;
phonecontact *p = *pp;
for (int i = 0; i < num; i++) {
if (strcmp(p[i].name, name) == 1) {
display_contact(&p[i]);
return 1;
}
}
/* reallocate the contact array */
p = (phonecontact *)realloc(p, (num + 1) * sizeof(*p));
if (p == NULL) {
printf("cannot reallocate contact array\n");
return -1;
}
printf("Enter phone number for entered person: ");
while (scanf("%d", &phone) != 1) {
printf("Invalid input. Try again: ");
if (flush_input() == EOF)
return -1;
}
init_contact(&p[num], phone, name);
*pnum = num + 1;
*pp = p;
return 0;
}
int main() {
int num, phone;
char name[30];
printf("Enter number of contacts : ");
if (scanf("%d", &num) != 1 || num < 0) {
fprintf(stderr, "invalid input\n");
return 1;
}
phonecontact *p = calloc(num, sizeof(*p));
if (p == NULL) {
fprintf(stderr, "memory allocation error\n");
return 1;
}
for (int i = 0; i < num; i++) {
printf("Enter name: ");
if (scanf(" %29[^\n]", name) != 1) {
fprintf(stderr, "invalid input\n");
return 1;
}
printf("Enter phone number: ");
if (scanf("%d", &phone) != 1)
fprintf(stderr, "invalid input\n");
return 1;
}
init_contact(&p[i], phone, name);
}
for (int i = 0; i < num; i++) {
display_contact(&p[i]);
}
printf("Enter name of contact: ");
if (scanf(" %29[^\n]", &name) == 1)
add_contact(&p, *num, name);
return 0;
}

Why is NULL printed here? - C-90

The code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 500
#define M 100
/*Version 1*/
void replace_word(char *word);
void add_new_word_dictionary(void);
void do_nothing(void);
void return_basic(void);
void check_word(void);
void compute_words(void);
void compute_characters(void);
void compute_ch_sp(void);
void compute_num_dif_words(void);
void create_istogram(void);
void save_file(void);
int get_choice(void);
void return_word(void);
void insert_text(int numwords, char matrix[N][M], int posit);
int main() {
int j;
int choice = 0;
char matrix[N][M];
char word[40] = { "t" };
while (1) {
choice = get_choice();
if (choice == 0) {
insert_text(M, matrix, 1);
}
if (choice == 1) {
add_new_word_dictionary();
}
if (choice == 2) {
do_nothing();
}
if (choice == 3) {
save_file();
}
if (choice == 4) {
compute_words();
}
if (choice == 5) {
break;
}
for (j = 0; j < M; j++) {
printf("%s", matrix[N][M]);
}
}
printf("\n End of Program \n");
return 0;
}
void replace_word(char *word) {
return;
}
void add_new_word_dictionary(void) {
char word[50] = { "s" };
printf("\nPlease enter the word\n");
scanf("\n%s", word);
printf("Your word is %s", word);
return;
}
void do_nothing(void) {
printf("\n do_nothing \n");
return;
}
void return_basic(void) {
printf("\n return basic \n");
return;
}
void check_word(void) {
printf("\n check word \n");
return;
}
void compute_words(void) {
printf("\n compute_words \n");
return;
}
void compute_characters(void) {
printf("\n compute characters \n");
}
void compute_ch_sp(void) {
printf("\n compute_ch_sp \n");
return;
}
void compute_num_dif_words(void) {
printf("\n compute_num_same_words \n");
return;
}
void create_istogram(void) {
printf("\n create istogram \n");
return;
}
void save_file(void) {
printf("\n save_file \n");
return;
}
int get_choice(void) {
int choice = 0;
printf("\n Select a choice from the below \n");
printf("\n Select 0 to add text \n");
printf("\n Select 1 to add new words in the dictionary \n");
printf("\n Select 2 to enter enter correction mode \n");
printf("\n Select 3 to save the text \n");
printf("\n Select 4 to see the statistics about your text \n");
printf("\n Select 5 to exit the program\n");
scanf("\n%d", &choice);
return choice;
}
void insert_text(int numwords, char matrix[N][M], int posit) {
int i;
int j;
char word2[40] = { "" };
while (strcmp(word2, "*T*E*L*O*S*")) {
printf("\n Add the word \n");
scanf("\n%s", word2);
if (posit + 1 > numwords) {
printf("\n Out of Bounds \n ");
}
for (i = numwords - 2; i >= posit; i--) {
strcpy(matrix[i + 1], matrix[i]);
if (!i)
break;
}
strcpy(matrix[posit], word2);
j++;
}
for (i = 0; i < j; i++) {
printf("%s", matrix[i]);
printf("\n j is %d\n", j);
}
return;
}
The problem: I have a function called insert_text. This function adds a string in the 1st position of an array (at least that is what I think it does) and it is called if choice is 0 and executes itself until the string *Ī¤ELOS* is given. When in insert_text I print matrix I get a bunch of *(null)*s... I can count how many words matrix has (by declaring a variable j and incrementing by 1 inside the while loop, but that does not seem to work either. How can I fix this?
The printing code is incorrect: matrix is an array of N arrays of M characters, where you store null terminated C strings. As coded, you pass a single character just beyond the end of the array to printf for %s, which expects a string. The loop should be:
for (j = 0; j < N; j++) {
printf("%s ", matrix[j]);
}
Note that char matrix[N][M]; is uninitialized, so its contents will seem random. Initialize matrix as char matrix[N][M] = { "" };
Also note that in add_new_word_dictionary(), the scanf() conversion should be scanf("%49s", word); to prevent a potential buffer overflow if the user enters a very long word.
Same in insert_text, the code should be scanf("%39s", word2) ans you should test the return value to check for input errors.
Finally, arrays are indexed from 0 in C, so insert_text should be given a position of 0 and the number of words should be N, not M.
Both the test and the insertion loop have problems too.
Here is a modified version:
// call from main as insert_text(N, matrix, 0);
//
void insert_text(int numwords, char matrix[N][M], int posit) {
char word2[40];
int i, j = 0;
for (;;) {
printf("\n Add the word \n");
if (scanf("%39s", word2) != 1) {
break; // end of file or input error
}
if (!strcmp(word2, "TELOS")) {
break; // magic word
}
if (posit >= numwords) {
printf("\n Out of Bounds \n");
break;
}
for (i = numwords - 2; i >= posit; i--) {
strcpy(matrix[i + 1], matrix[i]);
}
strcpy(matrix[posit], word2);
j++;
}
for (i = 0; i < j; i++) {
printf("%s ", matrix[i]);
}
printf("\n j is %d\n", j);
}
Here is a modified version of the whole program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 500
#define M 100
/*Version 1*/
void replace_word(char *word);
void add_new_word_dictionary(void);
void do_nothing(void);
void return_basic(void);
void check_word(void);
void compute_words(void);
void compute_characters(void);
void compute_ch_sp(void);
void compute_num_dif_words(void);
void create_istogram(void);
void save_file(void);
int get_choice(void);
void return_word(void);
void insert_text(int numwords, char matrix[N][M], int posit);
int main() {
int j, done = 0;
char matrix[N][M] = { "" };
while (!done) {
switch (get_choice()) {
case 0:
insert_text(N, matrix, 0);
break;
case 1:
add_new_word_dictionary();
break;
case 2:
do_nothing();
break;
case 3:
save_file();
break;
case 4:
compute_words();
break;
default:
done = 1;
break;
}
for (j = 0; j < N; j++) {
if (matrix[j][0])
printf("%s ", matrix[j]);
}
printf("\n");
}
printf("\n End of Program \n");
return 0;
}
void add_new_word_dictionary(void) {
char word[50];
printf("\nPlease enter the word\n");
if (scanf("%49s", word) == 1)
printf("Your word is %s\n", word);
}
void replace_word(char *word) { printf("\n replace word \n"); }
void do_nothing(void) { printf("\n do_nothing \n"); }
void return_basic(void) { printf("\n return basic \n"); }
void check_word(void) { printf("\n check word \n"); }
void compute_words(void) { printf("\n compute_words \n"); }
void compute_characters(void) { printf("\n compute characters \n"); }
void compute_ch_sp(void) { printf("\n compute_ch_sp \n"); }
void compute_num_dif_words(void) { printf("\n compute_num_same_words \n"); }
void create_istogram(void) { printf("\n create istogram \n"); }
void save_file(void) { printf("\n save_file \n"); }
int get_choice(void) {
int choice = -1;
printf("\nSelect a choice from the below \n");
printf("Select 0 to add text \n");
printf("Select 1 to add new words in the dictionary \n");
printf("Select 2 to enter enter correction mode \n");
printf("Select 3 to save the text \n");
printf("Select 4 to see the statistics about your text \n");
printf("Select 5 to exit the program\n");
scanf("%d", &choice);
return choice;
}
// call from main as insert_text(N, matrix, 0);
//
void insert_text(int numwords, char matrix[N][M], int posit) {
char word2[40];
int i;
for (;;) {
printf("\n Add the word \n");
if (scanf("%39s", word2) != 1) {
break; // end of file or input error
}
if (!strcmp(word2, "TELOS")) {
break; // magic word
}
if (posit >= numwords) {
printf("\n Out of Bounds \n");
break;
}
for (i = numwords - 2; i >= posit; i--) {
strcpy(matrix[i + 1], matrix[i]);
}
strcpy(matrix[posit], word2);
posit++;
}
for (i = 0; i < posit; i++) {
printf("%s ", matrix[i]);
}
printf("\n posit is %d\n", posit);
}

How to fix the error of get two string at the same time

I've got a task to input some information of some student and export it in the form of a report
#include <stdio.h>
#define MAX 1000
typedef struct
{
char id[6];
char name[80];
float grade;
char classassessment;
} exam;
void clear_buffer()
{
int ch;
while ((ch = getchar()) != '\n' && ch != EOF)
;
}
char assess(float a)
{
if (9 <= a && a <= 10)
return 'A';
else if (8 <= a && a < 9)
return 'B';
else if (6.5 <= a && a < 8)
return 'C';
else
return 'D';
}
int main()
{
char result[MAX];
exam student[MAX];
int n, i=0;
printf("The number of students: ");
scanf("%d", &n);
clear_buffer();
for (i = 0; i < n; i++)
{
printf("Enter student %d ID: ", i + 1);
scanf("%s", student[i].id);
clear_buffer();
printf("Enter this student name: ");
gets(student[i].name);
printf("Enter this student's grade: ");
scanf("%f", &student[i].grade);
clear_buffer();
result[i] = assess(student[i].grade);
}
for(i=0; i<n; i++)
{
printf("%d\t%s\t\t\t%s\t\t%c\n", i+1, student[i].id, student[i].name, result[i]);
}
return 0;
}
After compiling it, you can see that student[i].id has both ID and name while student[i].name still has names of the students that have just been inputted. I can not explain why it has this error.

I want to printout them in ascending order with the given function code

I really need help for this, like i want to print out them and what do i need to adding to this function to work.
#include <stdio.h>
#include <stdlib.h>
#define nameSizeMax 31
#define listSizeMax 100
char studentList[listSizeMax][nameSizeMax];
int listSize;
void addStudent() {
if(listSize == listSizeMax) {
printf("List is full!\n\n");
} else {
printf("Enter name of the student: ");
fflush(stdin);
char name[nameSizeMax];
gets(name);
trim(name);
strupr(name);
strcpy(studentList[listSize], name);
listSize++;
printf("The new student have been added!\n\n");
}
}
int searchStd(char list[][nameSizeMax], char item[], int size) {
int i;
for(i=0;i<size; i++);
if (strcmp(list[i], item) == 0) return 1;
return -1;
}
void removeStudent() {
int i
if (listSize == 0) {
printf("List is empty!\n\n");
} else {
printf ("Enter name of the removed student: ");
fflush(stdin);
char name[nameSizeMax];
gets(name);
trim(name);
strupr(name);
int pos = searchStd(studentList, name, listSize);
}
if(pos == -1) {
printf("This student does not exist!\n\n");
} else {
for (i=pos; i<listSize-1; i++);
strcpy(studentList[i], studentList[i+1]);
printf("This student has been removed from the list: \n\n");
listSize--;
}
}
void fintStudent() {
int i;
printf("Enter the name of student you want to search: ");
fflush(stdin);
char name[nameSizeMax];
gets(name);
trim(name);
strupr(name);
int pos = searchStd(studentList, name, listSize);
if(pos == -1) {
printf("This student does not exist:\n\n");
} else {
printf("\n This is the student list in the order you entered: \n");
for(i=0; i<listSize; i++) printf("%d - %s\n", i, studentList[i]);
printf ("This student's postition in the list is: %d\n\n", pos);
}
}
void printStudentAsc(char list[][nameSizeMax], int size) {
int i,j:
if (listSize == 0) {
printf("List is empty!\n\n");
} else {
char **listP = (char **) calloc(sizeof(char *));
for (i=o; i<size; i++) listP[i] = list[i];
for (i=0; i<size-1; i++)
for(j=i+1; j<size; j++) {
char *ln1,*ln2;
ln1= lastname(listP[i]);
ln2= lastname(listP[j]);
if (strcmp(ln1, ln2) == 1) {
char *tmp = listP[i];
listP[i] =listP[j];
listP[j] = tmp;
}
}
}
for (i = 0; i < (*pn); i++)
{
nameStr(list[i]);
printf("Name[%d] : %s \n", i, list[i]);
}
}
int menu(){
printf("Menu:\n");
printf("1- Add a student\n");
printf("2- Remove a student\n");
printf("3- Search a student\n");
printf("4- Print out the student list in ascending order\n");
int userChoice;
do{
printf("Insert ur operation: "); scanf("%d", &userChoice);
}
}
Edited :'( i really struggle about the printAsc like i dont know what to do next . Its really messed me up here. Only the printStudentAsc.
the Menu interface that im not finish yet but i can hanlde this.

Program errors due to strcmp and strcpy in C

No matter how I edit my program there seems to be overflow errors and mismatching type errors. Can someone help me to make this run without errors.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int choice;
int i;
int j;
char type;
int amount;
int don_count = 0;
int req_count = 0;
int flag;
char donations_inv_type[100][20];
int donations_amount[100];
char requests_inv_type[100][20];
int req_amount[100];
printf("Welcome to the Food Bank Program\n\n 1. Add a donation\n 2. Add a request\n 3. Fulfill a request\n 4. Print status report\n 5. Exit\n\nEnter your choice: ");
scanf("%d", &choice);
while (choice != 5) {
if (choice == 1) {
printf("\nEnter inventory type: ");
scanf("%s", &type);
printf("Enter the amount: ");
scanf("%d", &amount);
printf("\nDonation Added!\n\n");
flag = -99;
for (i = 0; i < don_count; i++) {
if (strcmp(donations_inv_type[i], type) == 0)
flag = i;
}
if (flag == -99) {
strcpy(donations_inv_type[i], type);
donations_amount[i] = amount;
don_count++;
}
else
donations_amount[flag] += amount;
printf("Donation Added!\n");
printf("Press any key to continue . . .\n\n");
}
else if (choice == 2) {
printf("\nEnter inventory type: ");
scanf("%s", &type);
printf("Enter the amount: ");
scanf("%d", &amount);
strcpy(requests_inv_type[req_count], type);
req_amount[req_count] = amount;
req_count++;
}
else if (choice == 3) {
printf("\n\n-------- Fulfilling Requests--------");
flag = -99;
for (i = 0; i < don_count; i++) {
if (strcmp(donations_inv_type[i], requests_inv_type[0]) == 0)
flag = i;
}
if (flag == -99)
printf("Cannot be Fulfilled\n\n");
else if (donations_amount[flag] > req_amount[0]) {
donations_amount[flag] -= req_amount[0];
printf("Request Fulfilled");
req_amount[0] = 0;
}
else if (donations_amount[flag] == req_amount[0]) {
printf("Request Fulfilled");
for (i = flag; i < don_count; i++) {
strcpy(donations_inv_type[i], donations_inv_type[i + 1]);
strcpy(donations_amount[i], donations_amount[i + 1]);
}
don_count--;
for (i = flag; i < req_count; i++) {
strcpy(requests_inv_type[i], requests_inv_type[i + 1]);
strcpy(req_amount[i], req_amount[i + 1]);
}
req_count--;
}
else if (donations_amount[flag] < req_amount[0]) {
printf("Partially Fulfilled");
req_amount[0] -= donations_amount[flag];
for (i = flag; i < don_count; i++) {
strcpy(donations_inv_type[i], donations_inv_type[i + 1]);
strcpy(donations_amount[i], donations_amount[i + 1]);
don_count--;
}
}
}
else if (choice == 4) {
printf("Printing the Donations Table\n\n");
for (i = 0; i < don_count; i++) {
printf("%s %d", donations_inv_type[i], donations_amount[i]);
}
printf("Printing the Requests Table\n\n");
for (i = 0; i < req_count; i++) {
printf("%s %d", requests_inv_type[i], req_amount[i]);
}
}
printf("Welcome to the Food Bank Program\n\n 1. Add a donation\n 2. Add a request\n 3. Fulfill a request\n 4. Print status report\n 5. Exit\n\nEnter your choice: ");
}
}
Any help is greatly appreciated and I would love an explanation as to what I did wrong so that I can learn and not make the same mistakes next time.
Declare type as character array.
char type[50];
Remove & in scanf(). You should not use & while reading string.
scanf("%s", &type); ==> scanf("%s", type);
^
Here you want to copy integers not strings
strcpy(donations_amount[i], donations_amount[i + 1]);
strcpy(req_amount[i], req_amount[i + 1]);
Modify like this
donations_amount[i]=donations_amount[i + 1];
req_amount[i]= req_amount[i + 1];
Instead of char type you need char type[100]
Error in your code:
if (strcmp(donations_inv_type[i], type) == 0)
// ^^^^ should be char*
Note: Functions strcmp() and strcpy() should be passed \0 nul-terminated array of char (or say string).
Your scanf should look like scanf("%s", type);

Resources