2017-12-03 12 views
-2
//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 Ring\n"<<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; 

} 

私のプログラムはこのエラーのために実行できません:[Error] expected ';' '{'トークンの前に。問題は「追加と削除の設定」の部分から始まります。問題は、そのセクションの大きなIf-Elseステートメントで、elseステートメントが始まる行にあります。私は今1時間これを見てきたし、それはイライラしているsooo!コンパイラは、 ';' 'else'ステートメント行の前にあるが、すべてが構文上有効であるように見える。私はこの問題を回避できるように、ネストされたif文のドキュメントをチェックしました。必要なヘルプ!コンパイラエラー。期待される ';'トークンの前に

+5

'else(answer ==" delete ")'は疑わしく見えます。 –

答えて

4

else(answer=="delete")else if(answer=="delete")する必要があり、C++はあなたが別の状態を確認したい場合はelse後に新しいifブロックを開始する必要がありますので、特別なelifのフォームを持っていません。

比較演算子の左辺に("delete" == answer)のようにリテラルを書き込むことをお勧めします。これにより、誤った割り当ての誤植を簡単に検出できます。

+0

私は通常このような複雑なif文を使用しません。複雑になると、常にステートメントを切り替えます。ちょうど私がこのプログラムでどのように使うことができるか分かりませんでした。ああ、多分次回、感謝します。 – eagergrammer

関連する問題