I'm trying to do a library
the books is the objectives
the user can enter a book and then I check if the book is my book menu:
if the book in the menu:
if the book is available so I print a message and return 1
if it doesn't available so I change the book to available and print that I added the book
if the book is not in the menu :
I do malloc and then check I the malloc succeed
if the malloc succeed :I do strcp to the object
if the malloc didn't succeed : I do free to the object and the print a message and return 1
The problem: when the user enter the book for the second time it shouldn't add the book as a new book! it should check if the book is available or not and then return a message but my code is not doing this and I don't know where is bug!
#define _CRT_SECURE_NO_WARNINGS
#define BOOK_NUM 4
#define NAME_LENGTH 200
#define AVAILABLE 10
#define NOT_AVAILABLE 20
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct book
{
char name[NAME_LENGTH]; char author[NAME_LENGTH]; int available; int times_borrowed;
}Book;
int main()
{
Book *books[BOOK_NUM] = { 0 };
char book_name[NAME_LENGTH];
char author_name[NAME_LENGTH];
int opreation = 0;
int i;
int j = 0;
int m = 0;
char tav;
scanf("%d", &opreation);
if (opreation == 1) {
printf("please enter the name:");
scanf("%c", &tav);
do {// kelet of the book_name
scanf("%c", &tav);
if (tav == '\n')
break;
book_name[m] = tav;
m++;
} while (m < NAME_LENGTH);
book_name[m] = '\0';
for (i = 0; i < BOOK_NUM && *(books+i)!=NULL ; i++) {
if (strcmp(*books[i]->name, book_name) == 0)
{
if (books[i]->available = NOT_AVAILABLE)
{
books[i]->available = AVAILABLE;
printf("This book is already in the library");
return 0;
}
else
{
printf("There is no enough space in the library");
return 0;
}
}
}
//befot bs eza 3ml sreka ghad 3la kolshe w ma tghyr eshe
for (j; j < BOOK_NUM; j++) {
if (books[j] == NULL)
{
books[j] = (Book*)malloc(sizeof(Book));
if (books[j] != NULL)
{
strcpy(books[j]->name, book_name);
printf("Please enter author name:");
m = 0;
do {// kelet of the book_name
scanf("%c", &tav);
if (tav == '\n')
break;
author_name[m] = tav;
m++;
} while (m < NAME_LENGTH);
author_name[m] = '\0';
strcpy(books[j]->author, author_name);
books[j]->available = AVAILABLE;
books[j]->times_borrowed = 0;
printf("The book %s was successfully added!", book_name);
return 0;
}
else
{
for (int k = 0; k < BOOK_NUM && books[k]!=NULL; k++) {
free(books[k]);
}
printf("NO MEMORY");
return 1;
}
}
}
}
}
MAYBE now is better:
#define _CRT_SECURE_NO_WARNINGS
#define BOOK_NUM 4
#define NAME_LENGTH 200
#define AVAILABLE 10
#define NOT_AVAILABLE 20
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct book
{
char name[NAME_LENGTH]; char author[NAME_LENGTH]; int available; int times_borrowed;
}Book;
int main()
{
Book *books[BOOK_NUM] = { 0 };
char book_name[NAME_LENGTH];
char author_name[NAME_LENGTH];
int opreation = 0;
int i;
int j = 0;
int m = 0;
char tav;
scanf("%d", &opreation);
if (opreation == 1) {
printf("please enter the name:");
scanf("%c", &tav);
do {// kelet of the book_name
scanf("%c", &tav);
if (tav == '\n')
break;
book_name[m] = tav;
m++;
} while (m < NAME_LENGTH);
book_name[m] = '\0';
for (i = 0; i < BOOK_NUM && *(books+i)!=NULL ; i++) {
if (strcmp(books[i]->name, book_name) == 0)
{
if (books[i]->available == NOT_AVAILABLE)
{
books[i]->available = AVAILABLE;
printf("This book is already in the library");
return 0;
}
else
{
printf("There is no enough space in the library");
return 0;
}
}
}
//befot bs eza 3ml sreka ghad 3la kolshe w ma tghyr eshe
if (books[j] == NULL)
{
books[j] = (Book*)malloc(sizeof(Book));
if (books[j] != NULL)
{
strcpy(books[j]->name, book_name);
printf("Please enter author name:");
m = 0;
do {// kelet of the book_name
scanf("%c", &tav);
if (tav == '\n')
break;
author_name[m] = tav;
m++;
} while (m < NAME_LENGTH);
author_name[m] = '\0';
strcpy(books[j]->author, author_name);
books[j]->available = AVAILABLE;
books[j]->times_borrowed = 0;
printf("The book %s was successfully added!", book_name);
return 0;
}
else
{
for (int k = 0; k < BOOK_NUM && books[k]!=NULL; k++) {
free(books[k]);
}
printf("NO MEMORY");
return 1;
}
}
}
}
by the way, you should listen to compiler warnings.
Related
#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;
}
I'm doing the day 8 of the 30 days of code in HackerRank and I am having a problem with strcmp.
The code asks the user for names of people and their numbers, then asks for other names, if a name wasn't entered before, then it outputs Not found, but if it was then it outputs the name and his number. But for some reason, the output only works in the last loop of the for statement.
Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
typedef struct {
char name[100];
int number;
} phonebook;
int main() {
int n = 0;
do {
scanf("%i", &n);
} while (n < 1 || n > 100000);
int i = 0;
phonebook people[n];
for (i = 0; i < n; i++) {
scanf("%s %i", people[i].name, &people[i].number);
}
char othernames[n][100];
for (i = 0; i < n; i++) {
scanf("%s", othernames[i]);
}
for (i = 0; i < n; i++) {
if (strcmp(othernames[i], people[i].name) == 0) {
printf("%s=%i\n", people[i].name, people[i].number);
} else {
printf("Not found\n");
}
}
return 0;
}
You didn't find the othernames from the beginning to end to compare peopleevery time, so you need to replace
for (i = 0; i < n; i++) {
if (strcmp(othernames[i], people[i].name) == 0) {
printf("%s=%i\n", people[i].name, people[i].number);
}
else {
printf("Not found\n");
}
}
to
bool found = false;
for (i = 0; i < n; i++) {
for ( j = 0 ; j < n ; j++ ) {
if (strcmp(othernames[j], people[i].name) == 0) {
printf("%s=%i\n", people[i].name, people[i].number);
found = true;
}
}
}
if ( found == false ) printf("Not found\n");
The problem is othernames should just be an array of char, not a matrix. And for each othername entered, you must scan whole phonebook to find it or display Not found. As coded, you only test if the i-th othername typed happens to correspond to the i-th entry in the phone book.
Here is a modified version:
#include <stdio.h>
#include <string.h>
typedef struct {
char name[100];
int number;
} phonebook;
int main() {
int n = 0;
do {
if (scanf("%i", &n) != 1)
return 1;
} while (n < 1 || n > 100000);
phonebook people[n];
for (int i = 0; i < n; i++) {
if (scanf("%99s %i", people[i].name, &people[i].number) != 2)
return 1;
}
for (int i = 0; i < n; i++) {
char othername[100];
if (scanf("%99s", othername) != 1)
break;
int j;
for (j = 0; j < n; j++) {
if (strcmp(othername, people[j].name) == 0) {
printf("%s=%i\n", people[i].name, people[i].number);
break;
}
}
if (j == n) {
printf("Not found\n");
}
}
return 0;
}
Note that it is probably not a good idea to store phone numbers as int values. Better use a char array so an initial 0 is significant and to store longer numbers.
Below is a program called scrabble.c that is being used to play a simplified version of the game. The user is dealt 7 random characters and then told to enter a word using those characters. The program should check if the user then uses the letters given in order to create a valid word. I am having a problem with the function that checks to see if the user enters a valid word.
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#define SIZE_LETTER_SET 100
#define SIZE_CHOOSEN_LETTER 7
void generate_letter_set(int letter_set[7], int size_letter_set, int
num_letters)
{
//num_letters should be 7
//size_letter_set should be 100
bool choosen[SIZE_LETTER_SET] = {false};
int group[SIZE_LETTER_SET] =
{'A','A','A','A','A','A','A','A','A','B','B','C','C','D','D','D','D','E','E','E','E','E','E','E','E','E','E','E','E','F','F','G','G','G','H','H',
'I','I','I','I','I','I','I','I','I','J','K','L','L','L','L','M','M','N','N','N','N','N','N','O','O','O','O','O','O','O','O','P','P','Q','R','R','R','R','R','R','S','S',
'S','S','T','T','T','T','T','T','U','U','U','U','V','V','W','W','X','Y','Y','Z',' ',' '};
int c, letter, letterfound;
srand((unsigned) time(NULL));
for(c = 0; c < num_letters; c++)
{
letterfound = 1;
while(letterfound % 2 != 0)
{
letter = rand() % 100;
if(choosen[letter] == false)
{
letter_set[c] = group[letter];
choosen[letter] = true;
letterfound++;
}
}
}
printf("Your letters are: ");
for(c = 0; c < num_letters; c++)
{
printf("%c ", letter_set[c]);
}
printf("\n");
return;
}
int read_word(char word[], int max_size_word)
{
int c = 0, input_count = 0;
printf("Please enter your word : ");
char user_input = getchar();
for(c = 0; c < max_size_word; c++)
{
if(user_input != '\n')
{
word[c] = user_input;
input_count++;
//printf("input_count = %d, letter entered = %c\n", input_count,
user_input);
}
else if(user_input == '\n')
{
return input_count;
}
user_input = getchar();
}
return input_count;
}
This function is where the error occurs, the function should check if the user is using their 7 letters to create a valid word.
_Bool check_word (char word[], int size_word, int letter_set[], int
size_letter_set)
{
printf("beginning word check : \n");
int c = 0;
int b = 0;
int dup_count = 0;
int pair_found = 0;
for(c = 0; c < size_word; c++);
pair_found = 1;
for(b = 0; b < size_letter_set; b++)
{
printf("Checking if %c is equal to %c", word[c], letter_set[b]);
if((toupper(word[c])) == letter_set[b])
{
letter_set[b] = 0;
dup_count++;
pair_found++;
}
}
printf("Total duplicates = %d : check word is ", dup_count);
if((dup_count >= size_word))
{
printf("true");
return true;
}
else
{
printf("false");
return false;
}
}
int main(void)
{
int letter_set[7] = {0};
char word[7] = {0};
int size_letter_set = 100;
int num_letters = 7;
generate_letter_set(letter_set, size_letter_set, num_letters);
int size_word = read_word(word, num_letters);
check_word(word, num_letters, letter_set, size_letter_set);
return 0;
}
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
I am trying have a program end when the user hits the Enter key. For some reason it doesn't seem to work. When I use "char c is not equal to enter key" it takes in an extra integer in c (the last inputted integer). What is the problem with this code?
#include <stdio.h>
#include <stdlib.h>
#define framenumber 4
int test1 =0;
int test2=1;
int main(void)
{
int mainarray[framenumber][2] = {0}, nHP = 3, takein, iPT;
char c = getchar();
printf("Enter: ");
while(1)
{
char c = getchar();
if(c == '\n') {
printf("here");
}
else
{
printf("not enter\n");
takein = atoi(&c);
for (iPT = 0; mainarray[iPT][test2] != takein && iPT < framenumber; iPT++);
if (mainarray[iPT][test2] != takein)
{
//search for a victim
do {
nHP = (nHP + 1) % framenumber;
} while ( !( mainarray[nHP][test1] == 1 ? mainarray[nHP][test1] = 0 : 1 ) );
//update the page table
mainarray[nHP][test1] = 1;
mainarray[nHP][test2] = takein;
}
else
{
mainarray[iPT][test1] = 1;
}
puts("page table:");
for (iPT = 0; iPT < framenumber; iPT++)
{
printf("%s %d, %d.\n", iPT == (nHP + 1) % 4 ? ">": " ", mainarray[iPT][test1], mainarray[iPT][test2]);
}
putchar('\n');
printf("Enter: ");
}
}
return 0;
}
Do not create block variable. (In while loop).
char c='\0'; /* initialize with 0 */
printf("Enter: ");
while(c!='\n') /* loop terminate condition */
{
c= getchar(); /* remove declaration */
if(c =='\n')
{
printf("here");
}
else
{
getchar(); /* read (eat) an extra input */
printf("not enter\n");
....