Функция добавления в двусвязный список
При добавлении номера сохраняется какой-то мусор, в чем может быть ошибка?
#include<iostream>
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
struct List
{
char name[11];
char surname[21];
char number[13];
bool flag;
List*prev;
List*next;
};
List*begin=NULL;
List*end=NULL;
List* check(List*tmp)
{
char number[13];
printf("Enter the name,please\n");
scanf("%11s",tmp->name);
while(strlen(tmp->name)>11)
{
printf("\nYou type incorrect name");
scanf("%11s",tmp->name);
}
printf("\nEnter the surname,please\n");
scanf("%21s",tmp->surname);
while(strlen(tmp->surname)>21)
{
printf("\nYou type incorrect surname\n");
scanf("%21s",tmp->surname);
}
printf("\nEnter the phone number,please\n");
bool ok;
do
{
scanf("%13s",number);
ok=true;
for(int j=0;j<strlen(number);++j)
{
if(isdigit(number[j])==0)
{
printf("Don't type number like this!vvedite nomer! \n");
ok=false;
}
}
}while(strlen(number)>12 || ok==false);
for(List*tmp=begin;tmp!=NULL;tmp=tmp->next)
{
if(strcmp(number,tmp->number)!=0)
{
strcpy(tmp->number,number);
}
else
{
printf("Takoyi nomer est' \n");
break;
}
}
return tmp;
}
void add()
{
List*tmp=(List*)malloc(sizeof(List));
if(tmp==0)
{
printf("Not good!\n");
exit(1);
}
if(begin==NULL && end==NULL)
{
tmp=check(tmp);
if(tmp!=false)
{
tmp->flag=true;
printf("\nYour register succesfully added\n");
tmp->next=0;
tmp->prev=0;
begin=end=tmp;
}
}
if(end==0)
{
tmp=check(tmp);
if(tmp!=false)
{
tmp->flag=true;
printf("\nYour register succesfully added\n");
tmp->prev=end;
tmp->next=0;
end->next=tmp;
end=tmp;
}
}
}
void show()
{
for(List*tmp=begin;tmp!=NULL;tmp=tmp->next)
{
if(tmp->flag==true)
{
printf("Name:%11s",tmp->name);
printf("\nSurname:%21s",tmp->surname);
printf("\nNumber:%13s",tmp->number);
printf("\n");
}
}
}
int main()
{
add();
show();
system("pause");
return 0;
}
Источник: Stack Overflow на русском