简体   繁体   中英

Optional Argument from a User C++

I'm making a command-line word editor program. The user is prompted to input a control character to make a change to the file. I'm having trouble with command 'D' which deletes either a single line of text, or a range of text.

input D:
        D 3       --deletes line 3
        D 2 8     --deletes lines 2 to 8 inclusively 

How do you make it so that the second line is optional? I have cin << char << int << int, but I can't find a way to make that optional.

Do

std::string line; 
std::getline(std::cin,line);

and then analyze the line manually, first splitting it into words.

It could be useful to have a function:

void ToWords(const std::string &line, std::vector<std::string> &words);

But the implementation is left as an exercise to the reader ;-).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM