strcmp refuses to work [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I've tried everything, yet strcmp (as well as strncmp) always give a value other than 0, both with and without using pointers, as well as inside and outside functions. What should I do?
The point of the program is to create a student data system for signing up and in, as well as managing and sorting said data, the latter which I haven't implemented yet.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int signup(char* ET, char* OT, int* G1T, int* G2T, int* G3T, int* avg, char* status, int* LI, int* sT, int* TT, char** UT, int** PT)
{
int i;
char LU[21];
int ID;
if (strcmp (status, "Register") == 0)
{
printf("Enter last name: ");
scanf("%s", ET);
printf("Enter first name: ");
scanf("%s", OT);
while(1)
{
printf("Enter C grade: ");
scanf("%d", G1T);
if (*G1T >= 0 && *G1T <= 10)
{
break;
}
}
while(1)
{
printf("Enter Java grade: ");
scanf("%d", G2T);
if (*G2T >= 0 && *G2T <= 10)
{
break;
}
}
while(1)
{
printf("Enter C++ grade :");
scanf("%d", G3T);
if (*G3T >= 0 && *G3T <= 10)
{
break;
}
}
*avg = (*G1T + *G2T + *G3T)/3;
*UT[*TT] = *ET;
*sT = 1;
}
else
{
printf("Enter username: ");
scanf("%s", &LU);
for (ID = 0; ID < 100; ID++)
{
if (strncmp(LU, UT[ID], 20) == 0)
{
break;
}
}
if (ID == 100)
{
return 0;
}
else
{
printf("Enter password: ");
scanf("%s", LU);
}
}
return 0;
}
void pass(char** User, int** Pass, int* Total)
{
int cd[21];
int i, j;
for (i=0; i<21; i++)
{
cd[i] = *User[i];
if (i%2 == 0)
{
if(cd[i] >= 97 && cd[i <= 122])
{
cd[i] = cd[i] - 32;
}
}
else
{
if(cd[i] >= 65 && cd[i] <= 90)
{
cd[i] = cd[i] + 32;
}
}
}
}
int main(void) {
int i, j, z;
int succ, *sT;
char intro[9], *status;
int Total = 0;
int* TT;
int LoggedIn = 0;
int* LI;
char Ep[100][21], *ET, On[100][21], *OT;
int Age[100], *AgeT, Gr1[100], *G1T, Gr2[100], *G2T, Gr3[100], *G3T, avg[100], *avgT;
char UN[100][21], *UT[100];
int PW[100][21], *PT[100];
while(1)
{
system("cls");
if (succ = 0)
{
printf("ERROR: Last name found.");
}
succ = 1;
while(1)
{
printf("Type your option (Login/Register): ");
scanf("%s", intro);
if ((strcmp (intro, "Login") == 0) || (strcmp (intro, "Register") == 0))
{
break;
}
}
if ((strcmp (intro, "Login") == 0) || (strcmp (intro, "Register") == 0))
{
for(i = 0; i < 100; i++)
{
UT[i] = &UN[i][0];
PT[i] = &PW[i][0];
}
ET = &Ep[Total][0];
OT = &On[Total][0];
G1T = &Gr1[Total];
G2T = &Gr2[Total];
G3T = &Gr3[Total];
avgT = &avg[Total];
LI = &LoggedIn;
status = &intro[0];
sT = &succ;
TT = &Total;
signup(ET, OT, Gr1, Gr2, Gr3, avg, status, LI, sT, TT, UT, PT);
for(i = 0; i<Total; i++)
{
if(strncmp(UN[Total], UN[i], 20) == 0)
{
succ = 0;
}
}
if (succ == 1)
{
pass(UT, PT, TT);
Total++;
}
}
}
return 0;
}
An example for my inputs is the following:
Register
LastName
FirstName
4
5
6
Then:
Register
LastName
FN2
7
6
5
And I expect to see "ERROR: Last name found." right above the starting message. However, it never appears, suggesting strcmp failed.

First off, you're allocating a lot of arrays in local variables in main(). That could lead to a stack overflow. You should probably use malloc() to allocate them instead.
Second, this line is an assignment, not a comparison, which is a bug:
if (succ = 0)
Change it to this, if you want it to be a comparison:
if (succ == 0)
Third, you're not initializing succ at the beginning of main() which is a bug.
If I see anything else suspect I'll update my answer. But start with fixing those issues.

There are a few issues with your code, as was pointed out, but the reason it doesn't work as you expect is the line: if(succ = 0)
I changed it to: if(succ==0)
And it worked as you described.
Additionally this line here: scanf("%s", &LU); should be scanf("%s", LU);
The compiler is probably generating a warning about the format.

Related

How to detect duplicate string using strcmp()

#include<stdio.h>
#include<stdio.h>
#include<string.h>
struct stud
{
char nam[20];
int num;
char letter[5];
};
int main()
{
struct stud s[5];
int i, j;
for(i = 0; i < 5; i++){
printf("Enter the name of student #%d: ", i+1);
scanf("%s", s[i].nam);
printf("Enter the number grade of student #%d: ", i+1);
scanf("%d", &s[i].num);
}
for (j = 0; j < i; j++) {
if (strcmp(s[i].nam, s[j].nam) == 0)
printf("Error. Duplicate name detected.");
}
for(i = 0; i < 5; i++){
if(s[i].num >= 90 )
strcpy(s[i].letter, "A");
else if(s[i].num >= 80)
strcpy(s[i].letter, "B");
else if(s[i].num >= 70)
strcpy(s[i].letter, "C");
else if(s[i].num >= 60)
strcpy(s[i].letter, "D");
else
strcpy(s[i].letter, "F");
}
for(i = 0; i < 5; i++)
printf("\n%s has a %s ", s[i].nam, s[i].letter);
return 0;
}
This program has the user enter 5 names and 5 numeric grades, which will then result in the output of their respective letter grades for that student. I'm trying to make it so if the user enters a duplicate name, and message will print saying they can't do that. My attempt in trying to do this is as follows:
for (j = 0; j < i; j++) {
if (strcmp(s[i].nam, s[j].nam) == 0)
printf("Error. Duplicate name detected.");
}
Where I believe that s[j] is the previous string, and compare to see if it equals 0(duplicate) and prints a message. This obviously doesn't work however so I would like to know how to fix this so it can correctly detect duplicate names. Thank you.
Also I have posted this question before but the person that provided an explanation deleted their response before I could provide further questions and ask for clarification. So I am posting this again with an attempt in seeking further aid in what I did wrong in my code.
At the start of the detection loop, i is already 5, so using s[i] is undefined behavior
In your detection loop, i is invariant. you are just comparing a name against the last one [except for the UB, of course].
You need two loops to compare all names against one another.
Also, using 5 everywhere is a "magic number". Better to use a #define (e.g. SMAX)
In the code below, I use cpp conditionals to denote old vs. new code:
#if 0
// old code
#else
// new code
#endif
#if 1
// new code
#endif
Here is the corrected code. It is annotated with the bugs and fixes:
#include <stdio.h>
#include <stdio.h>
#include <string.h>
struct stud {
char nam[20];
int num;
char letter[5];
};
#define SMAX 5 // maximum number of students
int
main()
{
struct stud s[SMAX];
int i, j;
for (i = 0; i < SMAX; i++) {
printf("Enter the name of student #%d: ", i + 1);
scanf("%s", s[i].nam);
printf("Enter the number grade of student #%d: ", i + 1);
scanf("%d", &s[i].num);
}
// NOTE/BUG: i is already SMAX, so using s[i] is UB (undefined behavior)
// NOTE/BUG: i never changes
#if 0
for (j = 0; j < i; j++) {
if (strcmp(s[i].nam, s[j].nam) == 0)
printf("Error. Duplicate name detected.");
}
#else
for (i = 0; i < (SMAX - 1); i++) {
for (j = i + 1; j < SMAX; j++) {
if (strcmp(s[i].nam, s[j].nam) == 0)
printf("Error. Duplicate name detected -- %s\n",s[j].nam);
}
}
#endif
for (i = 0; i < SMAX; i++) {
if (s[i].num >= 90)
strcpy(s[i].letter, "A");
else if (s[i].num >= 80)
strcpy(s[i].letter, "B");
else if (s[i].num >= 70)
strcpy(s[i].letter, "C");
else if (s[i].num >= 60)
strcpy(s[i].letter, "D");
else
strcpy(s[i].letter, "F");
}
// NOTE/BUG: newline should go at the end of the printf to prevent a hanging
// last line
#if 0
for (i = 0; i < SMAX; i++)
printf("\n%s has a %s ", s[i].nam, s[i].letter);
#else
for (i = 0; i < SMAX; i++)
printf("%s has a %s\n", s[i].nam, s[i].letter);
#endif
return 0;
}
UPDATE:
Thanks for the tip! On a side note, how would I make it so while the user is entering the duplicate names, the error message appears and the program ends right there.For example: Enter the name of student 1: dan Enter grade: 87 Enter the name of student 2: dan Enter the grade: 78 Error. No duplicate names allowed. And then the program ends there. –
User234567
Easy enough. I put the duplication detection code into functions.
But, I've added a few more enhancements so this may help you with your learning ;-)
I added reprompting the user if they enter a duplicate.
I hate scanf ;-) I reworked the prompting code by putting it into two functions. It will work better if input is a file. This is useful during testing
I changed the conversion from grade number to grade letter to use a table.
Anyway, here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
struct stud {
char nam[20];
int num;
char letter[5];
};
struct letter {
int num;
const char *letter;
};
#define LET(_num,_let) \
{ .num = _num, .letter = _let }
struct letter letters[] = {
LET(90,"A"),
LET(80,"B"),
LET(70,"C"),
LET(60,"D"),
LET(0,"F"),
LET(0,NULL)
};
#define SMAX 5 // maximum number of students
// chkall -- check entire array for duplicates
int
chkall(const struct stud *s,int smax)
{
int i;
int j;
int dup = 0;
for (i = 0; i < (smax - 1); i++) {
for (j = i + 1; j < smax; j++) {
if (strcmp(s[i].nam, s[j].nam) == 0) {
printf("Error. Duplicate name detected -- %s\n",s[j].nam);
dup += 1;
}
}
}
return dup;
}
// chkone -- check a given entry for duplicate (as they are added)
int
chkone(const struct stud *s,int i)
{
int j;
int dup = 0;
for (j = 0; j < i; j++) {
if (strcmp(s[i].nam, s[j].nam) == 0) {
printf("Error. Duplicate name detected -- %s\n",s[j].nam);
dup += 1;
}
}
return dup;
}
// prompt_string -- prompt user for a string
char *
prompt_string(const char *what,int i,char *buf,size_t siz)
{
static int tty = -1;
// decide if our input is tty or file
if (tty < 0) {
struct winsize ws;
tty = ioctl(0,TIOCGWINSZ,&ws);
tty = (tty >= 0);
}
printf("Enter the %s of student #%d: ", what, i + 1);
fflush(stdout);
char *cp = fgets(buf,siz,stdin);
do {
// handle EOF
if (cp == NULL)
break;
buf[strcspn(buf,"\n")] = 0;
// echo the data if input is _not_ a tty
if (! tty)
printf("%s\n",buf);
} while (0);
return cp;
}
// prompt_number -- prompt user for a number
long long
prompt_number(const char *what,int i)
{
char *cp;
char buf[100];
long long val;
while (1) {
cp = prompt_string(what,i,buf,sizeof(buf));
// handle EOF
if (cp == NULL) {
val = -1;
break;
}
// decode the number
val = strtoll(buf,&cp,10);
if (*cp == 0)
break;
printf("invalid number syntax -- '%s'\n",cp);
}
return val;
}
int
main(void)
{
struct stud s[SMAX];
int i;
for (i = 0; i < SMAX; i++) {
while (1) {
prompt_string("name",i,s[i].nam,sizeof(s[i].nam));
if (! chkone(s,i))
break;
}
s[i].num = prompt_number("number grade",i);
}
// recheck all entries
// this will _never_ report a duplicate because of the chkone above
chkall(s,SMAX);
for (i = 0; i < SMAX; i++) {
for (struct letter *let = letters; let->letter != NULL; ++let) {
if (s[i].num >= let->num) {
strcpy(s[i].letter,let->letter);
break;
}
}
}
for (i = 0; i < SMAX; i++)
printf("%s has a %s\n", s[i].nam, s[i].letter);
return 0;
}

