How do I read a command and a user specified string together?

My program gives the user a list of options such as "help", "exit", "create file", etc. If the user chooses to create a file, the user will write "create [insert user's desired filename here]", and then hit enter. The program then calls the create function and sends only the string filename as a parameter. How can I achieve this? https://19216801.onl/
Last edited on
What have you done so far?

You'll need to show the list of options, read the user selection, then act on it.
It's called parsing. std::stringstream is handy for parsing.

Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <sstream>
#include <string>
#include <iostream>
using namespace std;

void do_create(string& filename)
{	//	create file 
}

int main()
{
	string	line;
	string	cmd;
	string	filename;

	while (getline(cin, line))
	{
		stringstream	ss;
		ss >> cmd;
		if (cmd == "CREATE")
		{
			ss >> filename;
			do_create(filename);
		}
	}
}

Topic archived. No new replies allowed.