Question: Need help with string input and compare in C I am trying to implement a initBook function but when i try to input a space
Need help with string input and compare in C
I am trying to implement a initBook function but when i try to input a space between characters of the string i input the printf function will go crazy. And I could not use strcmp() function to compare two strings with a space inside.
And the sample output should looks like
But when i try to input more than one book my program will show 
int main(void){
while(returnvalue != -1){ printf("Please Enter Title, Author and year accordingly (Enter end to stop): "); returnvalue=initBook(&book); if(strlen(book->author) == 0 && strlen(book->title) == 0 ){ }else{ addByTitle(booksByTitle,book); } }
}
int initBook(BookType **book){ *book = (BookType*)calloc(1,sizeof(BookType)); char title[MAX_STR]={'\0'}; scanf("%[^ ]%*c",title); // scanf("%s",title); if(strcmp(title,"end")==0){ return C_NOK; } scanf("%[^ ]%*c",(*book)->author); // scanf("%s",(*book)->author); scanf("%d", &(*book)->year); strcpy((*book)->title,title); return C_OK; }
You will write a book initialization function with the prototype int initBook (BookType **book) This function will prompt the user for a book title, author, and year. If the user enters "end" for a book title, the function will return a failure code. If a different string is entered for a title, the user will be prompted for the author and year, then a book structure will be dynamically allocated and initialized with the data entered. The new book will be returned using the function's output parameter book, and success will be returned as the return value
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
