How to Handle Line Numbers and Text in a Menu-Driven String Editor?

  • Thread starter Trista
  • Start date
  • Tags
    String
In summary: Overall, the key is to break down the problem into smaller parts and tackle them one by one. In summary, to solve the problem of handling different types of input in the menu part of a program, one can use a loop and the cin.get() function to read in each character and determine its type. By breaking down the problem into smaller parts, it can become easier to handle and solve.
  • #1
Trista
33
0
I'm having some trouble writing the menu part of this program. Right now, I have everything in main() just for ease of compiling. The file is attached.

There are 5 basic commands: I, D, L, A, E
I = insert a line, D = Delete a line, L = list a line, A = Append to end of list,
E = exit.
Thats easy. The hard part is that a line number may follow the command.
I 5 means insert a string that follows before line 5.
D 5-13 means delete lines 5 through 13.
L 3 - 7 means list lines 3 - 7.

If text follows the numbers then it is considered a string and not a command.

I bring in the line using cin.getline(newString, 255, \n). newString is of type char. I'm not sure what to do with the loop to determine if there are numbers, then if the line is more than one digit ( 20 ) how do I push them together to represent the line number, AND then if there is letters after the numbers its a string... its all so confusing!

Please help me get through this! :eek:

thank you so much for your help!

Trista
 

Attachments

  • testLineEditor.cpp.txt
    2.2 KB · Views: 560
Technology news on Phys.org
  • #2
One way to approach this is to use a loop and the cin.get() function. Inside the loop, you can read in each character of the command one by one. You can then determine if the current character is a number, letter, space, or something else. Depending on what it is, you can take the appropriate action. For example, if you encounter a number, you can store it in a variable until you encounter a non-number character. This way, you can build up the line number that the user is entering. If you encounter a letter, you can break out of the loop and use the string that has been built up. This can be done with a simple if-else statement.
 
  • #3


Hello Trista,

I understand that you are having trouble writing the menu part of your string editor program. It can be overwhelming when there are multiple conditions and variables to consider, but don't worry, I am here to help.

Firstly, let's break down the 5 basic commands: I, D, L, A, and E.

- I: This command is used to insert a line. It can be followed by a line number, which indicates where the new line should be inserted.
- D: This command is used to delete a line. It can be followed by a line number or a range of line numbers to be deleted.
- L: This command is used to list a line. It can be followed by a line number or a range of line numbers to be listed.
- A: This command is used to append a line to the end of the list.
- E: This command is used to exit the program.

Now, let's focus on how to handle the line numbers and strings.

When a user enters a command followed by a line number, we need to separate the command and the line number. One way to do this is by using the getline() function and specifying a delimiter, in this case, a space. This will allow you to store the command in one variable and the line number in another variable.

Next, we need to check if the line number is a single digit or multiple digits. One way to do this is by using the isdigit() function, which checks if a character is a digit or not. If the line number is a single digit, you can simply convert it to an integer using the atoi() function. If the line number is multiple digits, you can use a loop to read each character and store them in a string. Once all the digits are read, you can then convert the string to an integer using the stoi() function.

Now, if there are letters after the numbers, it means that it is a string and not a line number. In this case, you can use the getline() function again to read the string and store it in a variable.

I hope this helps you understand how to handle the line numbers and strings in your menu-driven string editor program. Remember to break down the problem into smaller parts and take it one step at a time. If you need further assistance, don't hesitate to reach out for help. Good luck!
 

FAQ: How to Handle Line Numbers and Text in a Menu-Driven String Editor?

What is a Menu Driven String Editor?

A Menu Driven String Editor is a software tool that allows users to manipulate and edit text strings through a user-friendly menu interface. It typically offers a variety of options for editing and formatting text, such as adding or removing characters, changing case, and performing search and replace operations.

How does a Menu Driven String Editor work?

A Menu Driven String Editor works by first displaying a menu of options for the user to select from. Once an option is chosen, the editor prompts the user for the necessary input and performs the selected operation on the text string. This process can be repeated until the desired changes have been made.

What are the advantages of using a Menu Driven String Editor?

One advantage of using a Menu Driven String Editor is that it offers a simple and intuitive way to edit text strings, making it accessible for users with varying levels of technical expertise. It also allows for quick and easy modifications to be made, which can be especially useful when working with large amounts of text.

Are there any limitations to a Menu Driven String Editor?

One limitation of a Menu Driven String Editor is that it may not have the same level of flexibility and customization options as other types of text editors. It may also be limited in terms of the types of operations that can be performed on the text, depending on the specific features of the editor.

Can a Menu Driven String Editor be used for programming?

While a Menu Driven String Editor can be used for basic text manipulation, it is not typically designed for programming purposes. It may not have the advanced features and functionality needed for coding, such as syntax highlighting and debugging tools. However, some editors may offer additional features that make them suitable for basic programming tasks.

Similar threads

Replies
3
Views
724
Replies
3
Views
3K
Replies
36
Views
3K
Replies
6
Views
2K
Replies
1
Views
2K
Replies
20
Views
4K
Replies
2
Views
1K
Replies
6
Views
1K
Back
Top