0

この質問を読んでくれてありがとう。

基本的に、私は次のことを達成できるコードを実行しようとしています:

ユーザーには、このような詳細のリストが表示されます

ターミナル ビュー:

Please select the department you want to add participant: 
1. Admin 
2. HR 
3. Normal 
4. Back to Main Menu 

Selection: 3
normal's Department
 UserID: 85 [ Name: Andrew, Department:  normal ]
 UserID: 86 [ Name: Jacky, Department:  normal ]
 UserID: 90 [ Name: Baoky, Department:  normal ]

Current Selected Participant : 

Usage: 
Type exit to return to main menu
Type remove userid to remove participant
Type add userid to add participant

Selection: 

質問: ユーザーがメイン メニューに「終了」するまで好きなだけ参加者を追加できるようにしたいのですが、それを文字列参加者に保存するにはどうすればよいですか。

ユーザー入力が「ユーザー ID の削除」または「ユーザー ID の追加」であることを検出し、ユーザー ID を取得する方法

例: 86 を足してから 90 を足す

それから彼は90を削除することにしました

弦はどのようにそれに追いつくのですか

以下は私のコードです:

do
{
cout << "Current Selected Participant : " << participant << endl; 
cout << "" << endl;

do
{
if(counter>0)
{
//so it wont print twice
cout << "Usage: " << endl; 
cout << "Type exit to return to main menu" << endl;
cout << "Type remove userid to remove participant" << endl;
cout << "Type add userid to add participant" << endl;
cout << "" << endl;
cout << "Selection: ";
}

getline(cin,buffer);
counter++;
}while(buffer=="");




if(buffer.find("remove"))
{
str2 = "remove ";
buffer.replace(buffer.find(str2),str2.length(),"");

if(participant.find(buffer))
{
//see if buffer is in participant list
buffer = buffer + ",";
participant.replace(participant.find(buffer),buffer.length(),"");
}
else
{
cout << "There no participant " << buffer << " in the list " << endl;
}
}//buffer find remove keyword


if(buffer=="exit")
{
done=true;
}
else
{
sendToServer = "check_account#"+buffer;

write (clientFd, sendToServer.c_str(), strlen (sendToServer.c_str()) + 1);
//see if server return found or not found
readFromServer = readServer (clientFd);

if(readFromServer=="found")
{
//add to participant list
participant += buffer;
participant += ",";
}

}//end if not exit

}while(done!=true);

一部のユーザーは、文字列セットに保存する方法、文字列セットに保存する方法、端末が選択範囲で「削除」や「追加」などのキーワードを認識できるようにする方法を提案しています

次に、空白で区切られたユーザー ID を取得します。

次は、文字列セットに保存する場合の削除方法と、新しい値のプッシュ方法です。

4

1 に答える 1

1

文字列に格納しないでください。のように、簡単に挿入および削除できるコレクションに格納しますstd::set<int>。プロセスが終了したら、セットを必要な表現に変換できます。

以下は非常に単純な例です (コンパイルして実行できるかどうかはチェックしていません。これは読者の演習として残しています!)

void handle_command(const std::string& command, std::set<std::string>& userids)
{
    if (command.substr(0, 4) == "add ")
    {
        std::string uid = command.substr(4);

        if (userids.find(uid) == userids.end())
            userids.insert(uid);
        else
            std::cout << "Uid already added" << std::endl;

        return;
    }
    else
        throw std::exception("Unsupported command, etc");
}
于 2012-08-16T11:23:45.093 に答える