Even though array numbers match, it still comes back false

Hi I am currently having a problem with my program. When i enter a phone number char, and compare it with a different phone number char, the answer comes back false.
Here my function searches the "findContact" function for a exact number. The getTenDigitPhone is the function to get the phone number.
I end up getting the * Contact NOT FOUND * regardless if it matches or not
void searchContacts(const struct Contact contact[], int size) {
char phone[11];
int searchIndexContact;
printf("Enter the cell number for the contact: ");
getTenDigitPhone(phone);
searchIndexContact = findContactIndex(contact, size, phone);
if (searchIndexContact > -1) {
printf("\n");
printf("Contact found:\n");
displayContact(&contact[searchIndexContact]);
}
else {
printf("*** Contact NOT FOUND ***\n");
}
}
** Here is the getTenDigitPhone function
void getTenDigitPhone(char telNum[11])
{
int needInput = 1;
while (needInput == 1) {
scanf("%10s", telNum);
clearKeyboard();
// (String Length Function: validate entry of 10 characters)
if (strlen(telNum) == 10)
needInput = 0;
else
printf("Enter a 10-digit phone number: ");
}
}
And here is the findContactIndex (to find out if the numbers match)
int findContactIndex(const struct Contact contacts[], int size, const char cellNum[])
{
int i;
int value = 0;
for (i = 0; i < size; i++) {
if (contacts[i].numbers.cell == cellNum);{
printf(" %s %s",contacts[i].numbers.cell , cellNum);
value == 1;
}
}
if (value == 1) {
return value;
}
if (value == 0) {
return -1;
}
}
Enable your compiler's warnings! It would have found your problems. For example, with gcc, use at least
gcc -Wall -Wextra -pedantic ...
In findContactIndex,
value == 1;
is wrong. You were probably going for
value = 1;
but it should be
value = i;
break;
or just
return i;
since the function should return the index of the match. That means
int value = 0;
...
if (value == 1) {
return value;
}
if (value == 0) {
return -1;
}
should be
int value = -1;
...
return value;
or just
return -1;
Unrelated to your question, in findContactIndex,
if (...);{
should be
if (...) {
As you currently have it, the result of the conditional expression is disregarded, then the following block is executed unconditionally.
Unrelated to your question, in findContactIndex,
contacts[i].numbers.cell == cellNum
should be
strcmp(contacts[i].numbers.cell, cellNum) == 0
You are currently comparing the pointers instead of the strings.
Fixed:
int findContactIndex(const struct Contact contacts[], int size, const char cellNum[])
{
int i;
for (i=0; i<size; ++i) {
if (strcmp(contacts[i].numbers.cell, cellNum) == 0) {
return i;
}
}
return -1;
}

Dynamic array of Structs, delet elements

I am trying to create a database program of students where you should be able to add/modify and delete students. I have managed to get the add function working, and also the modify function but the delete function gives me some problems. my code seems to crash when im trying to delete a student from the database, can anyone tell me where the problem lies?
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* TODO: Avoid global variables. */
struct student {
char name[60];
long long personalNumber;
char gender[6];
char studyProgram[60];
char email[30];
int age;
};
struct student *pointer = NULL;
int numberofstudents = 0;
void modify()
{
long long persnr;
long long comp;
int match = 0;
printf("Please enter the personal number that you wish to modify: \n");
scanf("%lld", &persnr);
getchar();
for(int i = 0; i <= numberofstudents; i++)
{
comp = ((pointer+i)->personalNumber);
printf("%lld\n", ((pointer+i)->personalNumber));
printf("%lld\n", comp);
printf("Inne");
if (pointer[i].personalNumber == persnr && match == 0)
{
printf("Enter name, personalnumber, gender, studyprogram, email and age in this order\n");
scanf("%s%lld%s%s%s%d", (pointer+i)->name, &(pointer+i)->personalNumber, (pointer+i)->gender, (pointer+i)->studyProgram, (pointer+i)->email, &(pointer+i)->age);
match = 1;
getchar();
}
if (match == 0)
{
printf("Could not find person");
}
}
}
void deletestudent()
{
long long persnr;
long long comp;
int match = 0;
printf("Please enter the personal number that you wish to delete: \n");
scanf("%lld", &persnr);
getchar();
struct student *temporary = malloc((numberofstudents - 1) * sizeof(struct student));
for(int i = 0; i <= numberofstudents; i++)
{
if (pointer[i].personalNumber == persnr && match == 0)
{
match = 1;
}
else if (match == 1){
temporary[i-1] = pointer[i];
}
else
{
temporary[i] = pointer[i];
}
if (match == 0)
{
printf("Could not find person");
}
}
free(pointer);
pointer = temporary;
}
void add(){
if (numberofstudents > 0)
{
pointer = (struct student*) realloc(pointer, (numberofstudents+1) * sizeof(struct student));
printf("Lyckades allokeringen!\n\n");
}
printf("Enter name, personalnumber, gender, studyprogram, email and age in this order\n");
scanf("%s%lld%s%s%s%d", (pointer+numberofstudents)->name, &(pointer+numberofstudents)->personalNumber, (pointer+numberofstudents)->gender, (pointer+numberofstudents)->studyProgram, (pointer+numberofstudents)->email, &(pointer+numberofstudents)->age);
getchar();
printf("Visar data:\n");
for(int i = 0; i <= numberofstudents; ++i)
{
printf("%s\t%lld\t%s\t%s\t%s\t%d\n", (pointer+i)->name, (pointer+i)->personalNumber, (pointer+i)->gender, (pointer+i)->studyProgram, (pointer+i)->email, (pointer+i)->age);
}
numberofstudents = numberofstudents+1;
}
int main(void)
{
pointer = (struct student*) malloc(2 * sizeof(struct student));
if (pointer == NULL)
{
printf("pointer NULL");
exit(1);
}
int run = 1;
int choice;
while (run == 1)
{
printf("Please enter an option listed below\n1.ADD\n2.Modify\n3.Delete\n4.Search\n5.Save\n6.Load\n7.Exit");
scanf("%d", &choice);
getchar();
switch(choice) {
case 1 :
add();
break;
case 2 :
modify();
break;
case 3 :
deletestudent();
case 7 :
exit(0);
break;
default :
break;
}
}
return 0;
}
As mentioned in a comment, this:
for(int i = 0; i <= numberofstudents; i++)
is a huge warning sign. In C, such a loop should typically have i < numberofstudents in it, assuming numberofstudents is the actual length of the array.
Using <= rather than < makes the loop go one step too far, thus indexing outside the array and causing undefined behavior.
Writing outside heap-allocated memory and then trying to free() it is a good way to provoke crashes, since there's a chance you're stepping on the heap allocator's data structures (in practice; in theory all that happens is that you get undefined behavior and thus anything can happen).

I am getting a _\377 in my output

I have a school assignment to make a hangman game. The game works how I want it to except for one small glitch. If the user entered word is 4 letters or less, the hidden word is displayed with an extra "_\377" at the end. When the user entered word is 5 letters or more, then there is no glitch. I am hoping that someone would be kind enough to help me trouble shoot the problem. Thanks in advance!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int letterfinder(char string[], char a, int vari)
{
int length = strlen(string);
int i = vari;
int val = 0;
while( i <= length && val != 1)
{
if( string[i] == a)
{
val = 1;
}
i++;
}
if( val == 0)
{
return 100;
}
else
{
return i;
}
}
int main()
{
char inWord[] = "1111111111111111111111111111";
char outWord2[] = "1111111111111111111111111111";
char guess;
int gameover = 0;
int trys = 10;
int vari = 0;
printf("Please enter a word: ");
gets(inWord);
printf("%s\n", inWord);
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
int i2 = 0;
int j2 = 0;
int i3 = 0;
i2 = strcspn(inWord, outWord2);
char outWord[80];
while(i3 < i2)
{
outWord[i3] = '1';
i3++;
}
while(j2 < i2)
{
outWord[j2] = '-';
j2++;
}
puts(outWord);
while(gameover != 1 )
{
printf("What is your guess: ");
scanf("%s", &guess);
vari = 0;
if(letterfinder(inWord, guess, vari) == 100)
{
printf("Wrong!");
trys--;
printf("You have %d attempts left\n", trys);
if(trys == 0)
{
gameover = 1;
printf("You ran out of attempts. Game over\n");
}
}
else
{
outWord[(letterfinder(inWord, guess, vari) - 1)] = guess;
vari = (letterfinder(inWord, guess, vari));
while(letterfinder(inWord, guess, vari) != 100)
{
outWord[(letterfinder(inWord, guess, vari) - 1)] = guess;
vari = letterfinder(inWord, guess, vari);
}
puts(outWord);
}
int value = 0;
i3 = 0;
while( i3 <= i2)
{
if( outWord[i3] == '-')
{
value = 1;
}
i3++;
}
if(value != 1)
{
printf("Congratulations, you have guessed the word!\n");
gameover = 1;
}
}
return 0;
}
Your code has Undefined Behaviour. In the cases it "works" it is only by chance/luck. char guess; scanf("%s", &guess); That causes memory corruption as you are writing a string to a variable that can only hold a single char. Even a single letter guess will require two characters to store as all C strings are NUL terminated.
– kaylum

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