what is the correct way to handle multiple input command differently in c++?

前端 未结 5 654
深忆病人
深忆病人 2021-01-12 16:24

I have a program that take commands from user and it will process different commands differently. For example:

ADD_STUDENT ALEX 5.11 175
ADD_TEACHER MERY 5.4         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-12 16:52

    Do it like so:

    iss >> command;
    if (!iss)
        cout << "error: can not read command\n";
    else if (command == "ADD_STUDENT")  
        iss >> name >> height >> weight;
    else if (command == "ADD_TEACHER")  
        iss >> name >> height >> weight >> salary;
    else if ...
    

提交回复
热议问题