I am writing a C++ program that accepts a user\'s input via the console and save
ID: 3665646 • Letter: I
Question
I am writing a C++ program that accepts a user's input via the console and saves the inputted text as a char*. If the char* string is in the form "command(num1, num2)", where command is one of the actions the user wishes to apply to number 1 and number 2, how can I parse the text to be able to save num1 and num2 to global variables in main? That way I can actually apply the desired action. I am able to get main to determine what command is desired (used an if statement with string compare), but the parenthesis and comma are giving me trouble when trying to save the numbers in the char*. I am writing a C++ program that accepts a user's input via the console and saves the inputted text as a char*. If the char* string is in the form "command(num1, num2)", where command is one of the actions the user wishes to apply to number 1 and number 2, how can I parse the text to be able to save num1 and num2 to global variables in main? That way I can actually apply the desired action. I am able to get main to determine what command is desired (used an if statement with string compare), but the parenthesis and comma are giving me trouble when trying to save the numbers in the char*.Explanation / Answer
You can use split function so that when ever you come across special symbols like paranthesis in your case it will be splited and stored seperately.
If you want an easier on cpp has a special function strtok. This function divides the entire string into tokens so that you can use the tokens as per requirements.
Syntax: char * strtok ( char * str, const char * delimiters );
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.