该程序是一个链接列表程序,所有运行在我必须对该程序进行一些重大更改之前。调试程序时,一切顺利,直到出现此错误的代码的最后一行:
| 175 |错误:在输入末尾预期'}'|
只想看看代码的主要问题是什么。
我试图单独隔开程序,以查看是否错过了某个地方的输入,但是找不到任何错误。
#include<iostream>
using namespace std;
class node
{
public:
string name,id;
int age;
node *next;
void add();
void takeout();
void look();
void display();
node(string n,string i,int a)
{
name=n;
id=i;
age=a;
next=NULL;
}
};
class Linkedlist{
private:
node *list;
public:
Linkedlist(){
list = NULL;
}
void add() // adds the information for the linked list
{
string s,i;
int a;
cout << "\nEnter the student's information below.\n";
cout << "Student's Name : ";
cin >> s;
cout << "Student's ID : ";
cin >> i;
cout << "Student's Age : ";
cin >> a;
node *t = new node(s,i,a);
//adding front
t->next=list;
list=t;
}
void takeout()
{
string a;
cout << "\nEnter the Student's ID so their info can be removed. \n";
cout << "Student's ID : ";
cin >> a;
node *prev=NULL,*temp=list;
while(temp!=NULL)
{
if(temp->id==a)
{
cout<<"Found.\n";
break;
}
prev=temp;
temp=temp->next;
}
if(temp==NULL)
{
cout<<"\nInformation not found.\n";
}
else if(prev==NULL)
{
cout<<"Delinked1\n";
list=list->next;
}
else
{
cout<<"Delinked2\n";
prev->next=temp->next;
}
}
void look()
{
string a;
cout << "\nEnter Student's ID to search for their information.\n ";
cout << "Student's ID : ";
cin >> a;
node *temp=list;
while(temp!=NULL)
{
if(temp->id==a)
{
//diplyaing result
cout << "\n------------------------------\n";
cout << "Name :" << temp->name << endl;
cout << "ID :" << temp->id << endl;
cout << "Age :" << temp->age << endl;
cout << "\n------------------------------\n";
break;
}
temp=temp->next;
}
if(temp==NULL)
{
cout << "\nInformation on found.\n";
}
}
void display()
{
node *temp=list;
if(temp==NULL)
cout<<"\nLIST IS EMPTY---\n";
while(temp!=NULL)
{
//displaying
cout << "\n------------------------------\n";
cout << "Name :" << temp->name << endl;
cout << "Age :" << temp->age << endl;
cout << "\n------------------------------\n";
temp=temp->next;
}
}
int main()
{
Linkedlist list;
cout << "Student Information (Linked List)\n\n";
while(1)
{
int choice;
cout << "\n1. Add a node." << endl;
cout << "2. Delete a node." << endl;
cout << "3. Search a node." << endl;
cout << "4. Display a node." << endl;
cout << "Enter your choice : ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
{
list.add();
break;
}
case 2:
{
list.takeout();
break;
}
case 3:
{
list.look();
break;
}
case 4:
{
list.display();
break;
}
default:
cout << "Wrong choice!" << endl;
break;
}
}
}
我知道代码的末尾应该显示什么,因为我在不得不更改输入之前就运行了它,但是现在由于这个错误,程序无法运行。
答案 0 :(得分:2)
class Linkedlist{
private:
node *list;
public:
Linkedlist(){
list = NULL;
}
糟糕!...没有结束'}'
尽管没有任何要求,但我总是发现提供一致的格式,在表达式和大括号之间留有空格,这有助于我快速扫描文件并确认大括号平衡,例如
class Linkedlist {
private:
node *list;
public:
Linkedlist() {
list = NULL;
}
...
}; /* <== this is the missing closing-brace in your code */
实际上,您遇到的情况是您缺少该类的闭括号,该类包括下面的所有成员函数,例如
class Linkedlist {
private:
node *list;
public:
Linkedlist() {
list = NULL;
}
void add() // adds the information for the linked list
{
...
void display()
{
node *temp=list;
if(temp==NULL)
cout<<"\nLIST IS EMPTY---\n";
while(temp!=NULL)
{
//displaying
cout << "\n------------------------------\n";
cout << "Name :" << temp->name << endl;
cout << "Age :" << temp->age << endl;
cout << "\n------------------------------\n";
temp=temp->next;
}
}
}; /* <== this is the actual missing closing-brace in your code */
一致的缩进将有助于识别该问题。添加结束'}'
后,您的代码才编译。
在菜单上添加一个内容也很不错:
1. Add a node.
2. Delete a node.
3. Search a node.
4. Display a node.
5. Exit. // exit would be nice
Enter your choice :