2017-12-03 1 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 ';' '{'토큰 앞에. 문제는 '추가 및 삭제 설정'부분에서 시작됩니다. 문제는 else 절이 시작되는 행의 해당 섹션에있는 큰 If-Else 문입니다. 나는 1 시간 동안 이것을 이제까지보고 있었다. 그리고 그것은 초조하게 sooo하다! 컴파일러는 ';' 'else'명령문 행 앞에 있지만 모든 것이 구문에 현명한 것처럼 보입니다. 중첩 된 if 문에 대한 설명서를 확인하여이 문제를 피할 수있었습니다. 도움이 필요하다! 당신은 당신이 다른 상태를 확인하려면 else 후 새 if 블록을 시작해야하므로컴파일러 오류입니다. 예상 ';' 토큰 전

+5

'else (answer == "delete")'의심스러운 것으로 보입니다. –

답변

4

else(answer=="delete")else if(answer=="delete")을해야한다, C++ 특별한 elif 양식을 가지고 있지 않습니다.

("delete" == answer)과 같이 비교 연산자의 왼쪽에 리터럴을 쓰는 것도 좋은 생각입니다. 이렇게하면 우발적 인 오타를 쉽게 감지 할 수 있습니다.

+0

나는 보통 이와 같은 복잡한 if 문을 사용하지 않습니다. 복잡해지면 항상 문장을 전환하십시오. 이 프로그램에서 내가 어떻게 사용할 수 있는지 보지 못했습니다. 오 그럼 아마도 다음 번에 고마워. – eagergrammer