编译器错误。令牌之前的预期';'
Compiler Error. Expected ';' Before Token
//this application implements use of a simple set
//program does data insertion,searching, and deletion
//program will represent a set of keys
//a user can add a key, delete a key, and check for duplicate keys
#include<iostream>
#include<set>
#include<string>
using namespace std;
int main()
{
string key, answer,answer2;
int i;
std::set<std::string> Keyring;
//prompts user to insert elements in set
for(i = 0; i < 5; i++)
{
cout<<"nInsert Key " <<i+1<<" Onto Key Ring. Use _ For Spaces"<<endl;
cin >>key;
Keyring.insert(key);
}
//shows resulting key ring
cout<<"nHere is Completed Key Ringn"<<endl;
for(std::set<std::string>::iterator it=Keyring.begin(); it !=Keyring.end(); it++) //the '<' operator keeps UNIQUE elements in sorted order
{
std::cout<<" "<<"n"<< *it;
}
//Set Deletion and Addition
cout<<"nWould You Like to Add or Delete From Key Ring?"<<endl;
cout<<"nType add or delete"<<endl;
cin>>answer;
if(answer=="add")
{
cout<<"nAdd Another Key. Use _ For Spaces"<<endl;
cin>>key;
Keyring.insert(key);
if(Keyring.insert(key).second) //checks to see if key already present in set
{
cout<<key<<" Successful Addition!"<<endl;
}
else if(!Keyring.insert(key).second)
{
cout<<" Duplicate Key Can't Be Added!"<<endl;
}
}
else(answer=="delete")
{
cout<<"nDelete A Key. Use _ For Spaces"<<endl;
cin>>key;
Keyring.erase(key);
cout<<"nNew Keyring"<<endl;
}
for(std::set<std::string>::iterator it=Keyring.begin(); it !=Keyring.end(); it++)
{
std::cout<<" "<<"n"<< *it;
}
cout<<"nKeyring size is "<<Keyring.size()<<endl;
//Set Searching
cout<<"nWant To Search For A Key y/n?"<<endl;
cin>>answer2;
if(answer2=="y")
{
cout<<"nSearch For A Key. Use _ For Spaces"<<endl;
cout<<"nWhere Is My..."<<endl;
cin>>key; cout<<" Key?"<<endl;
}
std::set<std::string>::iterator it=Keyring.find(key);
if(it !=Keyring.end())
{
cout<<key<<"nFound!"<<endl;
}
else
{
cout<<key<<"nNot Found :("<<endl;
}
return 0;
}
我由于这个错误而无法运行程序:[错误]预期';'在" {"令牌之前。问题始于"集合添加和删除"部分。问题在于该部分中的大型IF-ELSE语句,在其他语句启动的地方。我一直在寻找一个小时,令人沮丧!编译器说应该有一个';在"否则"语句行之前,一切都很好。我检查了嵌套的文档,以便避免此问题。需要帮助!
else(answer=="delete")
应该为 else if(answer=="delete")
,C 没有任何特殊的elif
表格,因此如果您想检查另一个条件,则需要在else
之后启动新的if
块。
在比较操作员(例如("delete" == answer)
)的左侧写文字也是一个好主意,这样您就可以轻松地检测到意外分配错字。
相关文章:
- holeMenuProgram.cpp:38:1 错误:'}'令牌之前的预期主表达式
- Arduino 用于语句错误。令牌之前的预期')' ';'。如何解决这个问题?
- 错误:令牌 { '{' 之前应存在非限定 ID
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- (错误:令牌之前'<<'预期的主表达式)
- 错误:"->"令牌之前的预期初始值设定项
- LINUX 操作系统上的错误:令牌之前预期的构造函数、析构函数或类型转换'('?
- 错误:C++中']'令牌之前的预期主表达式
- 错误:令牌之前的预期主表达式'&&'
- 令牌 C++ 错误之前'['非限定 ID
- 错误:令牌之前的预期主表达式'/'
- QT:错误 ["*"令牌之前的预期")",并且主窗口未命名类型]
- C 补充错误:“ =”令牌之前的预期初始化器
- '&lt;'''&lt;'''&quot的模板类错误''令牌”
- 模板错误 令牌之前的预期主表达式'>'
- C++错误 "'"令牌之前的预期主表达式(在 ifstream 上使用 getline?
- C++ 错误 - 令牌之前的预期主表达式'.'|
- 错误:')'令牌强制转换问题之前期望的主表达式
- 错误:':'令牌之前期望的初始化项
- G++ 错误 - 令牌之前')'预期非限定 ID