I'm only a few days new to C++ and I'm working on a program where I need to give commands and a coresponding function is called. At the moment the code uses the given string and goes through "if string = functionname then..." but I am trying to be more efficient. What I want to do is give the function name, then it searches through a list of function names, when it matches up it calls that function. I hope my coding isn't too confusing. I'm new at this so go easy, any help would be apreciated.

thanks, WhiteHatHacker

code so far:

#include <iostream>
#include <string>
using std::string;
string GetName(string enterYourName); //declares function Login
string GetPass(string enteryourPass);
string GetCmd(string& CMD, string& yourName);
string CheckCmd(string& CMD, string& yourName);
int main()
{
string CMD;
string yourName; //declares userName as string
string yourPass; //declares yourPass as string
yourName = GetName(yourName); //Sends userName to Login to have a value returned to it
yourPass = GetPass(yourPass);
std::cout<<"\n"<<"You are now logged on as:"<<yourName;
std::cout<<"\n"<<yourName<<":";
CMD = GetCmd(CMD, yourName);
return 0;
}

string GetName(string yourName) //Beginning of function
{
string enterYourName;
std::cout<<"Initializing connection..."<<"\n";
std::cout<<"Main screen login, please provide a user name and password."<<"\n";
std::cout<<"Username:";
std::cin>>enterYourName;
return enterYourName;
}

string GetPass(string yourPass)
{
string enterYourPass;
std::cout<<"Password:";
std::cin>>enterYourPass;
return enterYourPass;
}

string GetCmd(string& CMD, string& yourName)
{
std::cin>>CMD;
CMD = CheckCmd(CMD, yourName);
return CMD;
}
string CheckCmd(string& CMD, string& yourName)
{
if (CMD == "god")
std::cout<<"You are god.";
else
std::cout<<"invalid command.";
std::cout<<"\n"<<yourName<<":";
GetCmd(CMD, yourName);
return CMD, yourName;
}

There are several ways to implement your problem, but the simplest for you is to just use a series of if statements

if( function == "foo")
   foo();
else if( function == "foo1")
   foo1();
// etc etc

There are a lot more efficient ways to code it but I doubt at your level you would understand them. Probably the best is to use a <map> and function pointers. If you think you can do it then by all means go for it.

There are a lot more efficient ways to code it but I doubt at your level you would understand them. Probably the best is to use a <map> and function pointers. If you think you can do it then by all means go for it.

Thanks, I used to program in basic so I do have some experience in programming just not C++, although they are quite similar. I'll look up <map> and function pointers and see what I can do. I was planning on have a lot of function commands and if I use the if then statement method I'm sure it will be slow and inefficient. I rather write efficient code now than have to learn new methods later and fix coding.

Was there anything else in my code that I could do differently to make it more efficient?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.