I have a function that receives the integer values :
void link::add(int val)
{
node1 *q,*temp;
q = new node1;
if(node==NULL)
{
node = new node1;
node->data=val;
node->next=NULL;
}
else
{
q=node;
while(q->next != NULL)
{
q = q->next;
}
temp = new node1;
temp->data=val;
temp->next=NULL;
q->next = temp;
}
temp=node;
while(temp!=NULL)
{
cout<<temp->data<<" -> ";
temp=temp->next;
}
}
but if the user inputs a character instead of an int, the programms goes into infinite loop:
above function is called as:
link list; //object of link class
int val;
cin>>val;
list.add(val); //call above funciton
any ideas what to do...
void link::add(int val)
{
node1 *q,*temp;
q = new node1;
if(node==NULL)
{
node = new node1;
node->data=val;
node->next=NULL;
}
else
{
q=node;
while(q->next != NULL)
{
q = q->next;
}
temp = new node1;
temp->data=val;
temp->next=NULL;
q->next = temp;
}
temp=node;
while(temp!=NULL)
{
cout<<temp->data<<" -> ";
temp=temp->next;
}
}
but if the user inputs a character instead of an int, the programms goes into infinite loop:
above function is called as:
link list; //object of link class
int val;
cin>>val;
list.add(val); //call above funciton
any ideas what to do...