Hello!
I'm totally new to this forum , C++ and any programing what so ever, just started out learning yesterday when i studied some source codes for very small SDL based applications and i got very interested in programing, so i decided to give it a try!
The program basically prompts you for a command and then runs a function according to that command.
There are currently only 3 commands however, a command to quit the program, a command to list all available commands and a command to run a simple BMI calculator i wrote.
I plan on using this as a collection program for any nifty little code i write.
Please give me feedback and tips on how to improve the structure of this little program of mine!
Suggestions:
- Eliminate the global variables. In this case, they are not necessary and can be moved to more local scopes.
- Make constants, like "cmdList" constant using the const qualifier.
- Handle erroneous input. What if some wise guy enters "a" as his height?
I have made the cmd variable a local variable to the while function within my main function.
I also removed the cmdList variable altogether and replaced it with a definition for the compiler, no sure if its the right thing to do or not so please let me know if and why it would be better to revert back to say: const string cmdList = "bmi, commands, quit";
Instead of using a definition.
I do feel that the q variable fills its purpose best as a global variable, however, I might be wrong about that so please give me some tips if that is the case.
OK, so i decided to rearrange my program a little by moving all my "command" functions to a separate source file called cmd.cpp.
I also thought i would try a more "open" way to handle my "command function" calls by simply calling a function named whatever the user inputs, without checking for special string values before calling.
here are my files:
what i am trying to do her is to set the string cmd to whatever the user enters, and then use the value of cmd to call a function named whatever the user entered, even if that function does not exist.
and to my limited knowledge this piece of code: cmd ();
would replace cmd with the value stored in the cmd variable and call the resulting function, but obviously it does not do that and instead it tries to call a function named cmd, which is not defined anywhere in my code, and the compiler fails.
What i need help with is a way to make the value of cmd be the name of the called function.
What you're trying there is something that can't be done. You can only call defined functions in the code you've written. User input is just a text string, just data. You code the functions and control the flow of the program (through things like if/else) according to the data entered.
You could learn about passing parameters to functions by doing something like this:
Make a function that takes the user input and calls the appropriate function for their choice, like you originally had for in your main function. This function however returns false if the user chose to quit and true if they chose something else.
This way you can use your while loop with this function to determine if yould ask again or end the program, and it would mean you can ditch the q global variable.
Some hints
1 2 3 4 5 6 7 8 9
int main()
{
string cmd;
do{
cout << "Enter Command: ";
cin >> cmd;
}while( userinput(cmd) ); // passes the user input to the function and checks the return
// value to see if we should loop again or not.
}
The user input function would be defined as something like:
1 2 3 4 5 6
bool userinput(string inp)
{
//and in here put your code for decieding which function to call
// if they enter to quit return false
// for all other entries return true.
}
Alright, thanks for the help Moooce.
I have set up my code a you described and it is working like a charm!
But it did not accomplish exactly what i was looking for.
What i want to do is to take whatever the user inputs to the cmd string, check if there is a function that can be called with that "name" and then do so, or return a "invalid command" message if there is no such function.
So if the user enters "area" when prompted for a command, i want to check if area (); is defined anywhere in the code (cmd.cpp whould be where i define the functions) and run it if it is or return a "Invalid command" message if it is not.
The idea here is that i won't have to add a if (cmd == "area") function for every "command" i add to the program.
I would simply write the function in cmd.cpp and it is usable in my program.
Alas computers are not intelligent ;-)
You have to instruct it what specified user input you want to cause a piece of code to run. There may be programming languages out there that can do the sort of thing you're attempting, but under the hood they have to do the above at some point.
Remember C++ is a compiled, not interpreted language, so it will contain the code you write in binary form, so you know what functions relate to what command the user enters. If the user entered a command you havn't written yet, how would you get the computer to run that command without adding it to your code as a function, and to run it would have to have been written and compiled previously.
What you ask could possibly be done say, by writing a program that read in an .ini file containing a list of commands and linking them to DLL files that could be added to and loaded by your program later, but that all gets a bit complex for a first program! or... maybe I'm misunderstanding you...
Anyway, to help, have a look into the different type of branching that C++ can do in the tutorials section. I think the switch/case keywords would maybe make what you're doing a bit easier, but remember you can only make decisions with numbers, so you would have to convert your commands to numberic values.