Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional Argument from a User C++

Tags:

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.

like image 315
dajee Avatar asked Dec 06 '25 09:12

dajee


1 Answers

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 ;-).

like image 87
rodrigo Avatar answered Dec 11 '25 19:12

rodrigo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!