-
Problem reading input
Hey, I got a problem.
I am to write a program that takes 4 different commands and performs different actions for each individual command. The problem is that the commands are not of the same format.
Here is what the commands take.
command 1: char d, int, int.
command 2: char a, int.
command 3: char s.
command 4: char e.
Its all read in one line. I cant type 'd', hit enter and then prompt for 2 integers.
What is a smart solution?
I was thinking about reading it all in as a string, but then im not sure how to let the program know to look for an integer (integer could be 1 to 10000).
Any thoughts would be appreciated
-
Yes, read it as a string. Get that much code written, and come back for more help.
-
after you read it as a string, you can then tokenize it with strtok.
-
// Get command from user (eg. fgets)
// Don't forget to remove the trailing newline character
Input = "command1 13 842\n"
// Split command into words (eg. strtok)
Word[0] = "command1"
Word[1] = "13"
Word[2] = "842"
// See what command the user has entered (eg. if,else,then,case)
// If the command has additional parameters, work with the tokenized strings (eg. atoi)
Code:
If Word[0] == "command1" Then
Convert Word[1] to integer
Convert Word[2] to integer
DoStuff();
Else If Word[0] == "command2" Then
Convert Word[1] to integer
DoStuff();
Else If Word[0] == "command3" Then
DoStuff();
Else If Word[0] == "command4" Then
DoStuff();
Else
UnknownCommand();
End If