As you can see this is my program.my problem is how to store strings in my program
I can store only single character..
any one can help me?
=(


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define p printf
#define s scanf
struct node
{
char nbook;
struct node *next;
};
typedef struct node *nodepointer;
void addbook(nodepointer &head,char book)
{
nodepointer newnode;
newnode=(nodepointer)malloc(sizeof(struct node));
newnode->nbook=book;
newnode->next=head;
head=newnode;
}
void display(nodepointer head)
{
if(head==NULL)
{
p("\nNothing to display");
}
else if(head!=NULL)
{
while(head->next!=NULL)
{
p("\n%c",head->nbook);
head=head->next;
}
p("\n%c",head->nbook);
}
}
main()
{
nodepointer head=NULL;
int choice;
char book;
do
{
p("\n[1]Add book");
p("\n[2]Display book");
p("\nKey choice: ");
s("%i",&choice);
switch(choice)
{
case 1:
p("\nEnter title of book");
s("\n%c",&book);
addbook(head,book);
break;
case 2:
display(head);
break;
}
}while(choice!=4);
system("pause");
}

You can only store a single character because in your node structure

struct node
{
 char nbook;
 struct node *next;
};

You only provide space to store a single character. You need an array of characters if you want to store a whole string.

Can you pls help me to understand what you are saying can u reprogram what i wrote? imean the declaration only. i use char *nbook already

Every place in your code that you have used char book; only holds a single character but you want to hold a title which is multiple characters so you need not a single character but an array of characters char book[50]; in all those places.

You will need to use %s in scanf instead of %c and you will need to use function like strcpy instead of just using =

YOU MEAN LIKE THIS???
WHENEVER I enter Book always set to null >_<

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define p printf
#define s scanf
struct node
{
char nbook;
struct node *next;
};
typedef struct node *nodepointer;
void addbook(nodepointer &head,char book[50])
{
nodepointer newnode;
newnode=(nodepointer)malloc(sizeof(struct node));
newnode->nbook=book[50];
newnode->next=head;
head=newnode;
}
void display(nodepointer head)
{
if(head==NULL)
{
p("\nNothing to display");
}
else if(head!=NULL)
{
while(head->next!=NULL)
{
p("\n%s",head->nbook);
head=head->next;
}
p("\n%s",head->nbook);
}
}
main()
{
nodepointer head=NULL;
int choice;
char book[50];
do
{
p("\n[1]Add book");
p("\n[2]Display book");
p("\nKey choice: ");
s("%i",&choice);
switch(choice)
{
case 1:
p("\nEnter title of book");
s("\n%s",&book);
addbook(head,book);
break;
case 2:
display(head);
break;
}
}while(choice!=4);
system("pause");
}

Banfa help me please.>_<

s("\n%s",&book); Because char book[50] is an array and book returns a pointer to the first item in the array. That means that when used in scanf you don't need to take its address as it is already an address so the above line should be s("\n%s",book); In your node structure you need to change the declaration of nbook to be an array as well.

and in

void addbook(nodepointer &head,char book[50])
{
  nodepointer newnode;
  newnode=(nodepointer)malloc(sizeof(struct node));
  newnode->nbook=book[50];
  newnode->next=head;
  head=newnode;
}

At line 5 the [50] is a syntax error you just need to refer to the parameter by name. Also you can't assign an array using =, as I said before at this line you need to make a call to the standard library function strcpy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.