求助:指针与结构,
我有个程序,当输入 "1 Unto the hills I will left up my eyes (Line 1)"就会说出现问题需要关闭。我们对此引起的不便表示抱歉。是什么原因啊,我用的是 Borland C++ Builder Compiler V5.5编译器。源代码试这样的:#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
struct line
{
char str[40];
struct line *next;
};
int get_choice (struct line *p);
struct line *add_line(struct line *p);
struct line *del_line(struct line *p);
int main ()
{
struct line *head;
int x;
head=NULL;
x=get_choice(head);
for (;x!=3;)
{
if (x==1)
head=add_line(head);
else
head=del_line(head);
x=get_choice(head);
}
}
struct line * del_line(struct line *p)
{
struct line *p_start,*p_last;
int i,number;
cout <<"\tWhich line number?";
cin >>number;
if (number==1)
p_start=p->next;
else
{
for (p_start=p,i=1;i<number;++i,p=p->next)
p_last=p;
p_last->next=p->next;
}
free(p);
return (p_start);
}
int get_choice(struct line *p)
{
int i,choice;
cout <<"\n...This is the start of your file so far...\n";
for (i=1;p!=NULL;++i,p=p->next)
cout <<i<<" "<<p->str<<endl;
cout <<"...This is the end";
cout <<"(Enter 1 to Insert,2 to Delete,and 3 to Quit)\n\t";
cin >>choice;
return choice;
}
struct line *add_line (struct line *p)
{
int number,i;
struct line *p_last, *p_new, *p_start;
char str_new[40];
p_start=p;
p_new=(struct line *)malloc(sizeof(struct line));
cout <<"First enter the line number and then its contens:\n";
cin >>number;
cin.getline(str_new,40);
for (i=1;i<number;++i,p=p->next)
p_last=p;
p_new->next=p;
strcpy(p_new->str,str_new);
if (i==1)
return (p_new);
else
{
p_last->next=p_new;
return (p_start);
}
}