Reading Different Commands in Order from Single String

NOTE: The solution cannot be arrays. It is apparently supposed to only include string operators/functions (such as find()) and counter-controlled/sentinel-controlled loops.

I'm working on a project that asks me to code a text editor. The user is prompted to input a sentence, then input a list of commands until they input "end". The program then executes those commands in the order they were typed. For a lot of it, it's not too difficult; I've completed several of the functions necessary for most of the commands the user can input (such as reversing the sentence).

However, here's my problem.

How can I get the user's command inputs to be done sequentially (in the order they're typed) when they input it as a single string?
As in, rather than doing this:
1
2
3
reverse
replace x y
end

They do this:
 
reverse replace x y end

...and the program still calls the reverse function first, then the replace function to replace x with y, and then ends?


From that, I have another question. What if the user doesn't type end in the first string, like this:
 
reverse replace x y reverse

It should allow the user to provide more input, but how could I store previous commands? Like, I get that the sentinel would loop until it read an "end" allowing a new prompt, but how would it store the previous commands (without doing them right away), wait until the user does input "end", then call all the functions as they were entered?

This is how the output on the screen should look:


1
2
3
4
5
6
7
8
9
10
11
12
Enter a sentence:          
This is a test. x         
reverse replace x y reverse end         
//endl
Doing reverse...
x .tset a si shiT
Replacing all x with y...
y .tset a si shiT
Doing reverse...
This is a test. y
Ending...


This is how the program is intended to look and execute. I'm still really lost on how to properly allow the user to input the commands in one single string (so, user inputs sentence, then inputs commands all on one line).

Speaking of the replace function (since I feel the answers to these two problems will be similar/the same), much like how I'm confused how a single string can be entered and still be executed sequentially, how do I make the user's input of "replace x y" store the command, *then* store the individual chars as two variables? Like, this might be an elementary question, but how do I separate the three parts when the user is supposed to input them with only single spaces in between?

I can definitely figure out where to go and how to finish it up once I figure out the answers to these questions, but I'm thoroughly stumped. I appreciate any help or guidance. As it is currently, there's a lot of code and it's clunky and messy, but I'll provide snips as best I can very soon (as of posting this).

Last edited on
An outer while loop reads each command and processes it. Part of processing the command might involve reading more strings from the input. For example, the replace command will read the "from" and "to" words:
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
27
28
#include <iostream>
#include <string>
#include <algorithm>

using std::string;
using std::cin;
using std::cout;

int main()
{
    string sentence;
    string command;

    cout << "Enter a sentence: ";
    getline(cin, sentence);
    while (cin >> command) {
        if (command == "reverse") {
            std::reverse(sentence.begin(), sentence.end());
        } else if (command == "replace") {
            string from, to;
            cin >> from >> to;
            // replace from with to
        } else if (command == "end") {
            break;
        }
    }
    cout << sentence << '\n';
}


@dhayden, what you suggested seems like it on to something. However, I don't think it does what I need it to do. I'll use the code box to represent the running program:

EDIT: Moved the code to the OP, since it cleaned it up and made it easier to read. It's below, too, though.

1
2
3
4
5
6
7
8
9
10
11
12
Enter a sentence:           //the prompt
This is a test. x          //the user's getline sentence
reverse replace x y reverse end          //the commands, all on one line
//endl
Doing reverse...
x .tset a si shiT
Replacing all x with y...
y .tset a si shiT
Doing reverse...
This is a test. y
Ending...


This is how the program is intended to look and execute. I'm still really lost on how to properly allow the user to input the commands in one single string (so, user inputs sentence, then inputs commands all on one line). Does this illustrate it better?
Last edited on
What I'm suggesting will work, you just have to fill in the rest of the code.
Topic archived. No new replies allowed.