I'm attempting to print a string from a linked list..for some reason it is outputting random symbols and characters.
Here is my code:
int main(){
char c;
int titleCount;
int authorCount;
char bookTitle[35];
char author[35];
/* Create Struct */
typedef struct bookData{
char bookTitle[35];
char author[35];
int book_stock;
float retail_price;
int wholesale_purchased;
int customer_purchased;
struct bookData *book;
}new_book;
/*Create Node */
typedef struct Node{
new_book books;
struct Node *next;
}node_t;
/* We are GUARANTEED at least 1 input value, so go ahead and initialize
the
head */
node_t *head = NULL; /*Initalize head to NULL since it is empty */
head = malloc(sizeof(node_t));
if(head == NULL){
printf("allocation failed");
return 0;
}
head -> next = NULL;
/*Memory allocation successful */
/*Might as well populate the head with data from the user */
titleCount = 0;
authorCount = 0;
printf("Enter Title\n");
while(( c = getchar()) != '\n'){
bookTitle[titleCount++] = c;
}
bookTitle[titleCount] = '\0';
printf("%s", bookTitle);
strcpy((head -> books).bookTitle,bookTitle);
printf("Enter Author\n");
while(( c = getchar()) != '\n'){
author[authorCount++] = c;
}
author[authorCount] = '\0';
strcpy((head -> books).author, author);
printf("Bookstock #:\n");
scanf("%d", &(head -> books).book_stock);
printf("Enter retail price $:\n");
scanf("%f", &(head -> books).retail_price);
printf("Enter Wholesale purchased quanity:\n");
scanf("%d", &(head -> books).wholesale_purchased);
printf("Enter quantity sold:\n");
scanf("%d", &(head -> books).customer_purchased);
printf("%c\n", head -> books.bookTitle);
printf("%c\n", head -> books.author);
printf("%d\n", head -> books.book_stock);
printf("%.2f\n", head -> books.retail_price);
printf("%d\n", head -> books.wholesale_purchased);
printf("%d\n", head -> books.customer_purchased);
}
And my output as follows:
I'm forced to use char
array of 35
so there is no getting around that. I'm positive that the pointer calls are correct as that seems to be what all the answers related say.
Thanks